excon-rails 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 48b2039b185df915eed299bd0f926910ff7c0ca7
4
+ data.tar.gz: d8a9c2fb80a6530de30a13f5e99e83501424c4da
5
+ SHA512:
6
+ metadata.gz: ed778ec90ae6c9d5c6036e5267d4642c1771f59c0fe78b827444229bc628b3316e8902320421683d6de233397833e2d63ba8d42d80a9956fe247a5306f8e9d74
7
+ data.tar.gz: acfe782ebac451de2727f0cef718c3039de64186cc6ee2cfdeefcf2d689a98e4bf930e1cb3b4c5b2615f2e75eb69ed4f05750eeb47b362db85eeaae31961eee5
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.2
5
+ matrix:
6
+ include:
7
+ - rvm: jruby
8
+ env: JRUBY_OPTS="--2.0"
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ville Lautanala
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Excon::Rails [![Build Status](https://travis-ci.org/lautis/excon-rails.svg?branch=master)](https://travis-ci.org/lautis/excon-rails)
2
+
3
+ Railtie to include Excon HTTP requests in Rails logging.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your Rails application's Gemfile:
8
+
9
+ gem 'excon-rails'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install excon-rails
18
+
19
+ After you've started your Rails application, your Rails logs should include
20
+ Excon HTTP requests.
21
+
22
+ If you have debug logging enabled, individual requests are printed out:
23
+
24
+ Excon Request (105.66ms) GET https://google.com/
25
+ Excon Response (0.02ms) 302 Found (259 Bytes)
26
+
27
+
28
+ A total runtime is also included per each request:
29
+
30
+ Completed 200 OK in 132ms (Views: 0.2ms | ActiveRecord: 0.4ms | excon: 105.7ms)
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it ( https://github.com/lautis/excon-rails/fork )
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'excon/rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "excon-rails"
8
+ spec.version = Excon::Rails::VERSION
9
+ spec.authors = ["Ville Lautanala"]
10
+ spec.email = ["lautis@gmail.com"]
11
+ spec.summary = %q{Railtie to include Excon HTTP requests in Rails logging.}
12
+ spec.description = %q{Log HTTP requests made via Excon and their total runtime in Rails request log.}
13
+ spec.homepage = "https://github.com/lautis/excon-rails"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "excon", ">= 0.18.0"
22
+ spec.add_dependency "activesupport", ">= 3.0"
23
+ spec.add_dependency "sweet_notifications", "~> 0.2"
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "webmock", "~> 1.18"
28
+ end
@@ -0,0 +1 @@
1
+ require 'excon/rails'
@@ -0,0 +1,58 @@
1
+ require 'excon/rails/version'
2
+ require 'excon/rails/middleware'
3
+ require 'sweet_notifications'
4
+ require 'active_support/number_helper'
5
+ require 'active_support/core_ext/string/inflections'
6
+ require 'excon'
7
+
8
+ module Excon
9
+ module Rails
10
+ Middleware.install
11
+
12
+ Railtie, LogSubscriber = SweetNotifications.subscribe :excon, label: 'Excon' do
13
+ color ActiveSupport::LogSubscriber::BLUE
14
+
15
+ event :request do |event|
16
+ next unless logger.debug?
17
+ debug request_info(event)
18
+ end
19
+
20
+ event :response do |event|
21
+ next unless logger.debug?
22
+ debug message(event, 'Excon Response', "#{status(event)} (#{length(event)})")
23
+ end
24
+
25
+ event :retry do |event|
26
+ next unless logger.debug?
27
+ debug request_info(event)
28
+ end
29
+
30
+ event :error do |event|
31
+ next unless logger.info?
32
+ info message(event, 'Excon Error', event.payload[:error])
33
+ end
34
+
35
+ private
36
+
37
+ def length(event)
38
+ ActiveSupport::NumberHelper::NumberToHumanSizeConverter
39
+ .convert(event.payload[:body].length, {})
40
+ end
41
+
42
+ def status(event)
43
+ status_code = event.payload[:status]
44
+ status_message = Rack::Utils::HTTP_STATUS_CODES[status_code]
45
+ "#{status_code} #{status_message}"
46
+ end
47
+
48
+ def request_info(event)
49
+ payload = event.payload
50
+ url = "#{payload[:scheme]}://#{payload[:host]}#{payload[:path]}"
51
+ type = event.name.split('.').first.titleize
52
+ method = event.payload[:method].try(:upcase)
53
+ message(event, "Excon #{type}", [method, url].compact.join(' '))
54
+ end
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,35 @@
1
+ require 'excon'
2
+
3
+ module Excon
4
+ module Rails
5
+ class Middleware < Excon::Middleware::Base
6
+ class << self
7
+ def install
8
+ index = ::Excon.defaults[:middlewares].index(::Excon::Middleware::Instrumentor)
9
+ ::Excon.defaults[:middlewares].insert(index || -1, self)
10
+ end
11
+ end
12
+
13
+ def error_call(datum)
14
+ ActiveSupport::Notifications.instrument('error.excon', error: datum[:error])
15
+ @stack.error_call(datum)
16
+ end
17
+
18
+ def request_call(datum)
19
+ event_name = if datum[:retries_remaining] < datum[:retry_limit]
20
+ 'retry.excon'
21
+ else
22
+ 'request.excon'
23
+ end
24
+ ActiveSupport::Notifications.instrument(event_name, datum) do
25
+ @stack.request_call(datum)
26
+ end
27
+ end
28
+
29
+ def response_call(datum)
30
+ ActiveSupport::Notifications.instrument('response.excon', datum[:response])
31
+ @stack.response_call(datum)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ module Excon
2
+ module Rails
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,54 @@
1
+ describe Excon::Rails::Middleware do
2
+ before :each do
3
+ stub_request(:any, 'example.com').to_return(body: 'abc', status: 200)
4
+ stub_request(:any, 'example.com/timeout').to_timeout
5
+ stub_request(:any, 'example.com/retry').to_return(status: 400)
6
+ end
7
+
8
+ it 'emits events for requests' do
9
+ received = 0
10
+ ActiveSupport::Notifications.subscribe 'request.excon' do
11
+ received += 1
12
+ end
13
+ Excon.get('http://example.com')
14
+ ActiveSupport::Notifications.unsubscribe 'request.excon'
15
+ expect(received).to eq(1)
16
+ end
17
+
18
+ it 'emits events for responses' do
19
+ statuses = []
20
+ ActiveSupport::Notifications.subscribe 'response.excon' do |name, start, finish, id, payload|
21
+ statuses << payload[:status]
22
+ end
23
+ Excon.get('http://example.com')
24
+ ActiveSupport::Notifications.unsubscribe 'response.excon'
25
+ expect(statuses).to eq([200])
26
+ end
27
+
28
+ it 'emits events for retried requests' do
29
+ retries = 0
30
+ ActiveSupport::Notifications.subscribe 'retry.excon' do
31
+ retries += 1
32
+ end
33
+ connection = Excon.new('http://example.com/retry')
34
+ begin
35
+ connection.request(expects: [200], idempotent: true, retry_limit: 3)
36
+ rescue Excon::Errors::BadRequest
37
+ end
38
+ expect(retries).to eq(2)
39
+ ActiveSupport::Notifications.unsubscribe 'retry.excon'
40
+ end
41
+
42
+ it 'emits events for errors' do
43
+ errors = 0
44
+ ActiveSupport::Notifications.subscribe 'error.excon' do
45
+ errors += 1
46
+ end
47
+ begin
48
+ Excon.get('http://example.com/timeout')
49
+ rescue Excon::Errors::Timeout
50
+ end
51
+ expect(errors).to eq(1)
52
+ ActiveSupport::Notifications.unsubscribe 'error.excon'
53
+ end
54
+ end
@@ -0,0 +1,54 @@
1
+ module Excon
2
+ describe Rails do
3
+ before do
4
+ stub_request(:any, 'example.com').to_return(body: 'OK')
5
+ end
6
+
7
+ after do
8
+ if Rails::Railtie.instance.send(:instance_variable_defined?, :@ran)
9
+ Rails::Railtie.instance.send(:remove_instance_variable, :@ran)
10
+ end
11
+ end
12
+
13
+ it 'registers Excon middleware' do
14
+ middlewares = ::Excon.defaults[:middlewares]
15
+ expect(middlewares).to include(Excon::Rails::Middleware)
16
+ end
17
+
18
+ it 'initializes a Railtie' do
19
+ expect(Excon::Rails::Railtie.superclass).to eq(::Rails::Railtie)
20
+ end
21
+
22
+ it 'logs runtimes' do
23
+ stub_request(:any, 'example.com').to_return(body: 'OK')
24
+ Rails::Railtie.run_initializers
25
+ Excon.get('http://example.com')
26
+ expect(Rails::LogSubscriber.runtime).to be > 0
27
+ end
28
+
29
+ it 'logs requests to logger' do
30
+ @logger.level = Logger::DEBUG
31
+ Rails::Railtie.run_initializers
32
+ Excon.get('http://example.com')
33
+ expect(@logger.logged(:debug)[0])
34
+ .to match(%r{Excon Request \(\d+\.\d+ms\) GET http://example\.com/})
35
+ end
36
+
37
+ it 'deals with missing method' do
38
+ @logger.level = Logger::DEBUG
39
+ Rails::Railtie.run_initializers
40
+ Excon.new('http://example.com').request
41
+ expect(@logger.logged(:debug)[0])
42
+ .to match(%r{Excon Request \(\d+\.\d+ms\) http://example\.com/})
43
+ end
44
+
45
+ it 'omits debug logging in production' do
46
+ @logger.level = Logger::ERROR
47
+ Rails::Railtie.run_initializers
48
+ Excon.new('http://example.com').request
49
+ expect(@logger.logged(:debug)).to be_empty
50
+ expect(@logger.logged(:info)).to be_empty
51
+ expect(@logger.logged(:error)).to be_empty
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,30 @@
1
+ require 'webmock/rspec'
2
+ require 'active_support/log_subscriber/test_helper'
3
+ require 'excon-rails'
4
+
5
+ RSpec.configure do |config|
6
+ config.include ActiveSupport::LogSubscriber::TestHelper
7
+ config.profile_examples = 10
8
+ config.order = :random
9
+ Kernel.srand config.seed
10
+
11
+
12
+ config.expect_with :rspec do |expectations|
13
+ expectations.syntax = :expect
14
+ end
15
+
16
+ config.mock_with :rspec do |mocks|
17
+ mocks.syntax = :expect
18
+ mocks.verify_partial_doubles = true
19
+ end
20
+
21
+ config.before do
22
+ setup # Setup LogSubscriber::TestHelper
23
+ allow(Excon::Rails::LogSubscriber).to receive(:logger)
24
+ .and_return(ActiveSupport::LogSubscriber::TestHelper::MockLogger.new)
25
+ end
26
+
27
+ config.after do
28
+ teardown # Tear down LogSubscriber::TestHelper
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: excon-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ville Lautanala
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: excon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.18.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.18.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sweet_notifications
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '0.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '1.18'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '1.18'
111
+ description: Log HTTP requests made via Excon and their total runtime in Rails request
112
+ log.
113
+ email:
114
+ - lautis@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - .rspec
121
+ - .travis.yml
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - excon-rails.gemspec
127
+ - lib/excon-rails.rb
128
+ - lib/excon/rails.rb
129
+ - lib/excon/rails/middleware.rb
130
+ - lib/excon/rails/version.rb
131
+ - spec/excon/rails/middleware_spec.rb
132
+ - spec/excon/rails_spec.rb
133
+ - spec/spec_helper.rb
134
+ homepage: https://github.com/lautis/excon-rails
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.1.11
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Railtie to include Excon HTTP requests in Rails logging.
158
+ test_files:
159
+ - spec/excon/rails/middleware_spec.rb
160
+ - spec/excon/rails_spec.rb
161
+ - spec/spec_helper.rb