blanket_wrapper 1.0.0 → 1.0.1

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: cff9cb20ef0765b3930bcc0b4851c512a6939c9c
4
- data.tar.gz: e91335deca8e2f7b337b91e8931f60318b7aef1c
3
+ metadata.gz: 4028f6fe83d360d8cdc4bc7af5d2c25b1c9b30f1
4
+ data.tar.gz: f94aded281f1b23a9e2f4a311dcee1ae071a23f7
5
5
  SHA512:
6
- metadata.gz: ff4050a1fb7668fb018a69c882b23aa9ab79fa0ee52ad0d8f7f0d668f8f705fb726215d9a27f4936e6477b21a80f4c5bd73601d9b3c8302fff9e72f461ed87fc
7
- data.tar.gz: c88e157f9c79c2599872baffbb5ad252b5c5b0f7de30c4a3f5e822102fc80610f5b9fa4f644cb38c56d1cbaf783077a2fbae2094de610c2f5bd1a9b7e6575376
6
+ metadata.gz: 2e2b774f5d56de52201ff07bcae76b2b85ccd1c9cbda67d7c2f7cb8de488e6e69acd35dcf45adc40f1540e1538782ac0454995c162ecddd90a1eb4fe49555efa
7
+ data.tar.gz: 56f747dd7b792f21ecc8df6bc028d802e85a3b00bc9d43d04e40c4da448b7f7fe5cf06ab1aa4adc98141c5aeaae1db1f1cdca814fb9f060539ad0f0a7dfc26a5
@@ -0,0 +1,7 @@
1
+ --protected
2
+ --no-private
3
+ --hide-tag yard.tag
4
+ --hide-tag yard.directive
5
+ --hide-tag yard.signature
6
+ --main README
7
+ --files LICENSE
@@ -31,4 +31,5 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency "pry-nav"
32
32
  spec.add_development_dependency "coveralls"
33
33
  spec.add_development_dependency "webmock"
34
+ spec.add_development_dependency "yard"
34
35
  end
@@ -1,71 +1,11 @@
1
1
  require "blanket/version"
2
2
  require "blanket/response"
3
+ require "blanket/wrapper"
3
4
  require 'httparty'
4
5
 
5
6
  module Blanket
7
+ # Wraps an API using Blanket::Wrapper
6
8
  def self.wrap(*args)
7
- Blanket.new *args
8
- end
9
-
10
- class Blanket
11
- attr_accessor :headers
12
- attr_accessor :extension
13
-
14
- def initialize(base_uri, options={})
15
- @base_uri = base_uri
16
- @uri_parts = []
17
- @headers = (options[:headers].nil?) ? {} : options[:headers]
18
- @extension = options[:extension]
19
- end
20
-
21
- # RESTful actions
22
- [:get, :post, :put, :patch, :delete].each do |action|
23
- define_method(action) do |id=nil, options={}|
24
- request(action, id, options)
25
- end
26
- end
27
-
28
- def method_missing(method, *args, &block)
29
- Blanket.new uri_from_parts([method, args.first]), {
30
- headers: @headers,
31
- extension: @extension
32
- }
33
- end
34
-
35
- private
36
-
37
- def request(method, id=nil, options={})
38
- if id.is_a? Hash
39
- options = id
40
- id = nil
41
- end
42
-
43
- headers = merged_headers(options[:headers])
44
- uri = uri_from_parts([id])
45
-
46
- if @extension
47
- uri = "#{uri}.#{extension}"
48
- end
49
-
50
- response = HTTParty.send(method, uri, {
51
- query: options[:params],
52
- headers: headers
53
- }.reject { |k, v| v.nil? || v.empty? })
54
-
55
- body = (response.respond_to? :body) ? response.body : nil
56
-
57
- (body.is_a? Array) ? body.map(Response.new) : Response.new(body)
58
- end
59
-
60
- def merged_headers(headers)
61
- headers = @headers.merge(headers != nil ? headers : {})
62
- end
63
-
64
- def uri_from_parts(parts)
65
- parts
66
- .clone
67
- .compact
68
- .inject(@base_uri.clone) { |memo, part| memo << "/#{part}" }
69
- end
9
+ Wrapper.new *args
70
10
  end
