abstract_api_wrapper 1.2.0 → 1.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 382d69983b02149041ca9c27e0fed424c84a147b
4
- data.tar.gz: dca2dc1bf4ade4eaf0f1946839b0c9fcc6b710e1
3
+ metadata.gz: d44dac1f1b099e9dade84a4fd871547a1778c855
4
+ data.tar.gz: ccf0fbf273198e999b19796f65e23f16f4899fc9
5
5
  SHA512:
6
- metadata.gz: c83865eb6828abc65adde8eb510b776204e380a81689d468596be7d6c501cec176a92e8b8586bd5d8945a398ad863321a52b8d72783e97a5a8cdcfe1e6966528
7
- data.tar.gz: 5693c006e199cde8b6ef97270673752f59d174aaade72219c0714bd4f6bf4a9f531d5f39bc39c8fd7a74169ed618f76ad8e221990b4afc1b6f5689ca41660cd8
6
+ metadata.gz: 9594e279953f13a993239b5bf7b0ec10df2bcf4d75a8fdb91e024752b70c89282313d6444809b540d612f2bcea1d2f04e9b2d8e6ccfb5386bae21ab41f477bf5
7
+ data.tar.gz: f41d40d4cb674ebc1160c8b542d1557c7bacc18003ec91b7b607644bd03d7e94df32596f39de1b98dd9811596b09917d173018fdd271bf62d7ec1a56780eb956
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # AbstractApiWrapper
2
2
 
3
- TODO: summary
3
+ An abstract REST API wrapper based on `missing_method` magic.
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,24 +20,50 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- Modify the `endpoint` method appending the `.json` extension to all requests
23
+ Define a new class and extend it with `AbstractApiWrapper`, then Modify the `Request#endpoint` and/or `Request#headers` methods to fit your requirements.
24
24
 
25
25
  ```ruby
26
- def endpoint
27
- query_string = filters.map { |k, v| "#{k}=#{v}" }.join('&')
28
- "#{@client.base_url}/#{path.join('/')}.json?#{query_string}"
26
+ # Basecamp example
27
+ class BasecampApi < AbstractApiWrapper
28
+ class Client < BasecampApi::Client
29
+ def method_missing(name, *params, &block)
30
+ BasecampApi::Request.new(name.to_s, self)
31
+ end
32
+ end
33
+
34
+ class Request < BasecampApi::Request
35
+ def headers
36
+ # Use Bearer instead of token
37
+ {
38
+ 'Authorization' => "Bearer #{@client.access_token}",
39
+ 'Content-Type' => 'application/json'
40
+ }
41
+ end
42
+
43
+ def endpoint
44
+ # Adds the .json at the end of each request
45
+ query_string = filters.map { |k, v| "#{k}=#{v}" }.join('&')
46
+ "#{@client.base_url}/#{path.join('/')}.json?#{query_string}"
47
+ end
48
+ end
29
49
  end
30
50
  ```
31
51
 
32
52
  ```ruby
33
53
  ACCESS_TOKEN = "YOUR ACCESS TOKEN"
34
- client = AbstractApiWrapper::Client.new(access_token: ACCESS_TOKEN, base_url: 'https://launchpad.37signals.com')
54
+
55
+ client = BasecampApi::Client.new(
56
+ access_token: ACCESS_TOKEN,
57
+ base_url: 'https://launchpad.37signals.com'
58
+ )
59
+
35
60
  authorizations = client.authorization.all.run
36
- # => <AbstractApiWrapper::Response::Resource accounts=#<Hashie::Array [#<AbstractApiWrapper::Response::Resource app_href="https://basecamp.com/xxxxxxx" href="https://basecamp.com/xxxxxxx/api/v1" id=xxxxxxx name="Your Company" product="bcx">, #<AbstractApiWrapper::Response::Resource app_href="https://basecamp.com/xxxxxxx" href="https://basecamp.com/xxxxxxx/api/v1" id=xxxxxxx name="Another company" product="bcx">]> expires_at="2016-12-19T18:13:52.000Z" identity=#<AbstractApiWrapper::Response::Resource email_address="email@yourcompany.com" first_name="Juan" id=xxxxxx last_name="Puelpan">>
61
+ # => <BasecampApi::Response::Resource accounts=#<Hashie::Array [#<BasecampApi::Response::Resource app_href="https://basecamp.com/xxxxxxx" href="https://basecamp.com/xxxxxxx/api/v1" id=xxxxxxx name="Your Company" product="bcx">, #<BasecampApi::Response::Resource app_href="https://basecamp.com/xxxxxxx" href="https://basecamp.com/xxxxxxx/api/v1" id=xxxxxxx name="Another company" product="bcx">]> expires_at="2016-12-19T18:13:52.000Z" identity=#<BasecampApi::Response::Resource email_address="email@yourcompany.com" first_name="Juan" id=xxxxxx last_name="Puelpan">>
37
62
 
38
63
  account = authorizations.accounts.first
39
- client = AbstractApiWrapper::Client.new(access_token: ACCESS_TOKEN, base_url: account.href)
64
+ client = BasecampApi::Client.new(access_token: ACCESS_TOKEN, base_url: account.href)
40
65
  projects = client.projects.all.run
66
+
41
67
  puts projects
42
68
  puts projects.pagination
43
69
  ```
