johac 0.9.0 → 0.9.1

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: c9a6e0cd9ab2cb5c7aba657db7c59a726d7ec85a
4
- data.tar.gz: c25864469ee9ace99f7b21fc7982a1bec72a8f69
3
+ metadata.gz: 169a90352f903f25093cb174c60cabd8c592e981
4
+ data.tar.gz: 8549fe4832fccee67a9d32b968d9def850d6b4e2
5
5
  SHA512:
6
- metadata.gz: c6a3065a97f85e39e4a05963b559eb0d9260200c7898953485b58217df614f6c99f44846d9616ce50995fd7d1af9e83fba414ac528818e0cd9f838654d954ac7
7
- data.tar.gz: 5bd7feade84a9af0a72e0341bf8ddb048867ce778d6aff22ee0f310d6d9a0ff38500d091c712cc5867d911eda69a4fec8687a96b65a985fc26376be97f021b2b
6
+ metadata.gz: 3d75e7c155d183c0153259e937f1b7473d7fcc02320742153b9607d1cfe8612f303c00bae85a4d4ac9c157e11ade8ae4f9eeeb91ab889445c05b47f44e9398c8
7
+ data.tar.gz: 7f8c041c46153d817e6fde4bc20be5d04e2247f9da1c6d62762280207f9d22e02381c916a943dd4e7c4ae233200ba2d3825f1299a83cb815c476f198fb34c443
@@ -17,6 +17,11 @@ module Johac
17
17
  faraday.request :johac_headers
18
18
  faraday.request :johac_auth, @config.auth_scheme, @config.access_key, @config.secret_key
19
19
 
20
+ # Allow caller to tap into the request before it is sent
21
+ if @config.request_tap
22
+ faraday.request :request_tap, @config.request_tap
23
+ end
24
+
20
25
  # Retry requests
