lhc 10.5.2 → 10.5.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: 1ceecd53d94051855df66cfa2733ac9afda9b6b4
4
- data.tar.gz: fbb5373333e5a18c287c1846b3bf0ad5b913e269
3
+ metadata.gz: ca7d73d46581639f7628788d19deba210e4a593d
4
+ data.tar.gz: 2915b1bfe0cca8c89f8637134496f438b2ea60ec
5
5
  SHA512:
6
- metadata.gz: 829385685260241d20874839ca0a32d8ad05f5870a7a9e6a509882833bc86af8d02d6addd1b837acf0b31456e29a65e4267d9110ddbb2f1fc6e6c76536ddf146
7
- data.tar.gz: 63bc036627030c330e0a1ea811ea8296ce1cdf64b0ea499d761e29e0c5cae7b853d7484d501116bed38b901d81d7e25b504a61b7d5254ff079e742e79bb062e8
6
+ metadata.gz: 679e9a3b6ba4c5766377924da57c027a1d0812e0da0112d0d0cbeb6ca2b010985c8678e03d76de3a60c3ef7092121ea3500dd55a265e5d4dbda922abfd8b6324
7
+ data.tar.gz: d0fbeb0aa24298e8f9c843eebbab27617dc65c0e62af0fc520449059a5c9cf227e48163722732a1e124565220ec95f61e41367b732af5b2bf060eb8f7648f4c7
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This was copied from: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/hash/deep_transform_values.rb
4
+ class Hash
5
+ # Returns a new hash with all values converted by the block operation.
6
+ # This includes the values from the root hash and from all
7
+ # nested hashes and arrays.
8
+ #
9
+ # hash = { person: { name: 'Rob', age: '28' } }
10
+ #
11
+ # hash.deep_transform_values{ |value| value.to_s.upcase }
12
+ # # => {person: {name: "ROB", age: "28"}}
13
+ def deep_transform_values(&block)
14
+ _deep_transform_values_in_object(self, &block)
15
+ end
16
+
17
+ # Destructively converts all values by using the block operation.
18
+ # This includes the values from the root hash and from all
19
+ # nested hashes and arrays.
20
+ def deep_transform_values!(&block)
21
+ _deep_transform_values_in_object!(self, &block)
22
+ end
23
+
24
+ private
25
+
26
+ # support methods for deep transforming nested hashes and arrays
27
+ def _deep_transform_values_in_object(object, &block)
28
+ case object
29
+ when Hash
30
+ object.transform_values { |value| _deep_transform_values_in_object(value, &block) }
31
+ when Array
32
+ object.map { |e| _deep_transform_values_in_object(e, &block) }
33
+ else
34
+ yield(object)
35
+ end
36
+ end
37
+
38
+ def _deep_transform_values_in_object!(object, &block)
39
+ case object
40
+ when Hash
41
+ object.transform_values! { |value| _deep_transform_values_in_object!(value, &block) }
42
+ when Array
43
+ object.map! { |e| _deep_transform_values_in_object!(e, &block) }
44
+ else
45
+ yield(object)
46
+ end
47
+ end
48
+ end
data/lib/lhc.rb CHANGED
@@ -9,6 +9,8 @@ module LHC
9
9
  'lhc/concerns/lhc/basic_methods_concern'
10
10
  autoload :ConfigurationConcern,
11
11
  'lhc/concerns/lhc/configuration_concern'
12
+ autoload :FixInvalidEncodingConcern,
13
+ 'lhc/concerns/lhc/fix_invalid_encoding_concern'
12
14
  autoload :FormatsConcern,
13
15
  'lhc/concerns/lhc/formats_concern'
14
16
 
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+
5
+ module LHC
6
+ module FixInvalidEncodingConcern
7
+ extend ActiveSupport::Concern
8
+
9
+ module ClassMethods
10
+ # fix strings that contain non-UTF8 encoding in a forceful way
11
+ # should none of the fix-attempts be successful,
12
+ # an empty string is returned instead
13
+ def fix_invalid_encoding(string)
14
+ return string unless string.is_a?(String)
15
+ result = string.dup
16
+
17
+ # we assume it's ISO-8859-1 first
18
+ if !result.valid_encoding? || !utf8?(result)
19
+ result.encode!('UTF-8', 'ISO-8859-1', invalid: :replace, undef: :replace, replace: '')
20
+ end
21
+
22
+ # if it's still an issue, try with BINARY
23
+ if !result.valid_encoding? || !utf8?(result)
24
+ result.encode!('UTF-8', 'BINARY', invalid: :replace, undef: :replace, replace: '')
25
+ end
26
+
27
+ # if its STILL an issue, return an empty string :(
28
+ if !result.valid_encoding? || !utf8?(result)
29
+ result = ""
30
+ end
31
+
32
+ result
33
+ end
34
+
35
+ private
36
+
37
+ def utf8?(string)
38
+ string.encoding == Encoding::UTF_8
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class LHC::Error < StandardError
4
+ include LHC::FixInvalidEncodingConcern
5
+
4
6
  attr_accessor :response, :_message