71
11
  end
@@ -3,13 +3,20 @@ require 'json'
3
3
 
4
4
  module Blanket
5
5
  class Response
6
+ # Attribute reader for the original JSON payload string
6
7
  attr_reader :payload
7
8
 
9
+ # A Blanket HTTP response wrapper.
10
+ # @param [String] json_string A string containing data in the JSON format
11
+ # @return [Blanket::Response] The wrapped Response object
8
12
  def initialize(json_string)
9
13
  json_string ||= "{}"
10
14
  @payload = payload_from_json(JSON.parse(json_string))
11
15
  end
12
16
 
17
+ private
18
+
19
+ # Allows accessing the payload's JSON keys through methods.
13
20
  def method_missing(method, *args, &block)
14
21
  if payload.respond_to? method
15
22
  payload.send method, *args, &block
@@ -18,8 +25,6 @@ module Blanket
18
25
  end
19
26
  end
20
27
 
21
- private
22
-
23
28
  def payload_from_json(json)
24
29
  if json
25
30
  parsed = [json].flatten.map do |item|
@@ -1,3 +1,4 @@
1
1
  module Blanket
2
- VERSION = "1.0.0"
2
+ # Current gem version
3
+ VERSION = "1.0.1"
3
4
  end
@@ -0,0 +1,86 @@
1
+ module Blanket
2
+ class Wrapper
3
+ class << self
4
+ private
5
+ # @macro [attach] REST action
6
+ # @method $1()
7
+ # Performs a $1 request on the wrapped URL
8
+ # @param [String, Symbol, Numeric] id The resource identifier to attach to the last part of the request
9
+ # @param [Hash] options An options hash with values for :headers, :extension and :params
10
+ # @return [Blanket::Response, Array] A wrapped Blanket::Response or an Array
11
+ def add_action(action)
12
+ define_method(action) do |id=nil, options={}|
13
+ request(action, id, options)
14
+ end
15
+ end
16
+ end
17
+
18
+ # Attribute accessor for HTTP Headers that
19
+ # should be applied to all requests
20
+ attr_accessor :headers
21
+
22
+ # Attribute accessor for file extension that
23
+ # should be appended to all requests
24
+ attr_accessor :extension
25
+
26
+ add_action :get
27
+ add_action :post
28
+ add_action :put
29
+ add_action :patch
30
+ add_action :delete
31
+
32
+ # Wraps the base URL for an API
33
+ # @param [String, Symbol] base_uri The root URL of the API you wish to wrap.
34
+ # @param [Hash] options An options hash with global values for :headers and :extension
35
+ # @return [Blanket] The Blanket object wrapping the API
36
+ def initialize(base_uri, options={})
37
+ @base_uri = base_uri
38
+ @uri_parts = []
39
+ @headers = (options[:headers].nil?) ? {} : options[:headers]
40
+ @extension = options[:extension]
41
+ end
42
+
43
+ private
44
+
45
+ def method_missing(method, *args, &block)
46
+ Wrapper.new uri_from_parts([method, args.first]), {
47
+ headers: @headers,
48
+ extension: @extension
49
+ }
50
+ end
51
+
52
+ def request(method, id=nil, options={})
53
+ if id.is_a? Hash
54
+ options = id
55
+ id = nil
56
+ end
57
+
58
+ headers = merged_headers(options[:headers])
59
+ uri = uri_from_parts([id])
60
+
61
+ if @extension
62
+ uri = "#{uri}.#{extension}"
63
+ end
64
+
65
+ response = HTTParty.send(method, uri, {
66
+ query: options[:params],
67
+ headers: headers
68
+ }.reject { |k, v| v.nil? || v.empty? })
69
+
70
+ body = (response.respond_to? :body) ? response.body : nil
71
+
72
+ (body.is_a? Array) ? body.map(Response.new) : Response.new(body)
73
+ end
74
+
75
+ def merged_headers(headers)
76
+ headers = @headers.merge(headers != nil ? headers : {})
77
+ end
78
+
79
+ def uri_from_parts(parts)
80
+ parts
81
+ .clone
82
+ .compact
83
+ .inject(@base_uri.clone) { |memo, part| memo << "/#{part}" }
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,159 @@
1
+ require 'spec_helper'
2
+ require 'blanket/wrapper'
3
+
4
+ describe "Blanket::Wrapper" do
5
+ let :api do
6
+ Blanket::wrap("http://api.example.org")
7
+ end
8
+
9
+ describe 'Making Requests' do
10
+ before :each do
11
+ allow(HTTParty).to receive(:get)
12
+ end
13
+
14
+ it 'resets after performing a request' do
15
+ api.users.get()
16
+ api.videos.get()
17
+
18
+ expect(HTTParty).to have_received(:get).with("http://api.example.org/videos", anything())
19
+ end
20
+
21
+ describe "Response" do
22
+ before :each do
23
+ stub_request(:get, "http://api.example.org/users").to_return(:body => '{"title": "Something"}')
24
+ end
25
+
26
+ let :response do
27
+ api.users.get()
28
+ end
29
+
30
+ it "returns a Blanket::Response instance" do
31
+ expect(response).to be_kind_of(Blanket::Response)
32
+ end
33
+ end
34
+
35
+ describe 'Resource identification' do
36
+ context 'When using a resource method' do
37
+ it 'allows identifying a resource' do
38
+ api.users(55).get()
39
+
40
+ expect(HTTParty).to have_received(:get).with("http://api.example.org/users/55", anything())
41
+ end
42
+
43
+ it 'only allows one identifier per resource' do
44
+ api.users(55, 37).get()
45
+
46
+ expect(HTTParty).not_to have_received(:get).with("http://api.example.org/users/55/37", anything())
47
+ end
48
+ end
49
+
50
+ context 'When using #get' do
51
+ it 'allows identifying the last resource' do
52
+ api.users(55).videos.get(15)
53
+
54
+ expect(HTTParty).to have_received(:get).with("http://api.example.org/users/55/videos/15", anything())
55
+ end
56
+ end
57
+ end
58
+
59
+ describe 'Headers' do
60
+ it 'allows sending headers in a request' do
61
+ api.users(55).get(headers: {foo: 'bar'})
62
+
63
+ expect(HTTParty).to have_received(:get).with('http://api.example.org/users/55', headers: {foo: 'bar'})
64
+ end
65
+
66
+ it 'allows setting headers globally' do
67
+ api = Blanket::wrap("http://api.example.org", headers: {token: 'my secret token'})
68
+ api.users(55).get()
69
+
70
+ expect(HTTParty).to have_received(:get).with('http://api.example.org/users/55', headers: {token: 'my secret token'})
71
+ end
72
+ end
73
+
74
+ describe 'Parameters' do
75
+ it 'allows sending parameters in a request' do
76
+ api.users(55).get(params: {foo: 'bar'})
77
+
78
+ expect(HTTParty).to have_received(:get).with('http://api.example.org/users/55', query: {foo: 'bar'})
79
+ end
80
+ end
81
+
82
+ describe 'URL Extension' do
83
+ it 'allows setting an extension for a request', :wip => true do
84
+ users_endpoint = api.users(55)
85
+ users_endpoint.extension = :json
86
+ users_endpoint.get
87
+
88
+ expect(HTTParty).to have_received(:get).with('http://api.example.org/users/55.json', anything())
89
+ end
90
+
91
+ it 'allows setting an extension globally' do
92
+ api = Blanket::wrap("http://api.example.org", extension: :xml)
93
+ api.users(55).get
94
+
95
+ expect(HTTParty).to have_received(:get).with('http://api.example.org/users/55.xml', anything())
96
+ end
97
+ end
98
+ end
99
+
100
+ describe '#get' do
101
+ before :each do
102
+ allow(HTTParty).to receive(:get)
103
+ end
104
+
105
+ it 'GETs a resource' do
106
+ api.users.get()
107
+
108
+ expect(HTTParty).to have_received(:get).with("http://api.example.org/users", anything())
109
+ end
110
+ end
111
+
112
+ describe '#post' do
113
+ before :each do
114
+ allow(HTTParty).to receive(:post)
115
+ end
116
+
117
+ it 'POSTs a resource' do
118
+ api.users.post()
119
+
120
+ expect(HTTParty).to have_received(:post).with("http://api.example.org/users", anything())
121
+ end
122
+ end
123
+
124
+ describe '#put' do
125
+ before :each do
126
+ allow(HTTParty).to receive(:put)
127
+ end
128
+
129
+ it 'PUTs a resource' do
130
+ api.users.put()
131
+
132
+ expect(HTTParty).to have_received(:put).with("http://api.example.org/users", anything())
133
+ end
134
+ end
135
+
136
+ describe '#patch' do
137
+ before :each do
138
+ allow(HTTParty).to receive(:patch)
139
+ end
140
+
141
+ it 'PATCHes a resource' do
142
+ api.users.patch()
143
+
144
+ expect(HTTParty).to have_received(:patch).with("http://api.example.org/users", anything())
145
+ end
146
+ end
147
+
148
+ describe '#delete' do
149
+ before :each do
150
+ allow(HTTParty).to receive(:delete)
151
+ end
152
+
153
+ it 'DELETEs a resource' do
154
+ api.users.delete()
155
+
156
+ expect(HTTParty).to have_received(:delete).with("http://api.example.org/users", anything())
157
+ end
158
+ end
159
+ end
@@ -1,158 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Blanket do
4
- let :api do
5
- Blanket::wrap("http://api.example.org")
6
- end
7
-
8
- describe 'Making Requests' do
9
- before :each do
10
- allow(HTTParty).to receive(:get)
11
- end
12
-
13
- it 'resets after performing a request' do
14
- api.users.get()
15
- api.videos.get()
16
-
17
- expect(HTTParty).to have_received(:get).with("http://api.example.org/videos", anything())
18
- end
19
-
20
- describe "Response" do
21
- before :each do
22
- stub_request(:get, "http://api.example.org/users").to_return(:body => '{"title": "Something"}')
23
- end
24
-
25
- let :response do
26
- api.users.get()
27
- end
28
-
29
- it "returns a Blanket::Response instance" do
30
- expect(response).to be_kind_of(Blanket::Response)
31
- end
32
- end
33
-
34
- describe 'Resource identification' do
35
- context 'When using a resource method' do
36
- it 'allows identifying a resource' do
37
- api.users(55).get()
38
-
39
- expect(HTTParty).to have_received(:get).with("http://api.example.org/users/55", anything())
40
- end
41
-
42
- it 'only allows one identifier per resource' do
43
- api.users(55, 37).get()
44
-
45
- expect(HTTParty).not_to have_received(:get).with("http://api.example.org/users/55/37", anything())
46
- end
47
- end
48
-
49
- context 'When using #get' do
50
- it 'allows identifying the last resource' do
51
- api.users(55).videos.get(15)
52
-
53
- expect(HTTParty).to have_received(:get).with("http://api.example.org/users/55/videos/15", anything())
54
- end
55
- end
56
- end
57
-
58
- describe 'Headers' do
59
- it 'allows sending headers in a request' do
60
- api.users(55).get(headers: {foo: 'bar'})
61
-
62
- expect(HTTParty).to have_received(:get).with('http://api.example.org/users/55', headers: {foo: 'bar'})
63
- end
64
-
65
- it 'allows setting headers globally' do
66
- api = Blanket::wrap("http://api.example.org", headers: {token: 'my secret token'})
67
- api.users(55).get()
68
-
69
- expect(HTTParty).to have_received(:get).with('http://api.example.org/users/55', headers: {token: 'my secret token'})
70
- end
71
- end
72
-
73
- describe 'Parameters' do
74
- it 'allows sending parameters in a request' do
75
- api.users(55).get(params: {foo: 'bar'})
76
-
77
- expect(HTTParty).to have_received(:get).with('http://api.example.org/users/55', query: {foo: 'bar'})
78
- end
79
- end
80
-
81
- describe 'URL Extension' do
82
- it 'allows setting an extension for a request', :wip => true do
83
- users_endpoint = api.users(55)
84
- users_endpoint.extension = :json
85
- users_endpoint.get
86
-
87
- expect(HTTParty).to have_received(:get).with('http://api.example.org/users/55.json', anything())
88
- end
89
-
90
- it 'allows setting an extension globally' do
91
- api = Blanket::wrap("http://api.example.org", extension: :xml)
92
- api.users(55).get
93
-
94
- expect(HTTParty).to have_received(:get).with('http://api.example.org/users/55.xml', anything())
95
- end
96
- end
97
- end
98
-
99
- describe '#get' do
100
- before :each do
101
- allow(HTTParty).to receive(:get)
102
- end
103
-
104
- it 'GETs a resource' do
105
- api.users.get()
106
-
107
- expect(HTTParty).to have_received(:get).with("http://api.example.org/users", anything())
108
- end
109
- end
110
-
111
- describe '#post' do
112
- before :each do
113
- allow(HTTParty).to receive(:post)
114
- end
115
-
116
- it 'POSTs a resource' do
117
- api.users.post()
118
-
119
- expect(HTTParty).to have_received(:post).with("http://api.example.org/users", anything())
120
- end
121
- end
122
-
123
- describe '#put' do
124
- before :each do
125
- allow(HTTParty).to receive(:put)
126
- end
127
-
128
- it 'PUTs a resource' do
129
- api.users.put()
130
-
131
- expect(HTTParty).to have_received(:put).with("http://api.example.org/users", anything())
132
- end
133
- end
134
-
135
- describe '#patch' do
136
- before :each do
137
- allow(HTTParty).to receive(:patch)
138
- end
139
-
140
- it 'PATCHes a resource' do
141
- api.users.patch()
142
-
143
- expect(HTTParty).to have_received(:patch).with("http://api.example.org/users", anything())
144
- end
145
- end
146
-
147
- describe '#delete' do
148
- before :each do
149
- allow(HTTParty).to receive(:delete)
150
- end
151
-
152
- it 'DELETEs a resource' do
153
- api.users.delete()
154
-
155
- expect(HTTParty).to have_received(:delete).with("http://api.example.org/users", anything())
4
+ describe '.wrap' do
5
+ it "creates a new Blanket::Wrapper instance" do
6
+ expect(Blanket.wrap("a_url")).to be_kind_of(Blanket::Wrapper)
156
7
  end
