better-faraday 1.0.7 → 1.1.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
  SHA256:
3
- metadata.gz: 566a4665f7f21fbc42e2ab20190caf5d87d3d49d775ac376135c273728132334
4
- data.tar.gz: 4ab4177eb90929e0c3f1f89aa8418c74af97120770c68595cf6ba630e9d8894c
3
+ metadata.gz: 5df15bce61cd943bd8736051604de939682c563cb2836144049661e80dca4e50
4
+ data.tar.gz: 6fd2d60429e1684e85d474abacda0343260ecac1cf2cad5fbc9e964dd4087a20
5
5
  SHA512:
6
- metadata.gz: 0c459e43aed401c92e6e57957c057bfef94a4194dfb77bb789e3d5466cd99153f17a43b883a9a5896c3c0c5a074ab42f39ddda0ae749bc33744c40aebeb34054
7
- data.tar.gz: ceddc5b83c55fc7546b0dd2c6edb88a9711f8308c7c9363f1868b32b67e419b9b44ad41b0a6a3c8a114c34b2497a6db11b79d924a45a64c0ec128c56340f95db
6
+ metadata.gz: 2a002c9f4cdf85b2f1cdc91c1899d8b6766b18cacfa7e7946cabcdf945e261148bfeeeb2981022179d22ba85509296c3fda9ae5f15991f2d6b8ece3513a9bf73
7
+ data.tar.gz: 444c4694d034eb482b7b161dad15c56467e24a56755715d001cecb68626f32db9c495d6745dc50edf9f54211ad8b27f9cb554b3cb28e7ca6fb1c4a7fb406b669
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "better-faraday"
6
- s.version = "1.0.7"
6
+ s.version = "1.1.3"
7
7
  s.author = "Yaroslav Konoplov"
8
8
  s.email = "eahome00@gmail.com"
9
9
  s.summary = "Extends Faraday with useful features."
@@ -11,14 +11,21 @@ require "active_support/core_ext/string/filters"
11
11
  require "active_support/core_ext/string/inflections"
12
12
 
13
13
  Module.new do
14
- def run_request(method, url, body, headers, &block)
15
- super.tap { |response| response.env.instance_variable_set(:@request_body, body) }
14
+ def call(env)
15
+ env.instance_variable_set(:@request_sent_at, Time.now.utc)
16
+ super
16
17
  end
17
- end.tap { |m| Faraday::Connection.send(:prepend, m) }
18
+
19
+ def save_response(env, status, body, headers = nil, reason_phrase = nil)
20
+ env.instance_variable_set(:@response_received_at, Time.now.utc)
21
+ env.instance_variable_set(:@request_body, env.body.respond_to?(:read) ? env.body.read : env.body)
22
+ super
23
+ end
24
+ end.tap { |m| Faraday::Adapter.send(:prepend, m) }
18
25
 
19
26
  module Faraday
20
27
  class Env
21
- attr_reader :request_body
28
+ attr_reader :request_body, :request_sent_at, :response_received_at
22
29
  end
23
30
 
24
31
  class Error
@@ -39,7 +46,7 @@ module Faraday
39
46
  Faraday::Error
40
47
  end
41
48
 
42
- error = klass.new(describe)
49
+ error = klass.new("\n#{describe}")
43
50
  error.instance_variable_set(:@response, self)
44
51
  raise error
45
52
  end
@@ -64,13 +71,23 @@ module Faraday
64
71
  end
65
72
 
66
73
  def describe
67
- request_headers = __protect_data(env.request_headers.deep_dup)
68
- request_json = __protect_data(parse_json(env.request_body))
69
- response_headers = __protect_data(::Hash === env.response_headers ? env.response_headers.deep_dup : {})
70
- response_json = __protect_data(__parse_json(env.body))
74
+ request_headers = __protect_data(env.request_headers.deep_dup)
75
+
76
+ if env.request_headers["Content-Type"].to_s.match?(/\bapplication\/json\b/)
77
+ request_json = __protect_data(__parse_json(env.request_body.dup))
78
+ end
79
+
80
+ if env.response_headers
81
+ response_headers = __protect_data(env.response_headers.deep_dup)
82
+ end
83
+
84
+ if env.response_headers && env.response_headers["Content-Type"].to_s.match?(/\bapplication\/json\b/)
85
+ response_json = __protect_data(__parse_json(env.body.dup))
86
+ end
71
87
 
