lhc 10.1.0 → 10.1.1

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
  SHA256:
3
- metadata.gz: e5027043fdf2d3bd3354777f95b1dd21438f4068170634c5788a8d40120002f6
4
- data.tar.gz: e7916e8fc0d2a55ecee4d2ceacc6372011ebdb6e809eddb25291089d7728b553
3
+ metadata.gz: a935eb47c744e10dd50006a973eff104e9716ade32ae443453aed8b198b41df8
4
+ data.tar.gz: '07126595ace009e7af40bb41da21a335eb30d0242d185af5792c2633435986d6'
5
5
  SHA512:
6
- metadata.gz: e405825ed3e030dc051b3c26d6855ff6facf9745855b07b59aa7258885135a0443c6e84d27635413d3cae53c1613b708fe37dfb140a6f1358327199238ad0b88
7
- data.tar.gz: 714d29fbd0b77416b6e2fa6a56d1189bea68311e14f8c516b84b129e7e3c0c3a45ff50eddc7e0244c3e22b878e888cb5006f7552c4ad7d8333316bfef16de1e0
6
+ metadata.gz: 8acc75ddd5efe4d61e10f5ff393c70dbe38c2a5319040b5481aa10773ff53cb017b8c04dd6a83480d29c4687e71764777266889a15e9e319f4a854f4cb177d1c
7
+ data.tar.gz: 69f88b7decaa58b3ee8137471232e66219f597c471cebf4e4ee234c7ed36ca48bedaf27f0023f09d9b0149545fc116ba3d810657bf4a598c722c6baebeb3ab75
data/lib/lhc.rb CHANGED
@@ -113,6 +113,8 @@ module LHC
113
113
  'lhc/interceptors'
114
114
  autoload :Formats,
115
115
  'lhc/formats'
116
+ autoload :Format,
117
+ 'lhc/format'
116
118
  autoload :Monitoring,
117
119
  'lhc/interceptors/monitoring'
118
120
  autoload :Request,
@@ -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
@@ -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[:format] = new
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
@@ -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
@@ -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])
@@ -1,3 +1,3 @@
1
1
  module LHC
2
- VERSION ||= '10.1.0'
2
+ VERSION ||= '10.1.1'
3
3
  end
@@ -19,8 +19,7 @@ describe LHC do
19
19
  options = {
20
20
  url: "http://datastore/v2/feedbacks",
21
21
  method: :post,
22
- body: {},
23
- headers: { 'Content-Type' => 'application/json' }
22
+ body: {}
24
23
  }
25
24
  expect { LHC.request(options) }.not_to raise_error
26
25
  end
@@ -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.0
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