Skip to content

Commit

Permalink
Add http client, support company/get and unit/lists requests
Browse files Browse the repository at this point in the history
  • Loading branch information
IngusSkaistkalns committed Mar 5, 2018
1 parent c76bddf commit 236c04a
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
/spec/reports/
/tmp/

.env

# rspec failure tracking
.rspec_status
.rspec_status
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gemspec

gem 'dotenv'
18 changes: 18 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@ PATH
remote: .
specs:
mapon_client (0.1.0)
rest-client (> 1)

GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.3)
domain_name (0.5.20170404)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.2.1)
http-cookie (1.0.3)
domain_name (~> 0.5)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
netrc (0.11.0)
rake (10.5.0)
rest-client (2.0.2)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rspec (3.7.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
Expand All @@ -21,12 +35,16 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-support (3.7.1)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.5)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 1.16)
dotenv
mapon_client!
rake (~> 10.0)
rspec (~> 3.0)
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,25 @@ Or install it yourself as:

## Usage

TODO: Write usage instructions here
### Instantiate client:
* api_key - create it under Settings/API keys, if blank, will try to read environment variable MAPON_CLIENT_API_KEY on the [site](https://mapon.com/new/settings#9)
* base_url - defaults to https://mapon.com/api/v1/
* format - json/xml, defaults to json

```ruby
mapon_client = MaponClient::Client.new
# OR
mapon_client = MaponClient::Client.new(
api_key: ENV['BY_DIFFERENT_NAME_API_KEY'],
base_url: 'https://mapon.com/api/v300/not-the-default-endpoint',
format: 'xml'
)
```
### Rest client

MaponClient uses [rest-client](https://github.com/rest-client/rest-client) as HTTP client.
So on any lower level calls rest-client library class instances gets returned,
like RestClient::Response, RestClient::Resource, etc.

## Development

Expand Down
1 change: 1 addition & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "dotenv/load"
require "mapon_client"

# You can add fixtures and/or initialization code here to make experimenting
Expand Down
4 changes: 4 additions & 0 deletions lib/mapon_client.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
require "mapon_client/version"

module MaponClient
DEFAULT_BASE_URL = 'https://mapon.com/api/v1/'.freeze
DEFAULT_FORMAT = 'json'.freeze

autoload :Client, 'mapon_client/client'
end
43 changes: 43 additions & 0 deletions lib/mapon_client/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'rest-client'

module MaponClient
class Client
attr_reader :api_key, :base_url, :resource_base, :format

def initialize(api_key: nil, base_url: nil, format: nil)
@api_key = (
api_key || ENV['MAPON_CLIENT_API_KEY']
)

@base_url = (
base_url || MaponClient::DEFAULT_BASE_URL
)

@format = (
format || MaponClient::DEFAULT_FORMAT
)

@resource_base ||= RestClient::Resource.new(
@base_url, headers: { params: { key: @api_key } }
)
end

def [](*args)
@resource_base.public_send(:[], *args)
end

def company
self[with_format("company/get")].get
end

def unit_list
self[with_format("unit/list")].get
end

private

def with_format(suburl)
"#{suburl}.#{@format}"
end
end
end
2 changes: 2 additions & 0 deletions mapon_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "rest-client", ">1"

spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
Expand Down
16 changes: 16 additions & 0 deletions spec/mapon_client/client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
RSpec.describe MaponClient::Client do

let(:mapon_client) {
MaponClient::Client.new(
api_key: 'fake_key_123',
base_url: 'https://mapon.com/api/v1',
format: 'json'
)
}

it "#[] should delegate calls to #resource_base" do
block_to_pass = Proc.new { }
expect(mapon_client.resource_base).to receive(:[]).with('company/get', &block_to_pass)
mapon_client.public_send(:[], 'company/get', &block_to_pass)
end
end
4 changes: 4 additions & 0 deletions spec/mapon_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
it "has a version number" do
expect(MaponClient::VERSION).not_to be nil
end

it "::DEFAULT_BASE_URL should be https://api.mapon.com/api/v1/" do
expect(MaponClient::DEFAULT_BASE_URL).to eq("https://mapon.com/api/v1/")
end
end

0 comments on commit 236c04a

Please sign in to comment.