blanket_wrapper 2.0.1 → 3.0.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: 8252184153a5c8734c3512c6ff6eea2e5a89d077
4
- data.tar.gz: c8562c25b4f679bbc6a2dda18dab57cf5e340d29
3
+ metadata.gz: 33d01b1274e6100810a9328ec030fe9bf0d1cf91
4
+ data.tar.gz: cb1e89db5d84825ca1632cf62b5fd415c862267e
5
5
  SHA512:
6
- metadata.gz: 6a2729a2ed218ccf1302a3202401f503ee9609d30424e035b52b9714f662634bff0781d29d70e43d1ecebf4fa60026f197f0793099054442cdb14d4c425c1ddb
7
- data.tar.gz: a39dedce0183fcf27d055944eb4e6d3276677c3a79c40df1766ff7e55274e10c853877bcb388ac26858eb3615d8a58eab7ac5e6407103994d25ab0bb394bb7df
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 `method_missingp`, you can `send` slashed uri parts to the wrapper and it will play nicely. This is especially usefull when APIs give you URLs:
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 an array of `Blanket::Response`.
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
@@ -28,7 +28,11 @@ module Blanket
28
28
  end
29
29
 
30
30
  def payload_from_json(json)
31
- [json].flatten.map { |item| RecursiveOpenStruct.new item, recurse_over_arrays: true }
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
@@ -0,0 +1,5 @@
1
+ module Blanket
2
+ def self.stringify_keys(hash)
3
+ hash.inject({}) {|memo,(k,v)| memo[k.to_s] = v; memo}
4
+ end
5
+ end
@@ -1,4 +1,4 @@
1
1
  module Blanket
2
2
  # Current gem version
3
- VERSION = "2.0.1"
3
+ VERSION = "3.0.0"
4
4
  end
@@ -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.first.title).to eq("Something")
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.first.desc.someKey).to eq("someValue")
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.first.main_item.values[0].quantity).to eq(1)
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: 'bar'})
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: 'my secret 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: 2.0.1
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-02-02 00:00:00.000000000 Z
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