rollday 0.4.0 → 0.6.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
  SHA256:
3
- metadata.gz: 88eac5b844d6a7cd87818c350d03f96b5d0d470dd8c044f8bab3da9625720595
4
- data.tar.gz: fe85155acaebfb01238ce3530bde4c3b2b9537c3a24389e36dc8466423857831
3
+ metadata.gz: 836fb01c93ad7c663511d41a10b61ba4ecb7947f8e7fe06973267178181581d7
4
+ data.tar.gz: dc92f28700124355c7c505ff73e39556c7c7d18c3517335a8818390d8d02eb36
5
5
  SHA512:
6
- metadata.gz: 7249b1d26e10e71c56fca2355fe8d2f609724f476ba62a8663c55207c6fb508ab59ef455cc436a93ab4aa0cb9db5d9967dcbcc1ce4bfd82e5eabedd2f531ea7e
7
- data.tar.gz: 62b0b30259c81d79c601c50ee5b183939da161fef4460d1d668c50d971697050a7ceccb51a5dd50f4bc5062abda367f96823c62a2f7c61db25f343b994990b70
6
+ metadata.gz: 1feeea482c3827de3f1d2c918aecde8aa9d897c41651b0e0f812edfae52f78cb8b4d2a140b749aca586a7453364b50f40edba73643dd975ea60e86b62fd00926
7
+ data.tar.gz: f39c7a03087faf86c11e3470be983a1addc265caa62ba29824187ef144a08ff690c9196f2f1dcecffa3190d977b2373702f26ade02007e1eb8a842e7d2b1d163
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rollday (0.4.0)
4
+ rollday (0.5.0)
5
5
  class_composer
6
6
  faraday
7
7
  rollbar
data/README.md CHANGED
@@ -20,9 +20,9 @@ gem 'rollday'
20
20
  Intialization should happen in `app/initializers/rollday.rb`. All options below are the current defaults unless stated
21
21
  ```ruby
22
22
  Rollday.configure do |config|
23
- config.use_default_middleware! # [Not default option] set middleware for all Faraday requests (Faraday.get(...))
23
+ config.use_default_middleware! # [Not default option] set middleware for all Faraday requests (Faraday.get(...)). Caution when used with Default Client Middleware
24
24
 
25
- config.use_default_client_middleware! # [Not default option] set middleware for all Faraday clients.
25
+ config.use_default_client_middleware! # [Not default option] set middleware for all Faraday instances. Caution when used with Default Middleware
26
26
 
27
27
  config.status_code_regex = /[45]\d\d$/ # If status code matches, will attempt to send a rollbar
28
28
 
@@ -44,6 +44,65 @@ Rollday.configure do |config|
44
44
  end
45
45
  ```
46
46
 
47
+ ### Ex: Default Faraday Client
48
+
49
+ ```ruby
50
+ # Rollday initializer
51
+ Rollday.configure do |config|
52
+ config.use_default_middleware!
53
+ config.status_code_regex = /[2345]\d\d$/
54
+ config.message = -> (s, phrase, b, path, domain) { "[#{domain}] via #{path} returned #{status}" }
55
+ end
56
+
57
+ Farady.get("http://httpstat.us/207") # => 200 status code returned
58
+ # Will send a rollbar because Status code matches regex
59
+ ```
60
+
61
+ ### Ex: Default Faraday Instance
62
+
63
+ ```ruby
64
+ # Rollday initializer
65
+ Rollday.configure do |config|
66
+ config.use_default_client_middleware!
67
+ config.message = -> (s, phrase, b, path, domain) { "[#{domain}] via #{path} returned #{status} using default client middleware" }
68
+ end
69
+ Farady.get("http://httpstat.us/500") # => 500 status code returned
70
+ # Rollbar will not get sent because `use_default_middleware!` is not set
71
+
72
+ client = Faraday.new(url: base_url)
73
+ client.get("404") # => 404 status code returned
74
+ # Will send a rollbar because Status code matches regex
75
+ ```
76
+
77
+ ### Ex: Custom Faraday Instance
78
+
79
+ ```ruby
80
+ # Rollday initializer
81
+ Rollday.configure do |config|
82
+ config.status_code_regex = /[2]\d\d$/
83
+ config.message = -> (s, phrase, b, path, domain) { "[#{domain}] via #{path} returned #{status} using custom client middleware" }
84
+ end
85
+ Farady.get("http://httpstat.us/500") # => 500 status code returned
86
+ # Rollbar will not get sent because `use_default_middleware!` is not set
87
+
88
+ client = Faraday.new(url: base_url) do |conn|
89
+ conn.use Rollday::MIDDLEWARE_NAME
90
+ end
91
+ client.get("209") # => 209 status code returned
92
+ # Will send a rollbar because Status code matches regex
93
+ ```
94
+
95
+ ### Use Caution
96
+ ```ruby
97
+ # Rollday initializer
98
+ Rollday.configure do |config|
99
+ # Do not do this!
100
+ config.use_default_middleware!
101
+ config.use_default_client_middleware!
102
+ end
103
+ ```
104
+ Adding both the `use_default_middleware!` and the `use_default_client_middleware!` will cause double reporting of all default Faraday builders.
105
+
47
106
  ## Development