157
8
  end
158
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blanket_wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Abrantes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-20 00:00:00.000000000 Z
11
+ date: 2014-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -192,6 +192,20 @@ dependencies:
192
192
  - - '>='
193
193
  - !ruby/object:Gem::Version
194
194
  version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: yard
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - '>='
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - '>='
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
195
209
  description:
196
210
  email:
197
211
  - bruno@brunoabrantes.com
@@ -202,6 +216,7 @@ files:
202
216
  - .coveralls.yml
203
217
  - .gitignore
204
218
  - .travis.yml
219
+ - .yardopts
205
220
  - Gemfile
206
221
  - Guardfile
207
222
  - LICENSE
@@ -212,7 +227,9 @@ files:
212
227
  - lib/blanket.rb
213
228
  - lib/blanket/response.rb
214
229
  - lib/blanket/version.rb
230
+ - lib/blanket/wrapper.rb
215
231
  - spec/blanket/response_spec.rb
232
+ - spec/blanket/wrapper_spec.rb
216
233
  - spec/blanket_spec.rb
217
234
  - spec/spec_helper.rb
218
235
  homepage: https://github.com/inf0rmer/blanket
@@ -241,5 +258,7 @@ specification_version: 4
241
258
  summary: A dead simple API wrapper. Access your data with style.
242
259
  test_files:
243
260
  - spec/blanket/response_spec.rb
261
+ - spec/blanket/wrapper_spec.rb
244
262
  - spec/blanket_spec.rb
245
263
  - spec/spec_helper.rb
264
+ has_rdoc: