lhc 7.3.3 → 8.0.0

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: 83dac4c381a05800447892414ec9f5ef164cf92e
4
- data.tar.gz: 15135229c079cca688d42d7a6d08f5ae745aa178
3
+ metadata.gz: 8e896a6247a34efa8fb35b9ba8cdf51ad68b8a00
4
+ data.tar.gz: f5d30a0736bbb74ea0cb4b62e060c4edeb9cc48e
5
5
  SHA512:
6
- metadata.gz: bccaf223150d3bafc8ea178ab5a7309eefba5d572a9af7e33582a00c7a7120e22762eab92d99d37f691e1cfb5a759e3033236ac552c6f44b472a5c348904a669
7
- data.tar.gz: 691ba40ddcb42c761b1092f97b98ff1e1a3a7110721d1a0c9c15d4c3c5abb3d3fe4daab1725385daa6ad358f08b986f80b0e4cbc28706fecbb5532e933f13782
6
+ metadata.gz: 06f42cbc9182070e2d89c325a261095ea66665aa27ae3b61f6e3206158b1f960f9939997001b9f8db087c5ff439c89b059548d54eaceb44ae7f106d73ae931a3
7
+ data.tar.gz: 663cfcbe61d6e9b220dc6073c1c1c9e4c576981984c304f60945fd30d01ca7c2c14e6aa040270439e15134b2178d8deba39d4c9141d65d214979e9382a47d99b
data/README.md CHANGED
@@ -241,7 +241,7 @@ To monitor and manipulate the http communication done with LHC, you can define i
241
241
  ```ruby
242
242
  class TrackingIdInterceptor < LHC::Interceptor
243
243
 
244
- def before_request(request)
244
+ def before_request
245
245
  request.params[:tid] = 123
246
246
  end
247
247
  end
data/docs/interceptors.md CHANGED
@@ -6,7 +6,7 @@ Interceptors
6
6
  ```ruby
7
7
  class TrackingIdInterceptor < LHC::Interceptor
8
8
 
9
- def before_request(request)
9
+ def before_request
10
10
  request.params[:tid] = 123
11
11
  end
12
12
  end