48
107
 
49
108
  After checking out the repo, run `bin/setup` to install dependencies. Then, run
@@ -4,7 +4,11 @@ module Rollday
4
4
  module FaradayConnectionOptions
5
5
  def new_builder(block)
6
6
  super.tap do |builder|
7
- builder.use(Rollday::FARADAY_NAME)
7
+ # allow scope to remove usage of middleware for a request
8
+ # after it has been injected into the Connection
9
+ if Rollday.config.allow_client_middleware
10
+ builder.use(Rollday::MIDDLEWARE_NAME)
11
+ end
8
12
  end
9
13
  end
10
14
  end
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "rollday/use_middleware"
4
- require "rollbar"
5
3
  require "class_composer"
4
+ require "rollbar"
5
+ require "rollday/errors"
6
+ require "rollday/use_middleware"
6
7
 
7
8
  module Rollday
8
9
  class Configuration
@@ -15,26 +16,32 @@ module Rollday
15
16
  ERROR = :error,
16
17
  CRITICAL = :critical
17
18
  ]
19
+ DEFAULT_ROLLBAR_LEVEL = WARNING
18
20
 
19
21
  DEFAULT_STATUS_CODE_REGEX = /[45]\d\d$/
20
22
  DEFAULT_MESSAGE_PROC = ->(status, phrase, body, path, domain) { "[#{status}]: #{domain} - #{path}" }
21
- DEFAULT_LEVEL_PROC = ->(_status) { WARNING }
23
+ DEFAULT_LEVEL_PROC = ->(_status) { DEFAULT_ROLLBAR_LEVEL }
22
24
  ROLLBAR_VALIDATOR = Proc.new do |value|
23
25
  value.is_a?(Proc) || ROLLBAR_LEVELS.include?(value)
24
26
  end
25
27
 
28
+ EXCEPTION_CLASS_DEFAULT = -> (status, phrase, body, path, domain) { Rollday::Faraday }
29
+
26
30
  add_composer :message, allowed: [Proc, String], default: DEFAULT_MESSAGE_PROC
27
31
  add_composer :params_query_sanitizer, allowed: Array, default: []
28
32
  add_composer :params_scope_sanitizer, allowed: Array, default: []
33
+ add_composer :person_scope, allowed: Proc
34
+ add_composer :rollbar_level, allowed: [Proc, Symbol], default: DEFAULT_LEVEL_PROC, validator: ROLLBAR_VALIDATOR, invalid_message: -> (val) { "Value must be a Proc or one of #{ROLLBAR_LEVELS}" }
29
35
  add_composer :status_code_regex, allowed: Regexp, default: DEFAULT_STATUS_CODE_REGEX
30
36
  add_composer :use_message_exception, allowed: [TrueClass, FalseClass], default: true
31
37
  add_composer :use_params_scope, allowed: [TrueClass, FalseClass], default: true
32
38
  add_composer :use_person_scope, allowed: [TrueClass, FalseClass], default: true
33
39
  add_composer :use_query_scope, allowed: [TrueClass, FalseClass], default: true
34
- add_composer :rollbar_level, allowed: [Proc, Symbol], default: DEFAULT_LEVEL_PROC, validator: ROLLBAR_VALIDATOR, invalid_message: -> (val) { "Value must be a Proc or one of #{ROLLBAR_LEVELS}" }
40
+ add_composer :exception_class, allowed: Proc, default: EXCEPTION_CLASS_DEFAULT
41
+ add_composer :allow_client_middleware, allowed: [TrueClass, FalseClass], default: true
35
42
 
36
43
  def person_scope
37
- return -> {} unless @use_person_scope
44
+ return -> {} unless use_person_scope
38
45
 
39
46
  @person_scope || Rollbar.scope.scope_object.raw[:person] || -> {}
40
47
  end
@@ -46,5 +53,9 @@ module Rollday
46
53
  def use_default_client_middleware!
47
54
  Rollday.use_default_client_middleware!
48
55
  end
56
+
57
+ def register_middleware!
58
+ Rollday.register_middleware!
59
+ end
49
60
  end
50
61
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rollday
4
+ class Error < StandardError; end
5
+ class Faraday < Error; end # used to create backtrace for rollbar
6
+ class ConfigError < Error; end
7
+ end
@@ -4,7 +4,7 @@ require "faraday"
4
4
  require "rollday/rollbar_helper"
5
5
 
