lhc 10.0.2 → 10.1.0

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
  SHA256:
3
- metadata.gz: fcff9d45e512c5c1a8c76aaffb710eb73d5a5b03909e167e5a584fc4614a6d19
4
- data.tar.gz: 366dd2f2b123018ca3d06d21d2e3eafd75923a292f363a54e1c8163f78753286
3
+ metadata.gz: e5027043fdf2d3bd3354777f95b1dd21438f4068170634c5788a8d40120002f6
4
+ data.tar.gz: e7916e8fc0d2a55ecee4d2ceacc6372011ebdb6e809eddb25291089d7728b553
5
5
  SHA512:
6
- metadata.gz: 9f57d6a94e86a60ec2866aefdf15f472353e270e0a8df7f88bbbded053f2d84dd59e7e3ff9027b9f629aa7a355e905a407dc41c7727996c53b35c6e76ebc09dc
7
- data.tar.gz: d94e275d67dad755be1a0ce290eca7ff7a1defeaadf30ea202d6659eca9a14d23e14fc220b8992d4087278e806f851b36b0be28aa7b71873b6266f4dde6658c3
6
+ metadata.gz: e405825ed3e030dc051b3c26d6855ff6facf9745855b07b59aa7258885135a0443c6e84d27635413d3cae53c1613b708fe37dfb140a6f1358327199238ad0b88
7
+ data.tar.gz: 714d29fbd0b77416b6e2fa6a56d1189bea68311e14f8c516b84b129e7e3c0c3a45ff50eddc7e0244c3e22b878e888cb5006f7552c4ad7d8333316bfef16de1e0
data/README.md CHANGED
@@ -36,6 +36,9 @@ use it like:
36
36
  * [Basic methods](#basic-methods)
37
37
  * [Request](#request)
38
38
  * [Formats](#formats)
39
+ * [Default format](#default-format)
40
+ * [Unformatted requests](#unformatted-requests)
41
+ * [Upload with LHC](#upload-with-lhc)
39
42
  * [Parallel requests](#parallel-requests)
40
43
  * [Follow redirects](#follow-redirects)
41
44
  * [Transfer data through the request body](#transfer-data-through-the-request-body)
@@ -123,7 +126,7 @@ You can use any of the basic methods in combination with a format like `json`:
123
126
  LHC.json.get(options)
124
127
  ```
125
128
 
126
- Currently supported formats: `json`
129
+ Currently supported formats: `json`, `multipart`, `plain` (for no formatting)
127
130
 
128
131
  If formats are used, headers for `Content-Type` and `Accept` are set by LHC, but also http bodies are translated by LHC, so you can pass bodies as ruby objects:
129
132
 
@@ -134,6 +137,29 @@ LHC.json.post('http://slack', body: { text: 'Hi there' })
134
137
  # Translates body to "{\"text\":\"Hi there\"}" before sending
135
138
  ```
136
139
 
140
+ #### Default format
141
+
142
+ If you use LHC's basic methods `LHC.get`, `LHC.post` etc. without any explicit format, `JSON` will be chosen as the default format.
143
+
144
+ #### Unformatted requests
145
+
146
+ In case you need to send requests without LHC formatting headers or the body, use `plain`:
147
+
148
+ ```ruby
149
+ LHC.plain.post('http://endpoint', body: { weird: 'format%s2xX' })
150
+ ```
151
+
152
+ ##### Upload with LHC
153
+
154
+ If you want to upload data with LHC, it's recommended to use the `multipart` format:
155
+
156
+ ```ruby
157
+ response = LHC.multipart.post('http://upload', body: { file })
158
+ response.headers['Location']
159
+ # Content-Type: multipart/form-data
160
+ # Leaves body unformatted
161
+ ```
162
+
137
163
  ### Parallel requests
138
164
 
139
165
  If you pass an array of requests to `LHC.request`, it will perform those requests in parallel.
@@ -6,6 +6,14 @@ module LHC
6
6
  def json
7
7
  LHC::Formats::JSON
8
8
  end
9
+
10
+ def multipart
11
+ LHC::Formats::Multipart
12
+ end
13
+
14
+ def plain
15
+ LHC::Formats::Plain
16
+ end
9
17
  end
10
18
  end
11
19
  end
@@ -1,3 +1,5 @@
1
1
  module LHC::Formats
2
2
  autoload :JSON, 'lhc/formats/json'
3
+ autoload :Multipart, 'lhc/formats/multipart'
4
+ autoload :Plain, 'lhc/formats/plain'
3
5
  end
@@ -0,0 +1,38 @@
1
+ module LHC::Formats
2
+ class Multipart
3
+ include LHC::BasicMethodsConcern
4
+
5
+ def self.request(options)
6
+ options[:headers] ||= {}
7
+ options[:headers]['Content-Type'] = 'multipart/form-data'
8
+ options[:format] = new
9
+ super(options)
10
+ end
11
+
12
+ def as_json(input)
13
+ parse(input)
14
+ end
15
+
16
+ def as_open_struct(input)
17
+ parse(input)
18
+ end
19
+
20
+ def to_body(input)
21
+ input
22
+ end
23
+
24
+ def to_s
25
+ 'multipart'
26
+ end
27
+
28
+ def to_sym
29
+ to_s.to_sym
30
+ end
31
+
32
+ private
33
+
34
+ def parse(input)
35
+ input
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,36 @@
1
+ module LHC::Formats
2
+ class Plain
3
+ include LHC::BasicMethodsConcern
4
+
5
+ def self.request(options)
6
+ options[:format] = new
7
+ super(options)
8
+ end
9
+
10
+ def as_json(input)
11
+ parse(input)
12
+ end
13
+
14
+ def as_open_struct(input)
15
+ parse(input)
16
+ end
17
+
18
+ def to_body(input)
19
+ input
20
+ end
21
+
22
+ def to_s
23
+ 'plain'
24
+ end
25
+
26
+ def to_sym
27
+ to_s.to_sym
28
+ end
29
+
30
+ private
31
+
32
+ def parse(input)
33
+ input
34
+ end
35
+ end
36
+ end
@@ -16,7 +16,7 @@ class LHC::Request
16
16
  self.errors_ignored = options.fetch(:ignored_errors, [])
17
17
  self.options = options.deep_dup || {}
18
18
  self.error_handler = options.delete :error_handler
19
- self.format = options.delete('format') || LHC::Formats::JSON.new
19
+ self.format = options.delete(:format) || LHC::Formats::JSON.new
20
20
  use_configured_endpoint!
21
21
  generate_url_from_template!
22
22
  self.interceptors = LHC::Interceptors.new(self)
@@ -1,3 +1,3 @@
1
1
  module LHC
2
- VERSION ||= '10.0.2'
2
+ VERSION ||= '10.1.0'
3
3
  end
@@ -0,0 +1,29 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHC do
4
+ context 'multipart' do
5
+ let(:file) do
6
+ ActionDispatch::Http::UploadedFile.new(
7
+ tempfile: Tempfile.new,
8
+ filename: 'image.jpg',
9
+ type: 'image/jpeg',
10
+ head: %q{Content-Disposition: form-data; name="files[]"; filename="image.jpg"\r\nContent-Type: image/jpeg\r\n}
11
+ )
12
+ end
13
+ let(:body) { { size: 2231 }.to_json }
14
+ let(:location) { 'http://local.ch/uploads/image.jpg' }
15
+
16
+ it 'leaves plains requests unformatted' do
17
+ stub_request(:post, 'http://local.ch/') do |request|
18
+ raise 'Content-Type header wrong' unless request.headers['Content-Type'] == 'multipart/form-data'
19
+ raise 'Body wrongly formatted' unless request.body.match(/file=%23%3CActionDispatch%3A%3AHttp%3A%3AUploadedFile%3A.*%3E&type=Image/)
20
+ end.to_return(status: 200, body: body, headers: { 'Location' => location })
21
+ response = LHC.multipart.post(
22
+ 'http://local.ch',
23
+ body: { file: file, type: 'Image' }
24
+ )
25
+ expect(response.body).to eq body
26
+ expect(response.headers['Location']).to eq location
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,32 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHC do
4
+ context 'plain' do
5
+ let(:file) do
6
+ ActionDispatch::Http::UploadedFile.new(
7
+ tempfile: Tempfile.new,
8
+ filename: 'image.jpg',
9
+ type: 'image/jpeg',
10
+ head: %q{Content-Disposition: form-data; name="files[]"; filename="image.jpg"\r\nContent-Type: image/jpeg\r\n}
11
+ )
12
+ end
13
+
14
+ it 'leaves plains requests unformatted' do
15
+ stub_request(:post, 'http://local.ch/')
16
+ .with(body: /file=%23%3CActionDispatch%3A%3AHttp%3A%3AUploadedFile%3A.*%3E&type=Image/)
17
+ .to_return do |request|
18
+ expect(request.headers['Content-Type']).to be_blank
19
+
20
+ { status: 204 }
21
+ end
22
+ response = LHC.plain.post(
23
+ 'http://local.ch',
24
+ body: { file: file, type: 'Image' }
25
+ )
26
+ expect(lambda {
27
+ response.body
28
+ response.data
29
+ }).not_to raise_error
30
+ end
31
+ end
32
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhc
3
3
  version: !ruby/object:Gem::Version
4
- version: 10.0.2
4
+ version: 10.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhc/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-15 00:00:00.000000000 Z
11
+ date: 2018-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -207,6 +207,8 @@ files:
207
207
  - lib/lhc/errors/unknown_error.rb
208
208
  - lib/lhc/formats.rb
209
209
  - lib/lhc/formats/json.rb
210
+ - lib/lhc/formats/multipart.rb
211
+ - lib/lhc/formats/plain.rb
210
212
  - lib/lhc/interceptor.rb
211
213
  - lib/lhc/interceptors.rb
212
214
  - lib/lhc/interceptors/auth.rb
@@ -287,6 +289,8 @@ files:
287
289
  - spec/error/timeout_spec.rb
288
290
  - spec/error/to_s_spec.rb
289
291
  - spec/formats/json_spec.rb
292
+ - spec/formats/multipart_spec.rb
293
+ - spec/formats/plain_spec.rb
290
294
  - spec/interceptors/after_request_spec.rb
291
295
  - spec/interceptors/after_response_spec.rb
292
296
  - spec/interceptors/auth/basic_auth_spec.rb
@@ -430,6 +434,8 @@ test_files:
430
434
  - spec/error/timeout_spec.rb
431
435
  - spec/error/to_s_spec.rb
432
436
  - spec/formats/json_spec.rb
437
+ - spec/formats/multipart_spec.rb
438
+ - spec/formats/plain_spec.rb
433
439
  - spec/interceptors/after_request_spec.rb
434
440
  - spec/interceptors/after_response_spec.rb
435
441
  - spec/interceptors/auth/basic_auth_spec.rb