5
7
 
6
8
  def self.map
@@ -61,44 +63,14 @@ class LHC::Error < StandardError
61
63
  return response if response.is_a?(String)
62
64
  request = response.request
63
65
  debug = []
64
- debug << [request.method, request.url].map { |str| fix_invalid_encoding(str) }.join(' ')
66
+ debug << [request.method, request.url].map { |str| self.class.fix_invalid_encoding(str) }.join(' ')
65
67
  debug << "Options: #{request.options}"
66
68
  debug << "Headers: #{request.headers}"
67
69
  debug << "Response Code: #{response.code} (#{response.options[:return_code]})"
68
70
  debug << "Response Options: #{response.options}"
69
71
  debug << response.body
70
72
  debug << _message
71
- debug.map { |str| fix_invalid_encoding(str) }.join("\n")
72
- end
73
-
74
- private
75
-
76
- # fix strings that contain non-UTF8 encoding in a forceful way
77
- # should none of the fix-attempts be successful,
78
- # an empty string is returned instead
79
- def fix_invalid_encoding(string)
80
- return string unless string.is_a?(String)
81
- result = string.dup
82
-
83
- # we assume it's ISO-8859-1 first
84
- if !result.valid_encoding? || !utf8?(result)
85
- result.encode!('UTF-8', 'ISO-8859-1', invalid: :replace, undef: :replace, replace: '')
86
- end
87
-
88
- # if it's still an issue, try with BINARY
89
- if !result.valid_encoding? || !utf8?(result)
90
- result.encode!('UTF-8', 'BINARY', invalid: :replace, undef: :replace, replace: '')
91
- end
92
-
93
- # if its STILL an issue, return an empty string :(
94
- if !result.valid_encoding? || !utf8?(result)
95
- result = ""
96
- end
97
-
98
- result
99
- end
100
73
 
101
- def utf8?(string)
102
- string.encoding == Encoding::UTF_8
74
+ debug.map { |str| self.class.fix_invalid_encoding(str) }.join("\n")
103
75
  end
104
76
  end
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'core_ext/hash/deep_transform_values'
4
+
3
5
  class LHC::Rollbar < LHC::Interceptor
4
6
  include ActiveSupport::Configurable
7
+ include LHC::FixInvalidEncodingConcern
5
8
 
6
9
  def after_response
7
10
  return unless Object.const_defined?('Rollbar')
@@ -23,6 +26,11 @@ class LHC::Rollbar < LHC::Interceptor
23
26
  params: request.params
24
27
  }
25
28
  }.merge additional_params
26
- Rollbar.warning("Status: #{response.code} URL: #{request.url}", data)
29
+ begin
30
+ Rollbar.warning("Status: #{response.code} URL: #{request.url}", data)
31
+ rescue Encoding::UndefinedConversionError
32
+ sanitized_data = data.deep_transform_values { |value| self.class.fix_invalid_encoding(value) }
33
+ Rollbar.warning("Status: #{response.code} URL: #{request.url}", sanitized_data)
34
+ end
27
35
  end
28
36
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LHC
4
- VERSION ||= '10.5.2'
4
+ VERSION ||= '10.5.3'
5
5
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+ require 'core_ext/hash/deep_transform_values'
5
+
6
+ describe Hash do
7
+ subject do
8
+ {
9
+ 'key' => 'value',
10
+ 'key2' => { 'key' => 'value', key2: 'value' }
11
+ }
12
+ end
13
+
14
+ let(:expected_result) do
15
+ {
16
+ 'key' => 'VALUE',
17
+ 'key2' => { 'key' => 'VALUE', key2: 'VALUE' }
18
+ }
19
+ end
20
+
21
+ it 'applies upcase to all values' do
22
+ expect(subject.deep_transform_values { |value| value.upcase }).to eq(expected_result)
23
+ end
24
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHC::Rollbar do
6
+ context 'invalid encoding in rollbar payload' do
7
+ before(:each) do
8
+ LHC.config.interceptors = [LHC::Rollbar]
9
+ stub_request(:get, 'http://local.ch').to_return(status: 400)
10
+
11
+ allow(described_class).to receive(:fix_invalid_encoding).and_call_original
12
+ # a stub that will throw a error on first call and suceed on subsequent calls
13
+ call_counter = 0
14
+ class Rollbar; end
15
+ ::Rollbar.stub(:warning) do
16
+ call_counter += 1
17
+ raise Encoding::UndefinedConversionError if call_counter == 1
18
+ end
19
+
20
+ # the response for the caller is still LHC::BadRequest
21
+ expect(-> { LHC.get('http://local.ch', rollbar: { additional: invalid }) }).to raise_error LHC::BadRequest
22
+ end
23
+
24
+ let(:invalid) { (+"in\xc3lid").force_encoding('ASCII-8BIT') }
25
+ let(:valid) { described_class.fix_invalid_encoding(invalid) }
26
+
27
+ it 'calls fix_invalid_encoding incase a Encoding::UndefinedConversionError was encountered' do
28
+ expect(described_class).to have_received(:fix_invalid_encoding).with(invalid)
29
+ end
30
+
31
+ it 'calls Rollbar.warn with the fixed data' do
32
+ expect(::Rollbar).to have_received(:warning)
33
+ .with(
34
+ 'Status: 400 URL: http://local.ch',
35
+ hash_including(
36
+ response: anything,
37
+ request: anything,
38
+ additional: valid
39
+ )
40
+ )
41
+ end
42
+ end
43
+ end
@@ -7,16 +7,21 @@ describe LHC::Rollbar do
7
7
  LHC.config.interceptors = [LHC::Rollbar]