72
- [ "",
73
- "-- #{status} #{reason_phrase} --",
88
+ lines = [
89
+ "",
90
+ "-- #{status} #{reason_phrase} --".upcase,
74
91
  "",
75
92
  "-- Request URL --",
76
93
  env.url.to_s,
@@ -79,20 +96,63 @@ module Faraday
79
96
  env.method.to_s.upcase,
80
97
  "",
81
98
  "-- Request headers --",
82
- ::JSON.generate(request_headers).yield_self { |t| t.truncate(1024, omission: "... (truncated, full length: #{t.length})") },
99
+ ::JSON.generate(request_headers).yield_self { |t| t.truncate(2048, omission: "... (truncated, full length: #{t.length})") },
83
100
  "",
101
+
84
102
  "-- Request body --",
85
- (request_json ? ::JSON.generate(request_json) : env.request_body.to_s).yield_self { |t| t.truncate(1024, omission: "... (truncated, full length: #{t.length})") },
103
+ if request_json
104
+ ::JSON.generate(request_json)
105
+ else
106
+ body = env.request_body.to_s.dup
107
+ if body.encoding.name == "ASCII-8BIT"
108
+ "Binary (#{body.size} bytes)"
109
+ else
110
+ body
111
+ end
112
+ end.yield_self { |t| t.truncate(1024, omission: "... (truncated, full length: #{t.length})") },
113
+ "",
114
+
115
+ "-- Request sent at --",
116
+ env.request_sent_at.strftime("%Y-%m-%d %H:%M:%S.%2N") + " UTC",
86
117
  "",
118
+
87
119
  "-- Response headers --",
88
- (response_headers ? ::JSON.generate(response_headers) : env.response_headers.to_s).yield_self { |t| t.truncate(1024, omission: "... (truncated, full length: #{t.length})") },
120
+ if response_headers
121
+ ::JSON.generate(response_headers)
122
+ else
123
+ env.response_headers.to_s
124
+ end.yield_self { |t| t.truncate(2048, omission: "... (truncated, full length: #{t.length})") },
89
125
  "",
126
+
90
127
  "-- Response body --",
91
- (response_json ? ::JSON.generate(response_json) : env.body.to_s).yield_self { |t| t.truncate(1024, omission: "... (truncated, full length: #{t.length})") },
92
- ""
93
- ].join("\n")
128
+ if response_json
129
+ ::JSON.generate(response_json)
130
+ else
131
+ body = env.body.to_s.dup
132
+ if body.encoding.name == "ASCII-8BIT"
133
+ "Binary (#{body.size} bytes)"
134
+ else
135
+ body
136
+ end
137
+ end.yield_self { |t| t.truncate(2048, omission: "... (truncated, full length: #{t.length})") }
138
+ ]
139
+
140
+ if env.response_received_at
141
+ lines.concat [
142
+ "",
143
+ "-- Response received at --",
144
+ env.response_received_at.strftime("%Y-%m-%d %H:%M:%S.%2N") + " UTC",
145
+ "",
146
+ "-- Response received in --",
147
+ "#{((env.response_received_at.to_f - env.request_sent_at.to_f) * 1000.0).round(2)}ms"
148
+ ]
149
+ end
150
+
151
+ lines.join("\n") + "\n"
94
152
  end
95
153
 
154
+ alias inspect describe
155
+
96
156
  private
97
157
 
98
158
  def __parse_json(json)
@@ -107,7 +167,7 @@ module Faraday
107
167
  return data.map(&method(:__protect_data)) if ::Array === data
108
168
  return data unless ::Hash === data
109
169
  data.each_with_object({}) do |(key, value), memo|
110
- memo[key] = if key.respond_to?(:=~) && Faraday.secrets.any? { |s| key =~ s }
170
+ memo[key] = if key.to_s.underscore.tr("_", " ").yield_self { |k| Faraday.secrets.any? { |s| k.match?(s) } }
111
171
  "SECRET"
112
172
  else
113
173
  __protect_data(value)
@@ -128,5 +188,5 @@ module Faraday
128
188
  attr_accessor :secrets
129
189
  end
130
190
 
131
- self.secrets = [/\bpass(?:word|phrase)\b/i, /\bauthorization\b/i, /\bsecret\b/i, /\b(:?access?)token\b/i]
191
+ self.secrets = [/\bpass(?:word|phrase)\b/i, /\bauthorization\b/i, /\bsecret\b/i, /\b(:?access)?token\b/i]
132
192
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better-faraday
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Konoplov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-16 00:00:00.000000000 Z
11
+ date: 2020-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday