bootic_client 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e814afcd60c951637016f9a3435104816be4420
4
- data.tar.gz: 37287bbd0a9d96c8fbd9bd5f05be64558ab125a9
3
+ metadata.gz: 7bd5c38b36abf80f522f33986103d5d2d94c2ba0
4
+ data.tar.gz: 5f11c6edb651d08e49cbbf1e8bd2a89037cfd3e8
5
5
  SHA512:
6
- metadata.gz: 696f4e4e34eb96ae1160c8b13520080ea99d79a75bef73280714b16978a84bfc119177025f4daebd5828c4f2e00afeb4d7483a0cde4b68a6e9366d16e23253f2
7
- data.tar.gz: bc751c6bbb19f08012f38770c9365af27fdbd61bb1278b35be81e8340c751f024c8f94f892642327fd9bd87aca6150a3f572027c1039d1518b3292e3f50f5b4a
6
+ metadata.gz: 8afbb75506aa39f78af563d6aa7b926f4fe9993554f9a79288d606af3bf11002423bc9bcfd1a66d633006ee37c8b69a3bb9987d1551d12b41f6c14941f7b77cc
7
+ data.tar.gz: fda4d8271434e14c7502329f8b3448f95a570628fed0ac6afb8dd6b380927a95fc4a39b26a77d77ae8d95c33b01a32523af784798c5e37507c03ccc1d7daa04d
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Change Log
2
2
 