21
26
  faraday.request :retry, {
22
27
  max: 2,
@@ -100,6 +105,24 @@ module Johac
100
105
  def call(env)
101
106
  env[:request_headers]['User-Agent'] = AgentString
102
107
  env[:request_headers]['Accept'] = Accept
108
+ env[:request_headers]['Content-Type'] = Accept
109
+ @app.call(env)
110
+ end
111
+
112
+ end
113
+
114
+ # Write custom user agent to all requests.
115
+ class RequestTapMiddleware < Faraday::Middleware
116
+
117
+ Faraday::Request.register_middleware :request_tap => self
118
+
119
+ def initialize(app, request_tap)
120
+ @request_tap = request_tap
121
+ super(app)
122
+ end
123
+
124
+ def call(env)
125
+ @request_tap.call(env)
103
126
  @app.call(env)
104
127
  end
105
128
 
data/lib/johac/error.rb CHANGED
@@ -8,15 +8,17 @@ module Johac
8
8
  # Exception to be used when dealing with HTTP responses from a Johac API.
9
9
  class ResponseError < Error
10
10
 
11
- attr_reader :code
11
+ attr_reader :status
12
12
  attr_reader :body
13
+ attr_reader :headers
13
14
 
14
15
  # Will attempt to decode a response body via JSON, and look for the 'message' key in the resulting (assumed) hash. If response body cannot be parsed via JSON the entire response body is set as the message for the exception.
15
16
  #
16
17
  # @param body [String] Response body.
17
18
  # @param headers [Hash] Response headers.
18
- def initialize(code, body, headers)
19
- @code = code
19
+ def initialize(status, body, headers)
20
+ @status = status
21
+ @headers = headers
20
22
  @body = if headers['Content-Type'] == 'application/json'
21
23
  JSON.parse(body) rescue body
22
24
  else
@@ -1,8 +1,7 @@
1
1
  module Johac
2
2
  class Response
3
3
 
4
- attr_reader :response
5
- attr_reader :exception
4
+ attr_reader :response, :exception, :body, :status
6
5
 
7
6
  include Enumerable
8
7
 
@@ -12,20 +11,16 @@ module Johac
12
11
  else
13
12
  @exception = result
14
13
  end
14
+
15
+ @body = result.body if result.respond_to?(:body)
16
+ @status = result.status if result.respond_to?(:status)
15
17
  end
16
18
 
17
19
  # Determine if the request failed
18
20
  #
19
21
  # @return true if response failed (http or expcetion)
20
22
  def error?
21
- exception != nil || status >= 400
22
- end
23
-
24
- # HTTP Status code
25
- #
26
- # @return response status code
27
- def status
28
- response_status || exception_status || 0
23
+ status.nil? || status >= 400
29
24
  end
30
25
 
31
26
  # HTTP Status code
@@ -35,13 +30,6 @@ module Johac
35
30
  status
36
31
  end
37
32
 
38
- # HTTP Response body as a JSON parsed object
39
- #
40
- # @return Hash/Array of the JSON parsed body, or empty hash
41
- def body
42
- response_body || exception_body || {}
43
- end
44
-
45
33
  # @return OpenStruct object of hash
46
34
  def object
47
35
  OpenStruct.new(body)
@@ -61,19 +49,23 @@ module Johac
61
49
  # @param args [Varargs] path of value
62
50
  #
63
51
  # @see {Hash.dig}
64
- # @see {Array.dig}
65
52
  #
66
53
  # @return value of key path
67
54
  def dig(*args)
68
- body.dig(*args)
55
+ body.kind_of?(Hash) ? body.dig(*args) : nil
69
56
  end
70
57
 
71
- # Enumerate over response body opject
58
+ # Enumerate over response body object and return
59
+ # a new Response with a modified body
72
60
  #
73
61
  # @see {Enumerable}
74
62
  # @param block [Block] to invoke
75
63
  def each(&block)
76
- body.each(&block)
64
+ if response?
65
+ body.each(&block)
66
+ else
67
+ self
68
+ end
77
69
  end
78
70
 
79
71
  # Invoke a block of code if the response fails, with
@@ -98,27 +90,35 @@ module Johac
98
90
  self
99
91
  end
100
92
 
101
- protected
102
-
103
- def response_status
104
- response? ? response.status : nil
105
- end
106
-
107
- def response_body
108
- response? ? response.body : nil
109
- end
110
-
111
- def exception_status
112
- response_error? ? exception.code : nil
93
+ # Chain another request to the successful response. The expected
94
+ # output of the block is another Johac::Response object.
95
+ #
96
+ # This enables request chaining, where an error in the chain will
97
+ # prevent further processing and return an error response
98
+ #
99
+ # response = client.request1(param)
100
+ # .flat_map { |r| client.request2(r.object.value) }
101
+ # .flat_map { |r| client.request3(r.object.value) }
102
+ #
103
+ # @param block [Block] to invoke
104
+ def flat_map(&block)
105
+ if response?
106
+ begin
107
+ yield self
108
+ rescue => e
109
+ Response.new(e)
110
+ end
111
+ else
112
+ self
113
+ end
113
114
  end
114
115
 
115
- def exception_body
116
- response_error? ? exception.body : nil
116
+ # @see flat_map
117
+ def and_then(&block)
118
+ flat_map(&block)
117
119
  end
118
120
 
119
- def response_error?
120
- exception.kind_of?(Johac::Error::ResponseError)
121
- end
121
+ protected
122
122
 
123
123
  def response?
124
124
  response != nil
data/lib/johac/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Johac
2
- Version = '0.9.0'
2
+ Version = '0.9.1'
3
3
  end
data/lib/johac.rb CHANGED
@@ -18,7 +18,7 @@ module Johac
18
18
  # @attr access_key [String] Public access key used for authorization of requests.
19
19
  # @attr secret_key [String] Private key used for authorization of requests.
20
20
  # @attr env [String] Environment. 'testing', 'development', or 'production'.
21
- Config = Struct.new(:base_uri, :auth_scheme, :access_key, :secret_key, :env, :logger, :raise_exceptions)
21
+ Config = Struct.new(:base_uri, :auth_scheme, :access_key, :secret_key, :env, :logger, :raise_exceptions, :request_tap)
22
22
 
23
23
 
24
24
  # Configure the global defaults for all clients built.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: johac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Simpson