apikit 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/Rakefile +3 -4
 - data/lib/apikit/client.rb +16 -3
 - data/lib/apikit/configuration.rb +19 -0
 - data/lib/apikit/version.rb +1 -1
 - data/lib/apikit.rb +19 -0
 - data/spec/apikit/client_spec.rb +47 -3
 - data/spec/spec_helper.rb +2 -2
 - metadata +4 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 5d48c6faae790450e338f79a2d082a5651bbe99b
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: e1f216aec8f006fb59b7ac2cb99619bffa85094c
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 61580572c6c92b11c00881b4805bfaf8cc1032c45985f893771b513ec00869813163b8fe0f4303e016c8f45137cd29ed65f3927a032fcf15119b68f256dc561b
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 5abab6118e164d2f7811c3d5c180174d6068a391dbceed21637e577eeaa63afdad288891ee8fe14828a12af2cd2b7882b32b1487b4dfe19ae2d53d9f536cee88
         
     | 
    
        data/Rakefile
    CHANGED
    
    
    
        data/lib/apikit/client.rb
    CHANGED
    
    | 
         @@ -1,11 +1,15 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            module Apikit
         
     | 
| 
       2 
2 
     | 
    
         
             
              class Client
         
     | 
| 
       3 
     | 
    
         
            -
                attr_accessor :api_endpoint
         
     | 
| 
      
 3 
     | 
    
         
            +
                attr_accessor :api_endpoint, :config
         
     | 
| 
       4 
4 
     | 
    
         | 
| 
       5 
     | 
    
         
            -
                 
     | 
| 
      
 5 
     | 
    
         
            +
                # options:
         
     | 
| 
      
 6 
     | 
    
         
            +
                def initialize(api_endpoint, config = Apikit.config )
         
     | 
| 
       6 
7 
     | 
    
         
             
                  @api_endpoint = api_endpoint
         
     | 
| 
      
 8 
     | 
    
         
            +
                  @config = config
         
     | 
| 
       7 
9 
     | 
    
         
             
                end
         
     | 
| 
       8 
10 
     | 
    
         | 
| 
      
 11 
     | 
    
         
            +
                # options:
         
     | 
| 
      
 12 
     | 
    
         
            +
                #   query:
         
     | 
| 
       9 
13 
     | 
    
         
             
                def get(path, options = {})
         
     | 
| 
       10 
14 
     | 
    
         
             
                  request :get, path, options
         
     | 
| 
       11 
15 
     | 
    
         
             
                end
         
     | 
| 
         @@ -30,11 +34,20 @@ module Apikit 
     | 
|
| 
       30 
34 
     | 
    
         
             
                private
         
     | 
| 
       31 
35 
     | 
    
         | 
| 
       32 
36 
     | 
    
         
             
                def agent
         
     | 
| 
       33 
     | 
    
         
            -
                  @agent ||= Sawyer::Agent.new(api_endpoint) do |http|
         
     | 
| 
      
 37 
     | 
    
         
            +
                  @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
         
     | 
| 
       34 
38 
     | 
    
         
             
                    http.headers[:content_type] = "application/json"
         
     | 
| 
       35 
39 
     | 
    
         
             
                  end
         
     | 
| 
       36 
40 
     | 
    
         
             
                end
         
     | 
| 
       37 
41 
     | 
    
         | 
| 
      
 42 
     | 
    
         
            +
                def sawyer_options
         
     | 
| 
      
 43 
     | 
    
         
            +
                  {}.tap do |opts|
         
     | 
| 
      
 44 
     | 
    
         
            +
                    opts[:faraday] = Faraday.new do |conn|
         
     | 
| 
      
 45 
     | 
    
         
            +
                      config.faraday_options.call(conn) if config.faraday_options
         
     | 
| 
      
 46 
     | 
    
         
            +
                      conn.adapter Faraday.default_adapter
         
     | 
| 
      
 47 
     | 
    
         
            +
                    end
         
     | 
| 
      
 48 
     | 
    
         
            +
                  end
         
     | 
| 
      
 49 
     | 
    
         
            +
                end
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
       38 
51 
     | 
    
         
             
              end
         
     | 
| 
       39 
52 
     | 
    
         
             
            end
         
     | 
| 
       40 
53 
     | 
    
         | 
| 
         @@ -0,0 +1,19 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Apikit
         
     | 
| 
      
 2 
     | 
    
         
            +
              class Configuration
         
     | 
| 
      
 3 
     | 
    
         
            +
                attr_reader :faraday_options
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
                def initialize
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
                end
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
                def config_faraday(&config_block)
         
     | 
| 
      
 10 
     | 
    
         
            +
                  @faraday_options = config_block
         
     | 
