johac 0.9.1 → 0.9.3

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: 169a90352f903f25093cb174c60cabd8c592e981
4
- data.tar.gz: 8549fe4832fccee67a9d32b968d9def850d6b4e2
3
+ metadata.gz: 9a61c39d9d7aa0e5b26e26dfb95c3fa939c2ba82
4
+ data.tar.gz: cb149b3ca883aecb69c3e4254c2972349d09e779
5
5
  SHA512:
6
- metadata.gz: 3d75e7c155d183c0153259e937f1b7473d7fcc02320742153b9607d1cfe8612f303c00bae85a4d4ac9c157e11ade8ae4f9eeeb91ab889445c05b47f44e9398c8
7
- data.tar.gz: 7f8c041c46153d817e6fde4bc20be5d04e2247f9da1c6d62762280207f9d22e02381c916a943dd4e7c4ae233200ba2d3825f1299a83cb815c476f198fb34c443
6
+ metadata.gz: 9d047090c1859f0219e3460d167f24f87306b29106fb46a2c18b08e430ffe2aa787dffc921fa11f871ece308bfe384e52d8ce44e0e7efc28a1b1cb0bdb4cca51
7
+ data.tar.gz: 89c2d34a9bacc322a2f6f4b64e053db7e94f987d8c9d68457dbcb66700c082722c91fbc74feba93c59a3bfa0870f569af595866e2a7ae738106a817b541cea6d
data/lib/johac/client.rb CHANGED
@@ -17,6 +17,7 @@ module Johac
17
17
  # @param config [Johac::Config]
18
18
  def initialize(config=nil)
19
19
  @config = Johac.merged_config(config)
20
+ @mutex = Mutex.new
20
21
  end
21
22
 
22
23
  # Reference to +base_uri+ set on {Johac.config} in {Johac.configure} or in the constructor.
@@ -34,6 +35,44 @@ module Johac
34
35
  config.env
35
36
  end
36
37
 
38
+ # Produce curls commands for captured requests made, within the block
39
+ #
40
+ # @param [Block] the block which invokes one or more requests
41
+ # @return [String] requests which were captured as curl commands
42
+ def curl_capture(&block)
43
+ capture(&block).map { |env|
44
+ output = []
45
+ output << "curl -s -X#{env.method.to_s.upcase}"
46
+ env.request_headers.each { |name, value|
47
+ output << "-H'#{name}: #{value}'"
48
+ }
49
+ output << "'#{env.url}'"
50
+ if env.body
51
+ output << '-d'
52
+ output << "'#{env.body}'"
53
+ end
54
+ output.join(' ')
55
+ }
56
+ end
57
+
58
+ # Capture requests and prevent them from going to the remote. Useful
59
+ # for testing.
60
+ #
61
+ # @param [Block] the block which invokes one or more requests
62
+ # @return [Array] faraday env structs which were captured in the block
63
+ def capture(&block)
64
+ result = []
65
+ @mutex.synchronize {
66
+ begin
67
+ connection.options.context = result
68
+ yield(self)
69
+ ensure
70
+ connection.options.context = nil
71
+ end
72
+ }
73
+ result
74
+ end
75
+
37
76
  protected
38
77
 
39
78
  def head(path, options={})
@@ -60,6 +99,8 @@ module Johac
60
99
  request { connection.patch(path, options[:body], options[:headers]) }
61
100
  end
62
101
 
102
+
103
+
63
104
  private
64
105
 
65
106
  # Wrap the response or error, or raise an exception if configured
@@ -22,6 +22,10 @@ module Johac
22
22
  faraday.request :request_tap, @config.request_tap
23
23
  end
24
24
 
25
+ # Captures requests when client calls are made
26
+ # within a capture block
27
+ faraday.request :request_capture
28
+
25
29
  # Retry requests
