lhc 10.1.0 → 10.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lhc.rb +2 -0
- data/lib/lhc/format.rb +16 -0
- data/lib/lhc/formats/json.rb +9 -3
- data/lib/lhc/formats/multipart.rb +8 -3
- data/lib/lhc/formats/plain.rb +5 -1
- data/lib/lhc/request.rb +6 -2
- data/lib/lhc/version.rb +1 -1
- data/spec/basic_methods/request_without_rails_spec.rb +1 -2
- data/spec/formats/json_spec.rb +48 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a935eb47c744e10dd50006a973eff104e9716ade32ae443453aed8b198b41df8
|
4
|
+
data.tar.gz: '07126595ace009e7af40bb41da21a335eb30d0242d185af5792c2633435986d6'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8acc75ddd5efe4d61e10f5ff393c70dbe38c2a5319040b5481aa10773ff53cb017b8c04dd6a83480d29c4687e71764777266889a15e9e319f4a854f4cb177d1c
|
7
|
+
data.tar.gz: 69f88b7decaa58b3ee8137471232e66219f597c471cebf4e4ee234c7ed36ca48bedaf27f0023f09d9b0149545fc116ba3d810657bf4a598c722c6baebeb3ab75
|
data/lib/lhc.rb
CHANGED
data/lib/lhc/format.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
class LHC::Format
|
2
|
+
|
3
|
+
private
|
4
|
+
|
5
|
+
def no_content_type_header!(options)
|
6
|
+
return if (options[:headers].keys & [:'Content-Type', 'Content-Type']).blank?
|
7
|
+
|
8
|
+
raise 'Content-Type header is not allowed for formatted requests!'
|
9
|
+
end
|
10
|
+
|
11
|
+
def no_accept_header!(options)
|
12
|
+
return if (options[:headers].keys & [:Accept, 'Accept']).blank?
|
13
|
+
|
14
|
+
raise 'Accept header is not allowed for formatted requests!'
|
15
|
+
end
|
16
|
+
end
|
data/lib/lhc/formats/json.rb
CHANGED
@@ -1,13 +1,19 @@
|
|
1
1
|
module LHC::Formats
|
2
|
-
class JSON
|
2
|
+
class JSON < LHC::Format
|
3
3
|
include LHC::BasicMethodsConcern
|
4
4
|
|
5
5
|
def self.request(options)
|
6
|
+
options[:format] = new
|
7
|
+
super(options)
|
8
|
+
end
|
9
|
+
|
10
|
+
def format_options(options)
|
6
11
|
options[:headers] ||= {}
|
12
|
+
no_content_type_header!(options)
|
7
13
|
options[:headers]['Content-Type'] = 'application/json; charset=utf-8'
|
14
|
+
no_accept_header!(options)
|
8
15
|
options[:headers]['Accept'] = 'application/json; charset=utf-8'
|
9
|
-
options
|
10
|
-
super(options)
|
16
|
+
options
|
11
17
|
end
|
12
18
|
|
13
19
|
def as_json(input)
|
@@ -1,14 +1,19 @@
|
|
1
1
|
module LHC::Formats
|
2
|
-
class Multipart
|
2
|
+
class Multipart < LHC::Format
|
3
3
|
include LHC::BasicMethodsConcern
|
4
4
|
|
5
5
|
def self.request(options)
|
6
|
-
options[:headers] ||= {}
|
7
|
-
options[:headers]['Content-Type'] = 'multipart/form-data'
|
8
6
|
options[:format] = new
|
9
7
|
super(options)
|
10
8
|
end
|
11
9
|
|
10
|
+
def format_options(options)
|
11
|
+
options[:headers] ||= {}
|
12
|
+
no_content_type_header!(options)
|
13
|
+
options[:headers]['Content-Type'] = 'multipart/form-data'
|
14
|
+
options
|
15
|
+
end
|
16
|
+
|
12
17
|
def as_json(input)
|
13
18
|
parse(input)
|
14
19
|
end
|
data/lib/lhc/formats/plain.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module LHC::Formats
|
2
|
-
class Plain
|
2
|
+
class Plain < LHC::Format
|
3
3
|
include LHC::BasicMethodsConcern
|
4
4
|
|
5
5
|
def self.request(options)
|
@@ -7,6 +7,10 @@ module LHC::Formats
|
|
7
7
|
super(options)
|
8
8
|
end
|
9
9
|
|
10
|
+
def format_options(options)
|
11
|
+
options
|
12
|
+
end
|
13
|
+
|
10
14
|
def as_json(input)
|
11
15
|
parse(input)
|
12
16
|
end
|
data/lib/lhc/request.rb
CHANGED
@@ -14,9 +14,8 @@ class LHC::Request
|
|
14
14
|
|
15
15
|
def initialize(options, self_executing = true)
|
16
16
|
self.errors_ignored = options.fetch(:ignored_errors, [])
|
17
|
-
self.options = options.deep_dup || {}
|
17
|
+
self.options = format!(options.deep_dup || {})
|
18
18
|
self.error_handler = options.delete :error_handler
|
19
|
-
self.format = options.delete(:format) || LHC::Formats::JSON.new
|
20
19
|
use_configured_endpoint!
|
21
20
|
generate_url_from_template!
|
22
21
|
self.interceptors = LHC::Interceptors.new(self)
|
@@ -54,6 +53,11 @@ class LHC::Request
|
|
54
53
|
|
55
54
|
attr_accessor :interceptors
|
56
55
|
|
56
|
+
def format!(options)
|
57
|
+
self.format = options.delete(:format) || LHC::Formats::JSON.new
|
58
|
+
format.format_options(options)
|
59
|
+
end
|
60
|
+
|
57
61
|
def optionally_encoded_url(options)
|
58
62
|
return options[:url] unless options.fetch(:url_encoding, true)
|
59
63
|
encode_url(options[:url])
|
data/lib/lhc/version.rb
CHANGED
data/spec/formats/json_spec.rb
CHANGED
@@ -8,5 +8,53 @@ describe LHC do
|
|
8
8
|
.to_return(body: {}.to_json)
|
9
9
|
LHC.json.get('http://local.ch')
|
10
10
|
end
|
11
|
+
|
12
|
+
context 'header key as symbol' do
|
13
|
+
it 'raises an error when trying to set content-type header even though the format is used' do
|
14
|
+
expect(lambda {
|
15
|
+
LHC.post(
|
16
|
+
'http://local.ch',
|
17
|
+
headers: {
|
18
|
+
'Content-Type': 'multipart/form-data'
|
19
|
+
}
|
20
|
+
)
|
21
|
+
}).to raise_error 'Content-Type header is not allowed for formatted requests!'
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'raises an error when trying to set accept header even though the format is used' do
|
25
|
+
expect(lambda {
|
26
|
+
LHC.post(
|
27
|
+
'http://local.ch',
|
28
|
+
headers: {
|
29
|
+
'Accept': 'multipart/form-data'
|
30
|
+
}
|
31
|
+
)
|
32
|
+
}).to raise_error 'Accept header is not allowed for formatted requests!'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'header key as string' do
|
37
|
+
it 'raises an error when trying to set content-type header even though the format is used' do
|
38
|
+
expect(lambda {
|
39
|
+
LHC.post(
|
40
|
+
'http://local.ch',
|
41
|
+
headers: {
|
42
|
+
'Content-Type' => 'multipart/form-data'
|
43
|
+
}
|
44
|
+
)
|
45
|
+
}).to raise_error 'Content-Type header is not allowed for formatted requests!'
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'raises an error when trying to set accept header even though the format is used' do
|
49
|
+
expect(lambda {
|
50
|
+
LHC.post(
|
51
|
+
'http://local.ch',
|
52
|
+
headers: {
|
53
|
+
'Accept' => 'multipart/form-data'
|
54
|
+
}
|
55
|
+
)
|
56
|
+
}).to raise_error 'Accept header is not allowed for formatted requests!'
|
57
|
+
end
|
58
|
+
end
|
11
59
|
end
|
12
60
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lhc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 10.1.
|
4
|
+
version: 10.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- https://github.com/local-ch/lhc/contributors
|
@@ -205,6 +205,7 @@ files:
|
|
205
205
|
- lib/lhc/errors/server_error.rb
|
206
206
|
- lib/lhc/errors/timeout.rb
|
207
207
|
- lib/lhc/errors/unknown_error.rb
|
208
|
+
- lib/lhc/format.rb
|
208
209
|
- lib/lhc/formats.rb
|
209
210
|
- lib/lhc/formats/json.rb
|
210
211
|
- lib/lhc/formats/multipart.rb
|