| 
      
 11 
     | 
    
         
            +
                end
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                def self.default
         
     | 
| 
      
 14 
     | 
    
         
            +
                  new
         
     | 
| 
      
 15 
     | 
    
         
            +
                end
         
     | 
| 
      
 16 
     | 
    
         
            +
              end
         
     | 
| 
      
 17 
     | 
    
         
            +
              Config = Configuration
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
            end
         
     | 
    
        data/lib/apikit/version.rb
    CHANGED
    
    
    
        data/lib/apikit.rb
    CHANGED
    
    | 
         @@ -1,7 +1,26 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            require 'sawyer'
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            require "apikit/version"
         
     | 
| 
      
 4 
     | 
    
         
            +
            require "apikit/configuration"
         
     | 
| 
       4 
5 
     | 
    
         
             
            require 'apikit/client'
         
     | 
| 
       5 
6 
     | 
    
         | 
| 
       6 
7 
     | 
    
         
             
            module Apikit
         
     | 
| 
      
 8 
     | 
    
         
            +
              class << self
         
     | 
| 
      
 9 
     | 
    
         
            +
                attr_reader :configuration
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                def configuration
         
     | 
| 
      
 12 
     | 
    
         
            +
                  @configuration ||= Configuration.new
         
     | 
| 
      
 13 
     | 
    
         
            +
                end
         
     | 
| 
      
 14 
     | 
    
         
            +
                alias config configuration
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
                def reset_config
         
     | 
| 
      
 17 
     | 
    
         
            +
                  @configuration = Configuration.new
         
     | 
| 
      
 18 
     | 
    
         
            +
                end
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
                def configure
         
     | 
| 
      
 21 
     | 
    
         
            +
                  yield configuration
         
     | 
| 
      
 22 
     | 
    
         
            +
                end
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
              end
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
       7 
26 
     | 
    
         
             
            end
         
     | 
    
        data/spec/apikit/client_spec.rb
    CHANGED
    
    | 
         @@ -2,22 +2,66 @@ 
     | 
|
| 
       2 
2 
     | 
    
         
             
            describe Apikit::Client do
         
     | 
| 
       3 
3 
     | 
    
         
             
              let(:api_endpoint) { "http://awesome.api"}
         
     | 
| 
       4 
4 
     | 
    
         
             
              let(:client) { Apikit::Client.new(api_endpoint) }
         
     | 
| 
      
 5 
     | 
    
         
            +
              let(:path) { '/ping.json' }
         
     | 
| 
      
 6 
     | 
    
         
            +
              let(:stub_ping) { stub_request(:get, api_endpoint + path) }
         
     | 
| 
       5 
7 
     | 
    
         | 
| 
       6 
8 
     | 
    
         
             
              describe ".get" do
         
     | 
| 
       7 
     | 
    
         
            -
                let(:path) { '/ping.json' }
         
     | 
| 
       8 
9 
     | 
    
         | 
| 
       9 
10 
     | 
    
         
             
                it "get parsed result " do
         
     | 
| 
       10 
     | 
    
         
            -
                  stub_get =  
     | 
| 
      
 11 
     | 
    
         
            +
                  stub_get = stub_ping.to_return(json_response({ok: true}))
         
     | 
| 
       11 
12 
     | 
    
         
             
                  result = client.get(path)
         
     | 
| 
       12 
13 
     | 
    
         
             
                  expect(result.ok).to eq(true)
         
     | 
| 
       13 
14 
     | 
    
         
             
                end
         
     | 
| 
       14 
15 
     | 
    
         | 
| 
       15 
16 
     | 
    
         
             
                it "get string result (without json response header)" do
         
     | 
| 
       16 
     | 
    
         
            -
                  stub_get =  
     | 
| 
      
 17 
     | 
    
         
            +
                  stub_get = stub_ping.to_return(body: {ok: true}.to_json)
         
     | 
| 
       17 
18 
     | 
    
         
             
                  result = client.get(path)
         
     | 
| 
       18 
19 
     | 
    
         
             
                  expect(result).to eq("{\"ok\":true}")
         
     | 
| 
       19 
20 
     | 
    
         
             
                end
         
     | 
| 
       20 
21 
     | 
    
         | 
| 
      
 22 
     | 
    
         
            +
                it "handle query parameters" do
         
     | 
| 
      
 23 
     | 
    
         
            +
                  stub_get = stub_ping.with(query: {timeout: 5}).to_return(body: {ok: true}.to_json)
         
     | 
| 
      
 24 
     | 
    
         
            +
                  result = client.get(path, query: {timeout: 5})
         
     | 
| 
      
 25 
     | 
    
         
            +
                  expect(result).to eq("{\"ok\":true}")
         
     | 
| 
      
 26 
     | 
    
         
            +
                end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
                it "handle request fail" do
         
     | 
| 
      
 29 
     | 
    
         
            +
                  stub_get = stub_ping.to_return(json_response({ok: false}, {status: [500, "Internal Server Error"]}))
         
     | 
| 
      
 30 
     | 
    
         
            +
                  result = client.get(path)
         
     | 
| 
      
 31 
     | 
    
         
            +
                  expect(result.ok).to eq(false)
         
     | 
| 
      
 32 
     | 
    
         
            +
                end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
                it "handle request fail, when invalid json, parsed response is extract what the server returns" do
         
     | 
| 
      
 35 
     | 
    
         
            +
                  stub_get = stub_ping.to_return(json_response("OMG, WTF", {status: [500, "Internal Server Error"]}))
         
     | 
| 
      
 36 
     | 
    
         
            +
                  result = client.get(path)
         
     | 
| 
      
 37 
     | 
    
         
            +
                  expect(result).to eq("OMG, WTF")
         
     | 
| 
      
 38 
     | 
    
         
            +
                end
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
              end
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
              describe ".post" do
         
     | 
| 
      
 43 
     | 
    
         
            +
                it "handle query parameters" do
         
     | 
| 
      
 44 
     | 
    
         
            +
                  stub_get = stub_request(:post, api_endpoint + path).with(query: {timeout: 5}).to_return(body: {ok: true}.to_json)
         
     | 
| 
      
 45 
     | 
    
         
            +
                  result = client.post(path, query: {timeout: 5})
         
     | 
| 
      
 46 
     | 
    
         
            +
                  expect(result).to eq("{\"ok\":true}")
         
     | 
| 
      
 47 
     | 
    
         
            +
                end
         
     | 
| 
      
 48 
     | 
    
         
            +
              end
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
              describe "options[:config]" do
         
     | 
| 
      
 51 
     | 
    
         
            +
                let(:config) do
         
     | 
| 
      
 52 
     | 
    
         
            +
                  Apikit::Config.new.tap do |config|
         
     | 
| 
      
 53 
     | 
    
         
            +
                    config.config_faraday do |conn|
         
     | 
| 
      
 54 
     | 
    
         
            +
                      conn.use Faraday::Response::RaiseError
         
     | 
| 
      
 55 
     | 
    
         
            +
                    end
         
     | 
| 
      
 56 
     | 
    
         
            +
                  end
         
     | 
| 
      
 57 
     | 
    
         
            +
                end
         
     | 
| 
      
 58 
     | 
    
         
            +
                let(:client) { Apikit::Client.new(api_endpoint, config) }
         
     | 
| 
      
 59 
     | 
    
         
            +
             
     | 
| 
      
 60 
     | 
    
         
            +
                it "should use the raise error middleware" do
         
     | 
| 
      
 61 
     | 
    
         
            +
                  stub_get = stub_ping.to_return(json_response({ok: false}, {status: [500, "Internal Server Error"]}))
         
     | 
| 
      
 62 
     | 
    
         
            +
                  expect { result = client.get(path) }.to raise_error(Faraday::ClientError)
         
     | 
| 
      
 63 
     | 
    
         
            +
                end
         
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
       21 
65 
     | 
    
         
             
              end
         
     | 
| 
       22 
66 
     | 
    
         | 
| 
       23 
67 
     | 
    
         | 
    
        data/spec/spec_helper.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: apikit
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.2
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - niedhui
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2014-12- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2014-12-15 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: sawyer
         
     | 
| 
         @@ -68,6 +68,7 @@ files: 
     | 
|
| 
       68 
68 
     | 
    
         
             
            - apikit.gemspec
         
     | 
| 
       69 
69 
     | 
    
         
             
            - lib/apikit.rb
         
     | 
| 
       70 
70 
     | 
    
         
             
            - lib/apikit/client.rb
         
     | 
| 
      
 71 
     | 
    
         
            +
            - lib/apikit/configuration.rb
         
     | 
| 
       71 
72 
     | 
    
         
             
            - lib/apikit/version.rb
         
     | 
| 
       72 
73 
     | 
    
         
             
            - spec/apikit/client_spec.rb
         
     | 
| 
       73 
74 
     | 
    
         
             
            - spec/spec_helper.rb
         
     | 
| 
         @@ -98,3 +99,4 @@ summary: try to implement an api consumer 
     | 
|
| 
       98 
99 
     | 
    
         
             
            test_files:
         
     | 
| 
       99 
100 
     | 
    
         
             
            - spec/apikit/client_spec.rb
         
     | 
| 
       100 
101 
     | 
    
         
             
            - spec/spec_helper.rb
         
     | 
| 
      
 102 
     | 
    
         
            +
            has_rdoc: 
         
     |