nxt_http_client 0.1.7 → 0.1.8

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
  SHA256:
3
- metadata.gz: e92672fe6aa4fc0e1a28cbe7384c7208823ddc240d08ab27d7d8d9a185396571
4
- data.tar.gz: 65dd6b57b39e9c32a3e21a22b4b25052bfb0b95e65f4b6289923db4aab55324a
3
+ metadata.gz: 5bdc0b6838780fe319613c5269ea5c0077cbc3ba621b69203d0902c15943c27f
4
+ data.tar.gz: 02fb6a39d21c5aa8661ddc591eee8324bf044898143f133b6f0478751a1726c0
5
5
  SHA512:
6
- metadata.gz: 5d76732a3495541363cf8e1000d1ac20a5c07a0898589a91634c0b024c198dc1cd59adcf134664761e6c1b9497299fb6b37e4a434ef132671f4fac7b24cc8e73
7
- data.tar.gz: 2cc16bc548581db1696626c7ff98307b0685de525be4f690881cbcf3af6483c67d342f3859d0498aecaa96a45a9aff46f50cb45be3dea3526e7fbfe6c1239ef6
6
+ metadata.gz: f157b49c5dc97c40650235d8828fb2f1e5d7917b65f3a42ca8ab6a517b621c6a89e747143ce782d50ecef8bb7f7c2bf1726f0daee7d78b321988758748a61d5b
7
+ data.tar.gz: 52086988a87eda709d21fa1114388411ee26e1d019366647199b039362073ff771c86064be3bc268bbce8edf9aab1dce7ad3e6aa78eed5c9ac76cdea6b9cb173
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nxt_http_client (0.1.7)
4
+ nxt_http_client (0.1.8)
5
5
  activesupport (~> 5.2)
6
6
  typhoeus
7
7
 
data/README.md CHANGED
@@ -33,6 +33,7 @@ class MyClient < NxtHttpClient
33
33
  method: :get,
34
34
  followlocation: true
35
35
  }
36
+ defaults.x_request_id_proc = -> { ('a'..'z').to_a.shuffle.take(10).join } # defaults to -> { SecureRandom.uuid }
36
37
  end
37
38
 
38
39
  register_response_handler do |handler|
@@ -41,8 +42,11 @@ class MyClient < NxtHttpClient
41
42
  end
42
43
  end
43
44
 
44
- before_fire do |request|
45
- # Will be called before fire
45
+ before_fire do |request, handler|
46
+ # Will be called before fire
47
+ handler.on!(200) do |response|
48
+ # reconfigure your handler before fire
49
+ end
46
50
  end
47
51
 
48
52
  after_fire do |request, result, response|
@@ -70,7 +74,8 @@ end
70
74
 
71
75
  ### register_defaults
72
76
 
73
- Register default request options on the class level.
77
+ Register default request options on the class level. Available options are `request_options` that are passed directly to
78
+ the underlying Typhoeus Request. Then there is `base_url` and `x_request_id_proc`.
74
79
 
75
80
  ### register_response_handler
76
81
 
@@ -3,6 +3,7 @@ require 'typhoeus'
3
3
 
4
4
  require 'nxt_http_client/version'
5
5
  require 'nxt_http_client/response_handler'
6
+ require 'nxt_http_client/default_config'
6
7
  require 'nxt_http_client/client_dsl'
7
8
  require 'nxt_http_client/client'
8
9
 
@@ -4,10 +4,10 @@ module NxtHttpClient
4
4
  CACHE_STRATEGIES = %w[global thread]
5
5
 
6
6
  def build_request(url, **opts)
7
- base_url = opts.delete(:base_url) || self.class.base_url
7
+ base_url = opts.delete(:base_url) || default_config.base_url
8
8
  url = [base_url, url].reject(&:blank?).join('/').gsub(/([^https?:]\/\/)/, '/')
9
9
 
10
- opts = self.class.default_request_options.with_indifferent_access.deep_merge(opts.with_indifferent_access)
10
+ opts = default_config.request_options.with_indifferent_access.deep_merge(opts.with_indifferent_access)
11
11
  opts[:headers] ||= {}
12
12
 
13
13
  if opts[:cache] ||= false
@@ -37,7 +37,7 @@ module NxtHttpClient
37
37
  request = build_request(url, opts.except(:response_handler))
38
38
 
39
39
  before_fire_callback = self.class.before_fire_callback
40
- before_fire_callback && instance_exec(request, &before_fire_callback)
40
+ before_fire_callback && instance_exec(request, response_handler, &before_fire_callback)
41
41
 
42
42
  if response_handler.callbacks['headers']
43
43
  request.on_headers do |response|
@@ -77,5 +77,9 @@ module NxtHttpClient
77
77
  def dup_handler_from_class
78
78
  self.class.response_handler.dup
79
79
  end
80
+
81
+ def default_config
82
+ self.class.default_config
83
+ end
80
84
  end
81
85
  end