@@ -14,7 +14,7 @@ class AbstractApiWrapper
14
14
  end
15
15
 
16
16
  def method_missing(name, *params, &block)
17
- AbstractApiWrapper::Request.new(name.to_s, self)
17
+ Request.new(name.to_s, self)
18
18
  end
19
19
  end
20
20
 
@@ -80,7 +80,7 @@ class AbstractApiWrapper
80
80
  params = @params.any? ? @params.to_json : nil
81
81
 
82
82
  request = Faraday.send(method, endpoint, params, headers)
83
- response = AbstractApiWrapper::Response.new(request)
83
+ response = Response.new(request)
84
84
 
85
85
  # Reset variables
86
86
  path = []
@@ -122,13 +122,13 @@ class AbstractApiWrapper
122
122
 
123
123
  def body
124
124
  if @parsed_body.is_a?(Hash)
125
- AbstractApiWrapper::Response::Resource.new(@parsed_body, @request)
125
+ Response::Resource.new(@parsed_body, @request)
126
126
  elsif @parsed_body.is_a?(Array)
127
127
  collection = @parsed_body.map do |item|
128
- AbstractApiWrapper::Response::Resource.new(item)
128
+ Response::Resource.new(item)
129
129
  end
130
130
 
131
- AbstractApiWrapper::Response::Collection.new(collection, @request)
131
+ Response::Collection.new(collection, @request)
132
132
  end
133
133
  end
134
134
 
@@ -1,3 +1,3 @@
1
1
  class AbstractApiWrapper
2
- VERSION = '1.2.0'
2
+ VERSION = '1.3.0'
3
3
  end
@@ -1,9 +1,4 @@
1
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
- require 'webmock/rspec'
3
- require 'abstract_api_wrapper'
4
-
5
- include WebMock::API
6
- WebMock.enable!
1
+ require 'helper'
7
2
 
8
3
  TEST_OPTIONS = {
9
4
  base_url: 'https://mysuperapi.com/api',
@@ -66,7 +61,7 @@ describe 'AbstractApiWrapper' do
66
61
  @client = AbstractApiWrapper::Client.new(TEST_OPTIONS)
67
62
 
68
63
  stub_request(:get, TEST_OPTIONS[:base_url])
69
- .to_return(:status => 200, :body => '[]')
64
+ .to_return(status: 200, body: '[]')
70
65
 
71
66
  @request = Faraday.get(TEST_OPTIONS[:base_url])
72
67
  end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'webmock/rspec'
3
+ require 'abstract_api_wrapper'
4
+
5
+ include WebMock::API
6
+ WebMock.enable!
@@ -0,0 +1,50 @@
1
+ require 'helper'
2
+
3
+ class BasecampApi < AbstractApiWrapper
4
+ class Client < BasecampApi::Client
5
+ def method_missing(name, *params, &block)
6
+ BasecampApi::Request.new(name.to_s, self)
7
+ end
8
+ end
9
+
10
+ class Request < BasecampApi::Request
11
+ def headers
12
+ {
13
+ 'Authorization' => "Bearer #{@client.access_token}",
14
+ 'Content-Type' => 'application/json'
15
+ }
16
+ end
17
+
18
+ def endpoint
19
+ query_string = filters.map { |k, v| "#{k}=#{v}" }.join('&')
20
+ "#{@client.base_url}/#{path.join('/')}.json?#{query_string}"
21
+ end
22
+ end
23
+ end
24
+
25
+ describe 'BasecampApi' do
26
+ describe 'Request' do
27
+ subject {
28
+ client = BasecampApi::Client.new(
29
+ apiver: '1',
30
+ base_url: 'http://localhost.com',
31
+ access_token: '123'
32
+ )
33
+ }
34
+
35
+ it 'should contain a .json in the request endpoint' do
36
+ request = subject.projects.all
37
+ expect(request).to be_kind_of(BasecampApi::Request)
38
+ expect(request.endpoint).to include('projects.json')
39
+ end
40
+
41
+ it 'should use Bearer as token in Authorization header' do
42
+ stub_request(:get, 'http://localhost.com/users.json')
43
+ .with(headers: { 'Authorization' => 'Bearer 123' })
44
+ .to_return(status: 200, body: "")
45
+
46
+ response = subject.users.all.run
47
+ expect(response.request.status).to be(200)
48
+ end
49
+ end
50
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abstract_api_wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Puelpan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-07 00:00:00.000000000 Z
11
+ date: 2016-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -126,6 +126,8 @@ files:
126
126
  - lib/abstract_api_wrapper.rb
127
127
  - lib/abstract_api_wrapper/version.rb
128
128
  - spec/abstract_api_wrapper_spec.rb
129
+ - spec/helper.rb
130
+ - spec/inheritance_spec.rb
129
131
  homepage: https://github.com/octopull/abstract_api_wrapper
130
132
  licenses: []
131
133
  metadata: {}