3
+ ## [v0.0.13](https://github.com/bootic/bootic_client.rb/tree/v0.0.13) (2016-01-12)
4
+ [Full Changelog](https://github.com/bootic/bootic_client.rb/compare/v0.0.12...v0.0.13)
5
+
6
+ **Merged pull requests:**
7
+
8
+ - Faraday HTTP Cache expects memcache store must respond to \#delete [\#5](https://github.com/bootic/bootic_client.rb/pull/5) ([ismasan](https://github.com/ismasan))
9
+
3
10
  ## [v0.0.12](https://github.com/bootic/bootic_client.rb/tree/v0.0.12) (2016-01-11)
4
11
  [Full Changelog](https://github.com/bootic/bootic_client.rb/compare/v0.0.11...v0.0.12)
5
12
 
data/README.md CHANGED
@@ -165,6 +165,17 @@ if shop.can?(:create_product)
165
165
  end
166
166
  ```
167
167
 
168
+ ### Working with Files and IO instances
169
+
170
+ Instances of `File` and other readable `IO` objects will be base64-encoded internally before JSON-encoding payloads for `POST`, `PUT` and `PATCH` requests.
171
+
172
+ ```ruby
173
+ asset = product.create_product_asset(
174
+ filename: 'foo.jpg',
175
+ data: File.new('/path/to/foo.jpg') # this will base64-encode the file data in the `data` field.
176
+ )
177
+ ```
178
+
168
179
  ## Relation docs
169
180
 
170
181
  All resource link relations include a "docs" URL so you can learn more about that particular resource.
@@ -187,7 +198,7 @@ BooticClient.configure do |c|
187
198
  end
188
199
  ```
189
200
 
190
- Outside of Rails, BooticClient ships with a wrapper around the [Dalli](https://github.com/mperham/dalli) memcache client.
201
+ Outside of Rails, BooticClient ships with a wrapper around the [Dalli](https://github.com/mperham/dalli) memcache client.
191
202
  You must include Dalli in your Gemfile and require the wrapper explicitely.
192
203
 
193
204
  ```ruby
@@ -1,3 +1,4 @@
1
+ require 'base64'
1
2
  require 'faraday'
2
3
  require 'faraday_middleware'
3
4
  require 'faraday-http-cache'
@@ -33,21 +34,21 @@ module BooticClient
33
34
  def post(href, payload = {}, headers = {})
34
35
  validated_request!(:post, href) do |req|
35
36
  req.headers.update headers
36
- req.body = JSON.dump(payload)
37
+ req.body = JSON.dump(sanitized(payload))
37
38
  end
38
39
  end
39
40
 
40
41
  def put(href, payload = {}, headers = {})
41
42
  validated_request!(:put, href) do |req|
42
43
  req.headers.update headers
43
- req.body = JSON.dump(payload)
44
+ req.body = JSON.dump(sanitized(payload))
44
45
  end
45
46
  end
46
47
 
47
48
  def patch(href, payload = {}, headers = {})
48
49
  validated_request!(:patch, href) do |req|
49
50
  req.headers.update headers
50
- req.body = JSON.dump(payload)
51
+ req.body = JSON.dump(sanitized(payload))
51
52
  end
52
53
  end
53
54
 
@@ -97,6 +98,20 @@ module BooticClient
97
98
  raise UnauthorizedError, "Unauthorized request" if resp.status == 401
98
99
  raise AccessForbiddenError, "Access Forbidden" if resp.status == 403
99
100
  end
101
+
102
+ def sanitized(payload)
103
+ return payload unless payload.kind_of?(Hash)
104
+ payload.each_with_object({}) do |(k, v), memo|
105
+ memo[k] = case v
106
+ when IO
107
+ Base64.encode64 v.read
108
+ when Hash
109
+ sanitized v
110
+ else
111
+ v
112
+ end
113
+ end
114
+ end
100
115
  end
101
116
 
102
117
  end
@@ -1,3 +1,3 @@
1
1
  module BooticClient
2
- VERSION = "0.0.13"
2
+ VERSION = "0.0.14"
3
3
  end
data/spec/client_spec.rb CHANGED
@@ -4,6 +4,10 @@ require 'json'
4
4
  describe BooticClient::Client do
5
5
  require 'webmock/rspec'
6
6
 
7
+ def fixture_path(filename)
8
+ File.join File.dirname(File.expand_path(__FILE__)), 'fixtures', filename
9
+ end
10
+
7
11
  describe 'valid response' do
8
12
  let(:root_url) { 'https://api.bootic.net/v1' }
9
13
  let(:client) { BooticClient::Client.new }
@@ -187,6 +191,21 @@ describe BooticClient::Client do
187
191
  end
188
192
  end
189
193
 
194
+ describe 'with file data' do
195
+ let(:base64_data) { Base64.encode64(File.read(fixture_path('file.gif'))) }
196
+ let(:file) { File.new(fixture_path('file.gif')) }
197
+
198
+ before do
199
+ stub_request(:post, root_url)
200
+ .with(body: JSON.dump({foo: 'bar', data: base64_data}), headers: request_headers)
201
+ .to_return(status: 201, body: JSON.dump(root_data), headers: response_headers)
202
+ end
203
+
204
+ it 'POSTs request with base64-encoded file and parses response' do
205
+ expect(client.post(root_url, {foo: 'bar', data: file}, request_headers).body['message']).to eql('Hello!')
206
+ end
207
+ end
208
+
190
209
  [:put, :patch].each do |verb|
191
210
  describe verb.to_s.upcase do
192
211
  before do
@@ -199,6 +218,21 @@ describe BooticClient::Client do
199
218
  expect(client.send(verb, root_url, {foo: 'bar'}, request_headers).body['message']).to eql('Hello!')
200
219
  end
201
220
  end
221
+
222
+ describe "#{verb.to_s.upcase} with file data" do
223
+ let(:base64_data) { Base64.encode64(File.read(fixture_path('file.gif'))) }
224
+ let(:file) { File.new(fixture_path('file.gif')) }
225
+
226
+ before do
227
+ stub_request(verb, root_url)
228
+ .with(body: JSON.dump({foo: 'bar', data: {name: 'la', file: base64_data}}), headers: request_headers)
229
+ .to_return(status: 200, body: JSON.dump(root_data), headers: response_headers)
230
+ end
231
+
232
+ it "#{verb.to_s.upcase}s request with base64-encoded file data and parses response" do
233
+ expect(client.send(verb, root_url, {foo: 'bar', data: {name: 'la', file: file}}, request_headers).body['message']).to eql('Hello!')
234
+ end
235
+ end
202
236
  end
203
237
 
204
238
  context 'DELETE' do
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bootic_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismael Celis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-12 00:00:00.000000000 Z
11
+ date: 2016-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -198,6 +198,7 @@ files:
198
198
  - spec/client_credentials_strategy_spec.rb
199
199
  - spec/client_spec.rb
200
200
  - spec/entity_spec.rb
201
+ - spec/fixtures/file.gif
201
202
  - spec/memcache_storage_spec.rb
202
203
  - spec/relation_spec.rb
203
204
  - spec/spec_helper.rb
@@ -232,6 +233,7 @@ test_files:
232
233
  - spec/client_credentials_strategy_spec.rb
233
234
  - spec/client_spec.rb
234
235
  - spec/entity_spec.rb
236
+ - spec/fixtures/file.gif
235
237
  - spec/memcache_storage_spec.rb
236
238
  - spec/relation_spec.rb
237
239
  - spec/spec_helper.rb