@@ -1,60 +1,52 @@
1
- module ClientDsl
2
-
3
- def register_defaults(opts = defaults, &block)
4
- @defaults = opts
5
- @defaults.tap { |d| block.call(d) }
6
- @defaults
7
- end
8
-
9
- def before_fire(&block)
10
- @before_fire_callback = block
11
- end
12
-
13
- def before_fire_callback
14
- @before_fire_callback
15
- end
16
-
17
- def after_fire(&block)
18
- @after_fire_callback = block
19
- end
20
-
21
- def after_fire_callback
22
- @after_fire_callback
23
- end
24
-
25
- def defaults
26
- @defaults ||= OpenStruct.new
27
- end
28
-
29
- def base_url
30
- @base_url ||= (defaults.base_url || '')
31
- end
32
-
33
- def default_request_options
34
- @default_request_options ||= (defaults.request_options || ActiveSupport::HashWithIndifferentAccess.new)
35
- end
36
-
37
- def register_response_handler(handler = nil, &block)
38
- @response_handler = handler
39
- @response_handler ||= dup_handler_from_ancestor_or_new
40
- @response_handler.configure(&block) if block_given?
41
- @response_handler
42
- end
43
-
44
- def response_handler
45
- @response_handler
46
- end
47
-
48
- def dup_handler_from_ancestor_or_new
49
- handler_from_ancestor = ancestors[1].instance_variable_get(:@response_handler)
50
- handler_from_ancestor && handler_from_ancestor.dup || NxtHttpClient::ResponseHandler.new
51
- end
52
-
53
- def inherited(child)
54
- child.instance_variable_set(:@response_handler, @response_handler.dup)
55
- child.instance_variable_set(:@base_url, @base_url)
56
- child.instance_variable_set(:@before_fire_callback, @before_fire_callback.dup)
57
- child.instance_variable_set(:@after_fire_callback, @after_fire_callback.dup)
58
- child.instance_variable_set(:@defaults, OpenStruct.new(**@defaults.to_h.deep_dup))
1
+ module NxtHttpClient
2
+ module ClientDsl
3
+ def register_defaults(opts = {}, &block)
4
+ @default_config ||= DefaultConfig.new(**opts)
5
+ @default_config.tap { |d| block.call(d) }
6
+ @default_config
7
+ end
8
+
9
+ def before_fire(&block)
10
+ @before_fire_callback = block
11
+ end
12
+
13
+ def before_fire_callback
14
+ @before_fire_callback
15
+ end
16
+
17
+ def after_fire(&block)
18
+ @after_fire_callback = block
19
+ end
20
+
21
+ def after_fire_callback
22
+ @after_fire_callback
23
+ end
24
+
25
+ def default_config
26
+ @default_config ||= DefaultConfig.new
27
+ end
28
+
29
+ def register_response_handler(handler = nil, &block)
30
+ @response_handler = handler
31
+ @response_handler ||= dup_handler_from_ancestor_or_new
32
+ @response_handler.configure(&block) if block_given?
33
+ @response_handler
34
+ end
35
+
36
+ def response_handler
37
+ @response_handler
38
+ end
39
+
40
+ def dup_handler_from_ancestor_or_new
41
+ handler_from_ancestor = ancestors[1].instance_variable_get(:@response_handler)
42
+ handler_from_ancestor && handler_from_ancestor.dup || NxtHttpClient::ResponseHandler.new
43
+ end
44
+
45
+ def inherited(child)
46
+ child.instance_variable_set(:@response_handler, @response_handler.dup)
47
+ child.instance_variable_set(:@before_fire_callback, @before_fire_callback.dup)
48
+ child.instance_variable_set(:@after_fire_callback, @after_fire_callback.dup)
49
+ child.instance_variable_set(:@default_config, DefaultConfig.new(**default_config.to_h.deep_dup))
50
+ end
59
51
  end
60
52
  end
@@ -0,0 +1,11 @@
1
+ module NxtHttpClient
2
+ CONFIGURABLE_OPTIONS = %i[request_options base_url x_request_id_proc]
3
+
4
+ DefaultConfig = Struct.new('DefaultConfig', *CONFIGURABLE_OPTIONS) do
5
+ def initialize(request_options: ActiveSupport::HashWithIndifferentAccess.new, base_url: '', x_request_id_proc: -> { SecureRandom.uuid })
6
+ self.request_options = request_options
7
+ self.base_url = base_url
8
+ self.x_request_id_proc = x_request_id_proc
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module NxtHttpClient
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nxt_http_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Robecke
@@ -173,6 +173,7 @@ files:
173
173
  - lib/nxt_http_client.rb
174
174
  - lib/nxt_http_client/client.rb
175
175
  - lib/nxt_http_client/client_dsl.rb
176
+ - lib/nxt_http_client/default_config.rb
176
177
  - lib/nxt_http_client/response_handler.rb
177
178
  - lib/nxt_http_client/version.rb
178
179
  - nxt_http_client.gemspec