api_client 0.5.15 → 0.5.16

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: a6c012cef145acbd3588f48fd9c4dbd56aa0ba90
4
- data.tar.gz: c9e0e93d078e9fac878b72f2a11220d40cb72e63
3
+ metadata.gz: 37901cb1987dc20c32b8853eedfabab968bf3b0e
4
+ data.tar.gz: fd064629bf64b68239f8957e3bbfa44eb2fde456
5
5
  SHA512:
6
- metadata.gz: 1111eab30cc3bcdc8fec74ba66a0b356f2ec6eb0460f4ee3e7991c2c5433996cd9416eaf84cbf418bc2dc790b2714e92ea5a352ffe68f77fb2abfd555c468b90
7
- data.tar.gz: 5aa6b0e78e3912e6c68b39ee4cacccc94394737e86cb5af64a2f32e370d72d1f811d9d2a0f59b4aab2ba8b43a7c86383152cc4d21e744e6131a1befec6932aab
6
+ metadata.gz: af89fc9e078b61a8a368e9ef777c5373fff8740e47cd67e40b1fb4a6a0724a1b6e581cf61df0cd14d8d2262be8bb405310fe31940aeb1974e1350e81f9154d8d
7
+ data.tar.gz: 13154f7651b172e8a647488583d89371873a64322c8ebc3bb55bc4c3fbdd070bab169982c424371ae288f058e5822f809dd5da50e47880a36be273cec8a0239d
@@ -1,3 +1,7 @@
1
+ # 0.5.16
2
+
3
+ * improve logging, log request details on Logger::DEBUG level
4
+
1
5
  # 0.5.15
2
6
 
3
7
  * add response status code to error message
data/README.md CHANGED
@@ -143,6 +143,17 @@ ApiClient::Base.
143
143
  get('/stuff.json') # => returns a parsed Array object
144
144
  ```
145
145
 
146
+ ## Logging
147
+
148
+ To log requests set the `ApiClient.logger`. To log request payloads and headers set level to `Logger::DEBUG`
149
+
150
+ ```ruby
151
+ require 'logger'
152
+ ApiClient.logger = Logger.new('api_client.log')
153
+ ApiClient.logger.level = Logger::INFO
154
+
155
+ ```
156
+
146
157
  Copyright
147
158
  ---------
148
159
 
@@ -1,17 +1,51 @@
1
1
  require "logger"
2
- class ApiClient::Connection::Middlewares::Request::Logger < Faraday::Middleware
3
2
 
3
+ class ApiClient::Connection::Middlewares::Request::Logger < Faraday::Middleware
4
4
  def call(env)
5
- time = Time.now
6
- returns = @app.call(env)
7
- taken = Time.now - time
8
- @logger.info "#{env[:method].to_s.upcase} #{env[:url]}: #{"%.4f" % taken} seconds"
9
- returns
5
+ debug_lines = []
6
+ should_log_details = @logger.level <= ::Logger::DEBUG
7
+
8
+ gather_request_debug_lines(env, debug_lines) if should_log_details
9
+
10
+ start = current_stamp_millisec
11
+ response = @app.call(env)
12
+ taken_sec = (current_stamp_millisec - start) / 1000.0
13
+
14
+ gather_response_debug_lines(response, taken_sec, debug_lines) if response && should_log_details
15
+
16
+ if should_log_details
17
+ @logger.debug { debug_lines.join("\n") }
18
+ else
19
+ @logger.info { "#{env[:method].to_s.upcase} #{env[:url]}: #{"%.4f" % taken_sec} seconds" }
20
+ end
21
+
22
+ response
10
23
  end
11
24
 
12
25
  def initialize(app, logger = nil)
13
26
  @logger = logger || ::Logger.new(STDOUT)
14
- @app = app
27
+ @app = app
15
28
  end
16
29
 
30
+ private
31
+
32
+ def gather_request_debug_lines(env, debug_lines)
33
+ debug_lines << "> #{env[:method].to_s.upcase} #{env[:url]}"
34
+ env[:request_headers].each { |k, v| debug_lines << "> #{k}: #{v}" }
35
+ debug_lines << "> "
36
+ debug_lines << "> #{env[:body]}\n> " if env[:body] && env[:body] != ""
37
+ debug_lines
38
+ end
39
+
40
+ def gather_response_debug_lines(response, taken_sec, debug_lines)
41
+ debug_lines << "< responded in #{"%.4f" % taken_sec} seconds with HTTP #{response.status}"
42
+ response.headers.each { |k, v| debug_lines << "< #{k}: #{v}" }
43
+ debug_lines << "< "
44
+ debug_lines << "< #{response.body}\n> " if response.body && response.body != ""
45
+ debug_lines
46
+ end
47
+
48
+ def current_stamp_millisec
49
+ Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
50
+ end
17
51
  end
@@ -1,3 +1,3 @@
1
1
  module ApiClient
2
- VERSION = "0.5.15"
2
+ VERSION = "0.5.16"
3
3
  end
@@ -46,7 +46,7 @@ describe ApiClient::Base do
46
46
 
47
47
  it "inspects subobjects properly" do
48
48
  subject.update(:id => 1, :sub => [1,2])
49
- subject.inspect.should == '#<ApiClient::Base id: 1, sub: [1, 2]>'
49
+ subject.inspect.should == '#<ApiClient::Base id: 1, sub: #<Hashie::Array [1, 2]>>'
50
50
  end
51
51
 
52
52
  it "makes sure id is the first key" do
@@ -1,10 +1,10 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe ApiClient::Connection::Middlewares::Request::Logger do
4
-
5
4
  it "adds a oauth header to the request" do
6
- app = double
7
- logger = FakeLogger.new
5
+ app = double
6
+ io = StringIO.new
7
+ logger = Logger.new(io)
8
8
  instance = ApiClient::Connection::Middlewares::Request::Logger.new(app, logger)
9
9
  env = {
10
10
  :url => "http://api.twitter.com",
@@ -13,7 +13,6 @@ describe ApiClient::Connection::Middlewares::Request::Logger do
13
13
  }
14
14
  app.should_receive(:call).with(env)
15
15
  instance.call(env)
16
- logger.history.first.include?("GET http://api.twitter.com").should be_true
16
+ io.string.should match("GET http://api.twitter.com")
17
17
  end
18
-
19
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.15
4
+ version: 0.5.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Bunsch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-27 00:00:00.000000000 Z
11
+ date: 2018-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -145,7 +145,6 @@ files:
145
145
  - spec/api_client/scope_spec.rb
146
146
  - spec/api_client/utils_spec.rb
147
147
  - spec/spec_helper.rb
148
- - spec/support/fake_logger.rb
149
148
  - spec/support/matchers.rb
150
149
  homepage: https://github.com/futuresimple/api_client
151
150
  licenses: []
@@ -166,7 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
166
165
  version: '0'
167
166
  requirements: []
168
167
  rubyforge_project: api_client
169
- rubygems_version: 2.6.2
168
+ rubygems_version: 2.6.13
170
169
  signing_key:
171
170
  specification_version: 3
172
171
  summary: API client builder
@@ -191,6 +190,4 @@ test_files:
191
190
  - spec/api_client/scope_spec.rb
192
191
  - spec/api_client/utils_spec.rb
193
192
  - spec/spec_helper.rb
194
- - spec/support/fake_logger.rb
195
193
  - spec/support/matchers.rb
196
- has_rdoc:
@@ -1,15 +0,0 @@
1
- class FakeLogger
2
-
3
- def initialize(*)
4
- @history = []
5
- end
6
-
7
- def history
8
- @history
9
- end
10
-
11
- def info(msg)
12
- @history.push(msg)
13
- end
14
-
15
- end