26
30
  faraday.request :retry, {
27
31
  max: 2,
@@ -111,6 +115,22 @@ module Johac
111
115
 
112
116
  end
113
117
 
118
+ # A request middleware that captures the request and halts the request, if
119
+ # the connections context isn't nil
120
+ class RequestCaptureMiddleware < Faraday::Middleware
121
+
122
+ Faraday::Request.register_middleware :request_capture => self
123
+
124
+ def call(env)
125
+ if env.request.context.kind_of?(Array)
126
+ env.request.context << env
127
+ return false
128
+ end
129
+ @app.call(env)
130
+ end
131
+
132
+ end
133
+
114
134
  # Write custom user agent to all requests.
115
135
  class RequestTapMiddleware < Faraday::Middleware
116
136
 
@@ -1,11 +1,9 @@
1
1
  module Johac
2
2
  class Response
3
3
 
4
- attr_reader :response, :exception, :body, :status
4
+ attr_reader :response, :exception, :body, :status, :chain
5
5
 
6
- include Enumerable
7
-
8
- def initialize(result)
6
+ def initialize(result, chain=[])
9
7
  if result.kind_of?(Faraday::Response)
10
8
  @response = result
11
9
  else
@@ -14,6 +12,7 @@ module Johac
14
12
 
15
13
  @body = result.body if result.respond_to?(:body)
16
14
  @status = result.status if result.respond_to?(:status)
15
+ @chain = chain
17
16
  end
18
17
 
19
18
  # Determine if the request failed
@@ -30,64 +29,48 @@ module Johac
30
29
  status
31
30
  end
32
31
 
33
- # @return OpenStruct object of hash
32
+ # @return OpenStruct object of hash, or empty struct if error
34
33
  def object
35
- OpenStruct.new(body)
34
+ if error?
35
+ OpenStruct.new(body)
36
+ else
37
+ OpenStruct.new
38
+ end
36
39
  end
37
40
 
38
- # Map body hash to another value using a block
41
+ # Map response body if successful
39
42
  #
40
43
  # @param block [Block] mapping function block
41
44
  #
42
45
  # @return result of block
43
- def map_object(&block)
44
- yield body
45
- end
46
-
47
- # Dig for a item in the body
48
- #
49
- # @param args [Varargs] path of value
50
- #
51
- # @see {Hash.dig}
52
- #
53
- # @return value of key path
54
- def dig(*args)
55
- body.kind_of?(Hash) ? body.dig(*args) : nil
56
- end
57
-
58
- # Enumerate over response body object and return
59
- # a new Response with a modified body
60
- #
61
- # @see {Enumerable}
62
- # @param block [Block] to invoke
63
- def each(&block)
64
- if response?
65
- body.each(&block)
46
+ def map(&block)
47
+ unless error?
48
+ yield body
66
49
  else
67
- self
50
+ nil
68
51
  end
69
52
  end
70
53
 
71
- # Invoke a block of code if the response fails, with
72
- # the exception as the paramter.
54
+ # Map the exception if present
73
55
  #
74
56
  # @param block [Block] to invoke
75
- def on_error(&block)
57
+ def map_error(&block)
76
58
  if error?
77
59
  yield exception
60
+ else
61
+ nil
78
62
  end
79
- self
80
63
  end
81
64
 
82
- # Invoke a block of code if the response succeeds, with the content
83
- # as a parameter
65
+ # Dig for a item in the body
84
66
  #
85
- # @param block [Block] to invoke
86
- def on_success(&block)
87
- unless error?
88
- yield body
89
- end
90
- self
67
+ # @param args [Varargs] path of value
68
+ #
69
+ # @see {Hash.dig}
70
+ #
71
+ # @return value of key path
72
+ def dig(*args)
73
+ body.kind_of?(Hash) ? body.dig(*args) : nil
91
74
  end
92
75
 
93
76
  # Chain another request to the successful response. The expected
@@ -104,9 +87,11 @@ module Johac
104
87
  def flat_map(&block)
105
88
  if response?
106
89
  begin
107
- yield self
90
+ result = yield self, chain
91
+ result.set_chain(chain + [self])
92
+ result
108
93
  rescue => e
109
- Response.new(e)
94
+ Response.new(e, chain + [self])
110
95
  end
111
96
  else
112
97
  self
@@ -120,6 +105,10 @@ module Johac
120
105
 
121
106
  protected
122
107
 
108
+ def set_chain(chain)
109
+ @chain = chain
110
+ end
111
+
123
112
  def response?
124
113
  response != nil
125
114
  end
data/lib/johac/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Johac
2
- Version = '0.9.1'
2
+ Version = '0.9.3'
3
3
  end
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.1
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Simpson
@@ -118,14 +118,14 @@ dependencies:
118
118
  requirements:
119
119
  - - "~>"
120
120
  - !ruby/object:Gem::Version
121
- version: 0.8.7.6
121
+ version: 0.9.11
122
122
  type: :development
123
123
  prerelease: false
124
124
  version_requirements: !ruby/object:Gem::Requirement
125
125
  requirements:
126
126
  - - "~>"
127
127
  - !ruby/object:Gem::Version
128
- version: 0.8.7.6
128
+ version: 0.9.11
129
129
  - !ruby/object:Gem::Dependency
130
130
  name: minitest
131
131
  requirement: !ruby/object:Gem::Requirement
@@ -183,4 +183,3 @@ signing_key:
183
183
  specification_version: 4
184
184
  summary: JSON Over HTTP API Client
185
185
  test_files: []
186
- has_rdoc: true