blanket_wrapper 2.0.1 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -2
- data/lib/blanket/response.rb +5 -1
- data/lib/blanket/utils.rb +5 -0
- data/lib/blanket/version.rb +1 -1
- data/lib/blanket/wrapper.rb +3 -1
- data/spec/blanket/response_spec.rb +3 -3
- data/spec/blanket/wrapper_spec.rb +2 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33d01b1274e6100810a9328ec030fe9bf0d1cf91
|
4
|
+
data.tar.gz: cb1e89db5d84825ca1632cf62b5fd415c862267e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c97bf1461412fe1bd551f14249abe336f1ef56bd6d81258ca65d55d72673ea9ebf0c1415de9e8fde6cce547765fe317ca726ea3bb96584302899fd2bb773194f
|
7
|
+
data.tar.gz: e65d51c81612486e8edc269c0f562377efd2976c0f67cb59b3cc8ffa530186b6a3c29a728cf6f57c7e6d8b577bcc9f2e2556b695d39400b34a97bf4a247b80fc
|
data/README.md
CHANGED
@@ -85,7 +85,7 @@ github.users('inf0rmer').repos
|
|
85
85
|
|
86
86
|
The final `get` method performs a GET HTTP request. You can also use it to append a final part to your request, so you can write something like:
|
87
87
|
|
88
|
-
As this magic works using `
|
88
|
+
As this magic works using `method_missing`, you can `send` slashed uri parts to the wrapper and it will play nicely. This is especially usefull when APIs give you URLs:
|
89
89
|
```ruby
|
90
90
|
github.get('users/inf0rmer/repos')
|
91
91
|
# or, if you don't wnat to perform the request yet, or have to append more parts to the uri
|
@@ -99,7 +99,25 @@ github.users.get('inf0rmer')
|
|
99
99
|
```
|
100
100
|
|
101
101
|
### Responses
|
102
|
-
At the moment Blanket only accepts JSON responses. Every request returns
|
102
|
+
At the moment Blanket only accepts JSON responses. Every request returns a `Blanket::Response` instance, which parses the JSON internally and lets you access keys using dot syntax:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
user = github.users('inf0rmer').get
|
106
|
+
|
107
|
+
user.login
|
108
|
+
# => "inf0rmer"
|
109
|
+
|
110
|
+
user.url
|
111
|
+
# => "https://api.github.com/users/inf0rmer"
|
112
|
+
|
113
|
+
# It even works on nested keys
|
114
|
+
repo = github.repos('inf0rmer').get('blanket')
|
115
|
+
|
116
|
+
repo.owner.login
|
117
|
+
# => "inf0rmer"
|
118
|
+
```
|
119
|
+
|
120
|
+
If the response is an array, all `Enumerable` methods work as expected:
|
103
121
|
|
104
122
|
```ruby
|
105
123
|
repos = github.users('inf0rmer').repos.get
|
data/lib/blanket/response.rb
CHANGED
@@ -28,7 +28,11 @@ module Blanket
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def payload_from_json(json)
|
31
|
-
[json].flatten.map
|
31
|
+
parsed = [json].flatten.map do |item|
|
32
|
+
RecursiveOpenStruct.new item, recurse_over_arrays: true
|
33
|
+
end
|
34
|
+
|
35
|
+
(parsed.count == 1) ? parsed.first : parsed
|
32
36
|
end
|
33
37
|
|
34
38
|
end
|
data/lib/blanket/version.rb
CHANGED
data/lib/blanket/wrapper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require_relative "utils"
|
2
|
+
|
1
3
|
module Blanket
|
2
4
|
class Wrapper
|
3
5
|
class << self
|
@@ -61,7 +63,7 @@ module Blanket
|
|
61
63
|
id = nil
|
62
64
|
end
|
63
65
|
|
64
|
-
headers = merged_headers(options[:headers])
|
66
|
+
headers = Blanket.stringify_keys merged_headers(options[:headers])
|
65
67
|
params = merged_params(options[:params])
|
66
68
|
uri = uri_from_parts([id])
|
67
69
|
|
@@ -9,15 +9,15 @@ describe "Blanket::Response" do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it "can access a surface property from a json string as a method" do
|
12
|
-
expect(response.
|
12
|
+
expect(response.title).to eq("Something")
|
13
13
|
end
|
14
14
|
|
15
15
|
it "can access a deep property from a json string as a method" do
|
16
|
-
expect(response.
|
16
|
+
expect(response.desc.someKey).to eq("someValue")
|
17
17
|
end
|
18
18
|
|
19
19
|
it "can access a deep property in an array from a json string as a method" do
|
20
|
-
expect(response.
|
20
|
+
expect(response.main_item.values[0].quantity).to eq(1)
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -101,14 +101,14 @@ describe "Blanket::Wrapper" do
|
|
101
101
|
it 'allows sending headers in a request' do
|
102
102
|
api.users(55).get(headers: {foo: 'bar'})
|
103
103
|
|
104
|
-
expect(HTTParty).to have_received(:get).with('http://api.example.org/users/55', headers: {foo
|
104
|
+
expect(HTTParty).to have_received(:get).with('http://api.example.org/users/55', headers: {"foo" => "bar"})
|
105
105
|
end
|
106
106
|
|
107
107
|
it 'allows setting headers globally' do
|
108
108
|
api = Blanket::wrap("http://api.example.org", headers: {token: 'my secret token'})
|
109
109
|
api.users(55).get()
|
110
110
|
|
111
|
-
expect(HTTParty).to have_received(:get).with('http://api.example.org/users/55', headers: {token
|
111
|
+
expect(HTTParty).to have_received(:get).with('http://api.example.org/users/55', headers: {"token" => "my secret token"})
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
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:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bruno Abrantes
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-04-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -226,6 +226,7 @@ files:
|
|
226
226
|
- lib/blanket.rb
|
227
227
|
- lib/blanket/exception.rb
|
228
228
|
- lib/blanket/response.rb
|
229
|
+
- lib/blanket/utils.rb
|
229
230
|
- lib/blanket/version.rb
|
230
231
|
- lib/blanket/wrapper.rb
|
231
232
|
- spec/blanket/exception_spec.rb
|