lhc 10.5.2 → 10.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/core_ext/hash/deep_transform_values.rb +48 -0
- data/lib/lhc.rb +2 -0
- data/lib/lhc/concerns/lhc/fix_invalid_encoding_concern.rb +42 -0
- data/lib/lhc/error.rb +4 -32
- data/lib/lhc/interceptors/rollbar.rb +9 -1
- data/lib/lhc/version.rb +1 -1
- data/spec/core_ext/hash/deep_transform_values_spec.rb +24 -0
- data/spec/interceptors/rollbar/invalid_encoding_spec.rb +43 -0
- data/spec/interceptors/rollbar/main_spec.rb +10 -5
- metadata +7 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca7d73d46581639f7628788d19deba210e4a593d
|
4
|
+
data.tar.gz: 2915b1bfe0cca8c89f8637134496f438b2ea60ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/lhc/error.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
data/lib/lhc/version.rb
CHANGED
@@ -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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
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.
|
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
|