@@ -27,15 +27,20 @@ like [Caching](/docs/interceptors/caching.md), [Monitoring](/docs/interceptors/m
27
27
 
28
28
  ## Callbacks
29
29
 
30
- `before_raw_request(request)` is called before the raw typhoeus request is prepared/created.
30
+ `before_raw_request` is called before the raw typhoeus request is prepared/created.
31
+
32
+ `before_request` is called when the request is prepared and about to be executed.
33
+
34
+ `after_request` is called after request was started.
35
+
36
+ `before_response` is called when response started to arrive.
31
37
 
32
- `before_request(request)` is called when the request is prepared and about to be executed.
38
+ `after_response` is called after the response arrived completely.
33
39
 
34
- `after_request(request)` is called after request was started.
35
40
 
36
- `before_response(request)` is called when response started to arrive.
41
+ ## Attributes: Request/Response
37
42
 
38
- `after_response(response)` is called after the response arrived completely.
43
+ Every interceptor can directly access their instance `request` or `response`.
39
44
 
40
45
  → [Read more about the request object](request.md)
41
46
 
data/lib/lhc.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'typhoeus'
2
2
  require 'active_support/core_ext/object/blank'
3
- require 'active_support/core_ext/hash/keys'
4
3
 
5
4
  module LHC
6
5
  autoload :BasicMethodsConcern,
@@ -106,8 +105,8 @@ module LHC
106
105
 
107
106
  autoload :Interceptor,
108
107
  'lhc/interceptor'
109
- autoload :InterceptorProcessor,
110
- 'lhc/interceptor_processor'
108
+ autoload :Interceptors,
109
+ 'lhc/interceptors'
111
110
  autoload :Formats,
112
111
  'lhc/formats'
113
112
  autoload :Monitoring,
@@ -1,14 +1,24 @@
1
1
  class LHC::Interceptor
2
2
 
3
- def before_raw_request(request); end
3
+ attr_reader :request
4
4
 
5
- def before_request(request); end
5
+ def initialize(request)
6
+ @request = request
7
+ end
8
+
9
+ def response
10
+ @request.response
11
+ end
12
+
13
+ def before_raw_request; end
14
+
15
+ def before_request; end
6
16
 
7
- def after_request(request); end
17
+ def after_request; end
8
18
 
9
- def before_response(request); end
19
+ def before_response; end
10
20
 
11
- def after_response(response); end
21
+ def after_response; end
12
22
 
13
23
  # Prevent Interceptors from beeing duplicated!
14
24
  # Their classes have flag-character.
@@ -0,0 +1,24 @@
1
+ # Handles interceptions during the lifecycle of a request
2
+ # Represents all active interceptors for a request/response.
3
+ class LHC::Interceptors
4
+
5
+ attr_accessor :all
6
+
7
+ # Intitalizes and determines if global or local interceptors are used
8
+ def initialize(request)
9
+ self.all = (request.options[:interceptors] || LHC.config.interceptors).map do |interceptor|
10
+ interceptor.new(request)
11
+ end
12
+ end
13
+
14
+ # Forwards messages to interceptors and handles provided responses.
15
+ def intercept(name)
16
+ all.each do |interceptor|
17
+ result = interceptor.send(name)
18
+ if result.is_a? LHC::Response
19
+ fail 'Response already set from another interceptor' if @response
20
+ @response = interceptor.request.response = result
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,6 +1,6 @@
1
1
  class LHC::Auth < LHC::Interceptor
2
2
 
3
- def before_request(request)
3
+ def before_request
4
4
  options = request.options[:auth] || {}
5
5
  authenticate!(request, options)
6
6
  end
@@ -8,7 +8,7 @@ class LHC::Caching < LHC::Interceptor
8
8
  # Options forwarded to the cache
9
9
  FORWARDED_OPTIONS = [:expires_in, :race_condition_ttl]
10
10
 
11
- def before_request(request)
11
+ def before_request
12
12
  return unless cache?(request)
13
13
  deprecation_warning(request.options)
14
14
  options = options(request.options)
@@ -19,7 +19,7 @@ class LHC::Caching < LHC::Interceptor
19
19
  from_cache(request, response_data)
20
20
  end
21
21
 
22
- def after_response(response)
22
+ def after_response
23
23
  return unless response.success?
24
24
  request = response.request
25
25
  return unless cache?(request)
@@ -6,7 +6,7 @@ class LHC::DefaultTimeout < LHC::Interceptor
6
6
  CONNECTTIMEOUT = 2 # seconds
7
7
  TIMEOUT = 15 # seconds
8
8
 
9
- def before_raw_request(request)
9
+ def before_raw_request
10
10
  request_options = (request.options || {})
11
11
  request_options[:timeout] ||= timeout || TIMEOUT
12
12
  request_options[:connecttimeout] ||= connecttimeout || CONNECTTIMEOUT
@@ -9,18 +9,18 @@ class LHC::Monitoring < LHC::Interceptor
9
9
 
10
10
  config_accessor :statsd
11
11
 
12
- def before_request(request)
12
+ def before_request
13
13
  return unless statsd
14
14
  LHC::Monitoring.statsd.count("#{key(request)}.before_request", 1)
15
15
  end
16
16
 
17
- def after_request(request)
17
+ def after_request
18
18
  return unless statsd
19
19
  LHC::Monitoring.statsd.count("#{key(request)}.count", 1)
20
20
  LHC::Monitoring.statsd.count("#{key(request)}.after_request", 1)
21
21
  end
22
22
 
23
- def after_response(response)
23
+ def after_response
24
24
  return unless statsd
25
25
  key = key(response)
26
26
  LHC::Monitoring.statsd.timing("#{key}.time", response.time) if response.success?
@@ -15,14 +15,15 @@ class LHC::Prometheus < LHC::Interceptor
15
15
  [LHC::Prometheus.namespace, 'lhc_times'].join('_').to_sym
16
16
  end
17
17
 
18
- def initialize
18
+ def initialize(request)
19
+ super(request)
19
20
  return if LHC::Prometheus.registered || LHC::Prometheus.client.blank?
20
21
  LHC::Prometheus.client.registry.counter(LHC::Prometheus.request_key, 'Counter of all LHC requests.')
21
22
  LHC::Prometheus.client.registry.histogram(LHC::Prometheus.times_key, 'Times for all LHC requests.')
22
23
  LHC::Prometheus.registered = true
23
24
  end
24
25
 
25
- def after_response(response)
26
+ def after_response
26
27
  return if !LHC::Prometheus.registered || LHC::Prometheus.client.blank?
27
28
  LHC::Prometheus.client.registry
28
29
  .get(LHC::Prometheus.request_key)
@@ -5,7 +5,7 @@ class LHC::Retry < LHC::Interceptor
5
5
  attr_accessor :max
6
6
  end
7
7
 
8
- def after_response(response)
8
+ def after_response
9
9
  response.request.options[:retries] ||= 0
10
10
  return unless retry?(response.request)
11
11
  response.request.options[:retries] += 1
@@ -1,7 +1,7 @@
1
1
  class LHC::Rollbar < LHC::Interceptor
2
2
  include ActiveSupport::Configurable
3
3
 
4
- def after_response(response)
4
+ def after_response
5
5
  return unless Object.const_defined?('Rollbar')
6
6
  return if response.success?
7
7
  request = response.request
data/lib/lhc/request.rb CHANGED
@@ -16,11 +16,11 @@ class LHC::Request
16
16
  self.error_handler = options.delete :error_handler
17
17
  use_configured_endpoint!
18
18
  generate_url_from_template!
19
- self.iprocessor = LHC::InterceptorProcessor.new(self)
20
- iprocessor.intercept(:before_raw_request, self)
19
+ self.interceptors = LHC::Interceptors.new(self)
20
+ interceptors.intercept(:before_raw_request)
21
21
  self.raw = create_request
22
22
  self.format = options.delete('format') || LHC::Formats::JSON.new
23
- iprocessor.intercept(:before_request, self)
23
+ interceptors.intercept(:before_request)
24
24
  run! if self_executing && !response
25
25
  end
26
26
 
@@ -50,7 +50,7 @@ class LHC::Request
50
50
 
51
51
  private
52
52
 
53
- attr_accessor :iprocessor
53
+ attr_accessor :interceptors
54
54
 
55
55
  def optionally_encoded_url(options)
56
56
  return options[:url] unless options.fetch(:url_encoding, true)
@@ -60,8 +60,8 @@ class LHC::Request
60
60
  def create_request
61
61
  request = Typhoeus::Request.new(optionally_encoded_url(options), typhoeusize(options))
62
62
  request.on_headers do
63
- iprocessor.intercept(:after_request, self)
64
- iprocessor.intercept(:before_response, self)
63
+ interceptors.intercept(:after_request)
64
+ interceptors.intercept(:before_response)
65
65
  end
66
66
  request.on_complete { |response| on_complete(response) }
67
67
  request
@@ -111,7 +111,7 @@ class LHC::Request
111
111
 
112
112
  def on_complete(response)
113
113
  self.response = LHC::Response.new(response, self)
114
- iprocessor.intercept(:after_response, self.response)
114
+ interceptors.intercept(:after_response)
115
115
  handle_error(self.response) unless self.response.success?
116
116
  end
117
117
 
data/lib/lhc/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module LHC
2
- VERSION ||= '7.3.3'
2
+ VERSION ||= '8.0.0'
3
3
  end
@@ -1,28 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe LHC do
4
- context 'GET' do
5
- before do
6
- stub_request(:get, "http://datastore/v2/feedbacks").to_return(status: 200, body: "{}")
7
- end
4
+ context 'request' do
8
5
  it "is able to call .request without LHC raising NoMethodError: undefined method `blank?' for nil:NilClass when calling it outside of the rails context" do
9
- expect { LHC.request(url: "http://datastore/v2/feedbacks", method: :get) }.not_to raise_error
10
- end
11
- end
12
-
13
- context 'POST' do
14
- before do
15
- stub_request(:post, "http://datastore/v2/feedbacks").to_return(status: 200, body: "{}")
16
- end
17
-
18
- it "is able to call .request without LHC raising NoMethodError: undefined method `deep_symbolize_keys' for {}:Hash" do
19
- options = {
20
- url: "http://datastore/v2/feedbacks",
21
- method: :post,
22
- body: "{}",
23
- headers: { 'Content-Type' => 'application/json' }
24
- }
25
- expect { LHC.request(options) }.not_to raise_error
6
+ expect { LHC.request(url: "http://datastore/v2/feedbacks", method: :get) }.not_to raise_error(NoMethodError)
26
7
  end
27
8
  end
28
9
  end
@@ -4,7 +4,7 @@ describe LHC do
4
4
  context 'interceptor' do
5
5
  before(:each) do
6
6
  class SomeInterceptor < LHC::Interceptor
7
- def after_request(request); end
7
+ def after_request; end
8
8
  end
9
9
  LHC.configure { |c| c.interceptors = [SomeInterceptor] }
10
10
  end
@@ -11,7 +11,7 @@ describe LHC do
11
11
 
12
12
  before(:each) do
13
13
  class StatsTimingInterceptor < LHC::Interceptor
14
- def after_response(response)
14
+ def after_response
15
15
  uri = URI.parse(response.request.url)
16
16
  path = [
17
17
  'web',
@@ -4,7 +4,7 @@ describe LHC do
4
4
  context 'interceptor' do
5
5
  before(:each) do
6
6
  class TrackingIdInterceptor < LHC::Interceptor
7
- def before_request(request)
7
+ def before_request
8
8
  request.params[:tid] = 123
9
9
  end
10
10
  end
@@ -4,7 +4,7 @@ describe LHC do
4
4
  context 'interceptor' do
5
5
  before(:each) do
6
6
  class SomeInterceptor < LHC::Interceptor
7
- def before_response(request); end
7
+ def before_response; end
8
8
  end
9
9
  LHC.configure { |c| c.interceptors = [SomeInterceptor] }
10
10
  end
@@ -6,7 +6,7 @@ describe LHC::Caching do
6
6
  expect do
7
7
  response = Typhoeus::Response.new(headers: { 'Accept' => 'application/json' })
8
8
  Marshal.dump(
9
- LHC::Caching.new.send(:to_cache, response)
9
+ LHC::Caching.new(response).send(:to_cache, response)
10
10
  )
11
11
  end.not_to raise_error
12
12
  end
@@ -8,7 +8,7 @@ describe LHC do
8
8
  @@cached = false
9
9
  cattr_accessor :cached
10
10
 
11
- def before_request(_request)
11
+ def before_request
12
12
  if @@cached
13
13
  return LHC::Response.new(Typhoeus::Response.new(response_body: 'Im served from local cache'), nil)
14
14
  end
@@ -18,7 +18,7 @@ describe LHC do
18
18
 
19
19
  class RemoteCacheInterceptor < LHC::Interceptor
20
20
 
21
- def before_request(request)
21
+ def before_request
22
22
  if request.response.nil?
23
23
  return LHC::Response.new(Typhoeus::Response.new(response_body: 'Im served from remote cache'), nil)
24
24
  end
@@ -5,7 +5,7 @@ describe LHC do
5
5
  before(:each) do
6
6
  class CacheInterceptor < LHC::Interceptor
7
7
 
8
- def before_request(_request)
8
+ def before_request
9
9
  LHC::Response.new(Typhoeus::Response.new(response_body: 'Im served from cache'), nil)
10
10
  end
11
11
  end
@@ -20,7 +20,7 @@ describe LHC do
20
20
  context 'misusage' do
21
21
  before(:each) do
22
22
  class AnotherInterceptor < LHC::Interceptor
23
- def before_request(_request)
23
+ def before_request
24
24
  LHC::Response.new(Typhoeus::Response.new({}), nil)
25
25
  end
26
26
  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: 7.3.3
4
+ version: 8.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhc/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-07 00:00:00.000000000 Z
11
+ date: 2018-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -217,7 +217,7 @@ files:
217
217
  - lib/lhc/formats.rb
218
218
  - lib/lhc/formats/json.rb
219
219
  - lib/lhc/interceptor.rb
220
- - lib/lhc/interceptor_processor.rb
220
+ - lib/lhc/interceptors.rb
221
221
  - lib/lhc/interceptors/auth.rb
222
222
  - lib/lhc/interceptors/caching.rb
223
223
  - lib/lhc/interceptors/default_timeout.rb
@@ -364,7 +364,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
364
364
  requirements:
365
365
  - Ruby >= 2.0.0
366
366
  rubyforge_project:
367
- rubygems_version: 2.6.14
367
+ rubygems_version: 2.6.12
368
368
  signing_key:
369
369
  specification_version: 4
370
370
  summary: LocalHttpClient
@@ -1,24 +0,0 @@
1
- # Handles interceptions during the lifecycle of a request
2
- class LHC::InterceptorProcessor
3
-
4
- attr_accessor :interceptors
5
-
6
- # Intitalizes the processor and determines if global or local interceptors are used
7
- def initialize(target)
8
- options = target.options if target.is_a? LHC::Request
9
- options ||= target.request.options if target.is_a? LHC::Response
10
- self.interceptors = (options[:interceptors] || LHC.config.interceptors).map { |i| i.new }
11
- end
12
-
13
- # Forwards messages to interceptors and handles provided responses.
14
- def intercept(name, target)
15
- interceptors.each do |interceptor|
16
- result = interceptor.send(name, target)
17
- if result.is_a? LHC::Response
18
- fail 'Response already set from another interceptor' if @response
19
- request = target.is_a?(LHC::Request) ? target : target.request
20
- @response = request.response = result
21
- end
22
- end
23
- end
24
- end