diff --git a/.gitignore b/.gitignore index b04a8c8..5256187 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,7 @@ /spec/reports/ /tmp/ +.env + # rspec failure tracking -.rspec_status +.rspec_status \ No newline at end of file diff --git a/Gemfile b/Gemfile index d2fceab..cd3507a 100644 --- a/Gemfile +++ b/Gemfile @@ -3,3 +3,5 @@ source "https://rubygems.org" git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } gemspec + +gem 'dotenv' diff --git a/Gemfile.lock b/Gemfile.lock index d8e06f7..e374b0e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) diff --git a/README.md b/README.md index 710365c..e0d378b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bin/console b/bin/console index a77a7ba..b5a269a 100755 --- a/bin/console +++ b/bin/console @@ -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 diff --git a/lib/mapon_client.rb b/lib/mapon_client.rb index 60ffdfc..f6e3fc3 100644 --- a/lib/mapon_client.rb +++ b/lib/mapon_client.rb @@ -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 diff --git a/lib/mapon_client/client.rb b/lib/mapon_client/client.rb new file mode 100644 index 0000000..e91fc69 --- /dev/null +++ b/lib/mapon_client/client.rb @@ -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 diff --git a/mapon_client.gemspec b/mapon_client.gemspec index 3d388f1..a12ae2c 100644 --- a/mapon_client.gemspec +++ b/mapon_client.gemspec @@ -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" diff --git a/spec/mapon_client/client_spec.rb b/spec/mapon_client/client_spec.rb new file mode 100644 index 0000000..941ccbb --- /dev/null +++ b/spec/mapon_client/client_spec.rb @@ -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 diff --git a/spec/mapon_client_spec.rb b/spec/mapon_client_spec.rb index 6051aff..238f87a 100644 --- a/spec/mapon_client_spec.rb +++ b/spec/mapon_client_spec.rb @@ -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