6
6
  module Rollday
7
- class Middleware < Faraday::Middleware
7
+ class Middleware < ::Faraday::Middleware
8
8
  include RollbarHelper
9
9
 
10
10
  def initialize(app, **)
@@ -11,10 +11,12 @@ module Rollday
11
11
  end
12
12
 
13
13
  def rollbar_message(result)
14
- message = Rollday.config.message.(result.status, result.reason_phrase, result.body, URI(result.env.url).path, URI(result.env.url).host)
14
+ params = [result.status, result.reason_phrase, result.body, URI(result.env.url).path, URI(result.env.url).host]
15
+ message = Rollday.config.message.(*params)
15
16
  return message unless Rollday.config.use_message_exception
16
17
 
17
- Rollday::Faraday.new(message)
18
+ error_klass = Rollday.config.exception_class.(*params)
19
+ error_klass.new(message)
18
20
  end
19
21
 
20
22
  def rollbar_scope(result)
@@ -36,8 +38,10 @@ module Rollday
36
38
 
37
39
  def query_scope(result)
38
40
  return {} unless Rollday.config.use_query_scope
41
+ raw_query = URI(result.env.url).query
42
+ return if raw_query.nil?
39
43
 
40
- query_scope = CGI::parse(URI(result.env.url).query)
44
+ query_scope = CGI::parse(raw_query)
41
45
  Rollday.config.params_scope_sanitizer.each do |sanitizer|
42
46
  query_scope = sanitizer.(query_params)
43
47
  end
@@ -23,6 +23,7 @@ module Rollday
23
23
  def self.use_default_client_middleware!
24
24
  return false if @use_default_client_middleware
25
25
 
26
+ register_middleware!
26
27
  require "rollday/client_middleware"
27
28
  ::Faraday::ConnectionOptions.prepend(Rollday::FaradayConnectionOptions)
28
29
 
@@ -32,7 +33,7 @@ module Rollday
32
33
  def self.register_middleware!
33
34
  return false if @register_middleware
34
35
 
35
- ::Faraday::Middleware.register_middleware(Rollday::FARADAY_NAME => Middleware)
36
+ ::Faraday::Middleware.register_middleware(Rollday::MIDDLEWARE_NAME => Middleware)
36
37
  @register_middleware = true
37
38
  end
38
39
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rollday
4
- VERSION = "0.4.0"
4
+ VERSION = "0.6.0"
5
5
  end
data/lib/rollday.rb CHANGED
@@ -1,16 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "rollday/version"
4
3
  require "faraday"
5
4
  require "rollday/configuration"
5
+ require "rollday/errors"
6
6
  require "rollday/middleware"
7
+ require "rollday/version"
7
8
 
8
9
  module Rollday
9
- class Error < StandardError; end
10
- class Faraday < Error; end # used to create backtrace for rollbar
11
- class ConfigError < Error; end
12
-
13
- FARADAY_NAME = :rollday.freeze
10
+ MIDDLEWARE_NAME = :rollday.freeze
14
11
 
15
12
  def self.configure
16
13
  yield configuration if block_given?
@@ -20,18 +17,20 @@ module Rollday
20
17
  @configuration ||= Rollday::Configuration.new
21
18
  end
22
19
 
23
- class << self
24
- alias_method :config, :configuration
25
- end
26
-
27
20
  def self.configuration=(object)
28
21
  raise ConfigError, "Expected configuration to be a Rollday::Configuration" unless object.is_a?(Rollday::Configuration)
29
22
 
30
23
  @configuration = object
31
24
  end
32
25
 
33
- def self.use_middleware!
26
+ def self.reset_configuration!
27
+ @configuration = Rollday::Configuration.new
28
+ end
34
29
 
30
+ class << self
31
+ alias_method :config, :configuration
32
+ alias_method :config=, :configuration=
33
+ alias_method :reset_config!, :reset_configuration!
35
34
  end
36
35
 
37
36
  def self.use_default_middleware!
@@ -42,6 +41,10 @@ module Rollday
42
41
  Rollday::UseMiddleware.use_default_client_middleware!
43
42
  end
44
43
 
44
+ def self.register_middleware!
45
+ Rollday::UseMiddleware.register_middleware!
46
+ end
47
+
45
48
  def self.with_scope()
46
49
  end
47
50
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rollday
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Taylor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-04 00:00:00.000000000 Z
11
+ date: 2022-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: class_composer
@@ -133,6 +133,7 @@ files:
133
133
  - lib/rollday.rb
134
134
  - lib/rollday/client_middleware.rb
135
135
  - lib/rollday/configuration.rb
136
+ - lib/rollday/errors.rb
136
137
  - lib/rollday/middleware.rb
137
138
  - lib/rollday/rollbar_helper.rb
138
139
  - lib/rollday/use_middleware.rb