8
8
  end
9
9
 
10
- it 'does not report if Rollbar is not defined' do
11
- stub_request(:get, 'http://local.ch').to_return(status: 400)
12
- expect(-> { LHC.get('http://local.ch') })
13
- .to raise_error LHC::BadRequest
10
+ context 'Rollbar is undefined' do
11
+ before(:each) do
12
+ Object.send(:remove_const, 'Rollbar') if Object.const_defined?('Rollbar')
13
+ end
14
+ it 'does not report' do
15
+ stub_request(:get, 'http://local.ch').to_return(status: 400)
16
+ expect(-> { LHC.get('http://local.ch') })
17
+ .to raise_error LHC::BadRequest
18
+ end
14
19
  end
15
20
 
16
21
  context 'Rollbar is defined' do
17
22
  before(:each) do
18
23
  class Rollbar; end
19
- allow(::Rollbar).to receive(:warning)
24
+ ::Rollbar.stub(:warning)
20
25
  end
21
26
 
22
27
  it 'does report errors to rollbar' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhc
3
3
  version: !ruby/object:Gem::Version
4
- version: 10.5.2
4
+ version: 10.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhc/contributors
@@ -213,9 +213,11 @@ files:
213
213
  - cider-ci/task_components/ruby.yml
214
214
  - friday.yml
215
215
  - lhc.gemspec
216
+ - lib/core_ext/hash/deep_transform_values.rb
216
217
  - lib/lhc.rb
217
218
  - lib/lhc/concerns/lhc/basic_methods_concern.rb
218
219
  - lib/lhc/concerns/lhc/configuration_concern.rb
220
+ - lib/lhc/concerns/lhc/fix_invalid_encoding_concern.rb
219
221
  - lib/lhc/concerns/lhc/formats_concern.rb
220
222
  - lib/lhc/concerns/lhc/request/user_agent_concern.rb
221
223
  - lib/lhc/config.rb
@@ -262,6 +264,7 @@ files:
262
264
  - spec/basic_methods/request_without_rails_spec.rb
263
265
  - spec/config/endpoints_spec.rb
264
266
  - spec/config/placeholders_spec.rb
267
+ - spec/core_ext/hash/deep_transform_values_spec.rb
265
268
  - spec/dummy/README.rdoc
266
269
  - spec/dummy/Rakefile
267
270
  - spec/dummy/app/assets/images/.keep
@@ -340,6 +343,7 @@ files:
340
343
  - spec/interceptors/response_competition_spec.rb
341
344
  - spec/interceptors/retry/main_spec.rb
342
345
  - spec/interceptors/return_response_spec.rb
346
+ - spec/interceptors/rollbar/invalid_encoding_spec.rb
343
347
  - spec/interceptors/rollbar/main_spec.rb
344
348
  - spec/interceptors/throttle/main_spec.rb
345
349
  - spec/interceptors/throttle/reset_track_spec.rb
@@ -409,6 +413,7 @@ test_files:
409
413
  - spec/basic_methods/request_without_rails_spec.rb
410
414
  - spec/config/endpoints_spec.rb
411
415
  - spec/config/placeholders_spec.rb
416
+ - spec/core_ext/hash/deep_transform_values_spec.rb
412
417
  - spec/dummy/README.rdoc
413
418
  - spec/dummy/Rakefile
414
419
  - spec/dummy/app/assets/images/.keep
@@ -487,6 +492,7 @@ test_files:
487
492
  - spec/interceptors/response_competition_spec.rb
488
493
  - spec/interceptors/retry/main_spec.rb
489
494
  - spec/interceptors/return_response_spec.rb
495
+ - spec/interceptors/rollbar/invalid_encoding_spec.rb
490
496
  - spec/interceptors/rollbar/main_spec.rb
491
497
  - spec/interceptors/throttle/main_spec.rb
492
498
  - spec/interceptors/throttle/reset_track_spec.rb