lhc 3.5.5 → 3.6.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: 5bd96d437b05df955ff37aed6c1bea4c6c3a68ff
4
- data.tar.gz: 6fa74559274820bf6cecca05fc6deb4620483529
3
+ metadata.gz: 115f947aab614d63771827eac4b14455c0233baa
4
+ data.tar.gz: a44cfd0e91469c137db12ee78061fa935b61ccec
5
5
  SHA512:
6
- metadata.gz: a46627d6d00ea9193860f8cab7e8beaace7c514992cccc803baa36ed050112667445c7cd683f9c95e1d19703dc2e0fd47feb969d09e1d770b9c5bcd4be7b9bb0
7
- data.tar.gz: 1ea0e5bd2384dc033d38f93a20227160de7c6512b4ff9322c3f2800237ce9db354ea88489ee761938580295ddceab2e70a325852f4c5649670fdfd2fc71b958d
6
+ metadata.gz: cd8e5c6561008f93d3ea4f90a078152512097e769b664dc292a8274d8a988fc63a379d23b7b6ccc0910810e0d406799959d1594023bbcc68c6de5233e30c51dd
7
+ data.tar.gz: 7d6826fee6f4e55535017380e84318cb351356167faafbc9fdf31a29640be2344515ee8dbbb9bfbde71e9278252ffe4da22d7555128d02a4520d0b44647188a3
data/README.md CHANGED
@@ -110,6 +110,20 @@ Anything but a response code indicating success (2**) throws an exception.
110
110
 
111
111
  → [Read more about exceptions](docs/exceptions.md)
112
112
 
113
+ ## Custom error handling
114
+
115
+ You can provide custom error handlers to handle errors happening during the request.
116
+
117
+ If a error handler is provided nothing is raised.
118
+
119
+ If your error handler returns anything else but `nil` it replaces the response body.
120
+
121
+ ```ruby
122
+ handler = ->{ do_something; return {name: 'unknown'} }
123
+ response = LHC.get('http://something', error_handler: handler)
124
+ response.data.name # 'unknown'
125
+ ```
126
+
113
127
  ## Interceptors
114
128
 
115
129
  To monitor and manipulate the http communication done with LHC, you can define interceptors.
@@ -10,8 +10,8 @@ Gem::Specification.new do |s|
10
10
  s.authors = ['local.ch']
11
11
  s.email = ['ws-operations@local.ch']
12
12
  s.homepage = 'https://github.com/local-ch/lhc'
13
- s.summary = 'LocalHttpServices'
14
- s.description = 'Rails gem for HTTP wrapping typhoeus and providing additional features (like interceptors)'
13
+ s.summary = 'LocalHttpClient'
14
+ s.description = 'Rails gem for HTTP: Wraps typhoeus and provides additional features (like interceptors)'
15
15
 
16
16
  s.files = `git ls-files`.split("\n")
17
17
  s.test_files = `git ls-files -- spec/*`.split("\n") +
@@ -9,10 +9,11 @@ class LHC::Request
9
9
 
10
10
  TYPHOEUS_OPTIONS = [:params, :method, :body, :headers, :follow_location]
11
11
 
12
- attr_accessor :response, :options, :raw, :format
12
+ attr_accessor :response, :options, :raw, :format, :error_handler
13
13
 
14
14
  def initialize(options, self_executing = true)
15
15
  self.options = options.deep_dup || {}
16
+ self.error_handler = options.delete :error_handler
16
17
  use_configured_endpoint!
17
18
  generate_url_from_template!
18
19
  self.iprocessor = LHC::InterceptorProcessor.new(self)
@@ -97,10 +98,15 @@ class LHC::Request
97
98
  def on_complete(response)
98
99
  self.response ||= LHC::Response.new(response, self)
99
100
  iprocessor.intercept(:after_response, self.response)
100
- throw_error unless self.response.success?
101
+ handle_error(self.response) unless self.response.success?
101
102
  end
102
103
 
103
- def throw_error
104
+ def handle_error(response)
105
+ throw_error(response) unless error_handler
106
+ response.body_replacement = error_handler.call(response)
107
+ end
108
+
109
+ def throw_error(response)
104
110
  error = LHC::Error.find(response)
105
111
  fail error.new(error, response)
106
112
  end
@@ -4,7 +4,7 @@ require 'typhoeus'
4
4
  # and provides functionality to access response data.
5
5
  class LHC::Response
6
6
 
7
- attr_accessor :request
7
+ attr_accessor :request, :body_replacement
8
8
 
9
9
  # A response is initalized with the underlying raw response (typhoeus in our case)
10
10
  # and the associated request.
@@ -25,7 +25,7 @@ class LHC::Response
25
25
  end
26
26
 
27
27
  def body
28
- raw.body
28
+ body_replacement || raw.body
29
29
  end
30
30
 
31
31
  def code
@@ -1,3 +1,3 @@
1
1
  module LHC
2
- VERSION = "3.5.5"
2
+ VERSION = "3.6.0"
3
3
  end
@@ -59,4 +59,27 @@ describe LHC::Request do
59
59
  }).to raise_error(LHC::ParserError)
60
60
  end
61
61
  end
62
+
63
+ context 'custom error handler' do
64
+ it 'handles errors with the provided handler and does not raise them' do
65
+ stub_request(:get, "http://something").to_return(status: 400)
66
+ handler = spy('handler')
67
+ LHC::Request.new(url: "http://something", error_handler: handler)
68
+ expect(handler).to have_received(:call)
69
+ end
70
+
71
+ it 'exchanges body with handlers return if the handler returns something' do
72
+ stub_request(:get, "http://something").to_return(status: 400)
73
+ handler = ->(_response) { { name: 'unknown' }.to_json }
74
+ request = LHC::Request.new(url: "http://something", error_handler: handler)
75
+ expect(request.response.data.name).to eq 'unknown'
76
+ end
77
+
78
+ it 'does not exchange body with handlers return if the handler returns nil' do
79
+ stub_request(:get, "http://something").to_return(status: 400, body: { message: 'an error occurred' }.to_json)
80
+ handler = ->(_response) { nil }
81
+ request = LHC::Request.new(url: "http://something", error_handler: handler)
82
+ expect(request.response.data.message).to eq 'an error occurred'
83
+ end
84
+ end
62
85
  end
@@ -5,6 +5,6 @@ describe LHC::Request do
5
5
  stub_request(:get, 'http://local.ch')
6
6
  response = LHC.get('http://local.ch')
7
7
  request = response.request
8
- expect(request.headers).to eq("User-Agent" => "Typhoeus - https://github.com/typhoeus/typhoeus")
8
+ expect(request.headers.keys).to include 'User-Agent'
9
9
  end
10
10
  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: 3.5.5
4
+ version: 3.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - local.ch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-25 00:00:00.000000000 Z
11
+ date: 2016-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -122,8 +122,8 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
- description: Rails gem for HTTP wrapping typhoeus and providing additional features
126
- (like interceptors)
125
+ description: 'Rails gem for HTTP: Wraps typhoeus and provides additional features
126
+ (like interceptors)'
127
127
  email:
128
128
  - ws-operations@local.ch
129
129
  executables: []
@@ -280,7 +280,7 @@ rubyforge_project:
280
280
  rubygems_version: 2.2.2
281
281
  signing_key:
282
282
  specification_version: 4
283
- summary: LocalHttpServices
283
+ summary: LocalHttpClient
284
284
  test_files:
285
285
  - spec/basic_methods/delete_spec.rb
286
286
  - spec/basic_methods/get_spec.rb