rack-faraday_inspector 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 95fdbd002b5be9a4b528166a62e9eb9e3bfa5cb0
4
+ data.tar.gz: c0249d1e1963c4dcc899468ae4b722bc533b4499
5
+ SHA512:
6
+ metadata.gz: 885457a8bf7c316204a1eeada3102947d8eee6d326648fc43ac0fcf1da0e3e9e545239dac91cadedd3be252589a8a2f4b5d5dc9e9a57d846c85ec38757a3e6af
7
+ data.tar.gz: 19fd5656a9fa66ee65df837a49dc8157bca02326e0872669eaf1b133a705bf77688a17b3962333cb1c31e6a6015510a51c82ddf87c06591ba46a99b182dcd0e7
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-faraday_inspector.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Rack::FaradayInspector
2
+
3
+ Rack::FaradayInspector renders a bit of HTML at the bottom of your pages that allows you to inspect all of the requests Faraday made for the current request.
4
+
5
+ ![rack-faraday_inspector](https://cloud.githubusercontent.com/assets/29424/13030496/029e5f7a-d262-11e5-886c-1b8e3b8a6260.gif)
6
+
7
+ Currently only supports Rails. Requires SASS and jQuery.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'rack-faraday_inspector', github: 'chrisb/rack-faraday_inspector'
15
+ ```
16
+
17
+ I'm still actively working on this Gem, so it has not been pushed to Rubygems yet.
18
+
19
+ ## Usage
20
+
21
+ To view requests, simply add the middleware to each of the Faraday connections you want to inspect:
22
+
23
+ ```ruby
24
+ require 'rack/faraday_inspector'
25
+
26
+ Faraday.new url: 'http://www.sushi.com' do |faraday|
27
+ faraday.use :inspector
28
+ # ...
29
+ end
30
+ ```
31
+
32
+ Note: only in the `development` environment will requests be instrumented and the inspector rendered.
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/chrisb/rack-faraday_inspector.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,60 @@
1
+ <style type='text/css'>
2
+ <!--
3
+ <%= @css %>
4
+ -->
5
+ </style>
6
+ <h1>I'm here!</h1>
7
+ <div class='faraday_inspector minimized'>
8
+ <div class='prompt'>
9
+ <%= Thread.current[:faraday_requests].count %> Faraday Request<%= 's' unless Thread.current[:faraday_requests].to_a.count == 1 %>
10
+ </div>
11
+ <div class='close'>&times;</div>
12
+ <table class='faraday_inspector-requests'>
13
+ <% Thread.current[:faraday_requests].to_a.each do |event| %>
14
+ <% req = event[:env] %>
15
+ <tr class='event'>
16
+ <td class='method'><%= req.method.upcase %></td>
17
+ <td class='host'><%= req.url.host %></td>
18
+ <td class='path'><%= req.url.path %></td>
19
+ <td class='duration'><%= event[:duration].round(2) %>s</td>
20
+ <td class='status'><%= req.status %></td>
21
+ </tr>
22
+ <tr>
23
+ <td colspan='5'>
24
+ <div class='request'>
25
+ <% if req[:body].present? %>
26
+ <% body = req[:body] %>
27
+ <% body = JSON.pretty_generate(JSON.parse(req[:body])) if req[:response_headers]['content-type'].to_s.include?('json') %>
28
+ <%= CGI.escapeHTML(body).gsub("\n", "<br />") %>
29
+ <% else %>
30
+ (No body for this request.)
31
+ <% end %>
32
+ </div>
33
+ </td>
34
+ </tr>
35
+ <% end %>
36
+ </table>
37
+ </div>
38
+ <script type="text/javascript">
39
+ //<![CDATA[
40
+ var closeAllRequests = function() {
41
+ $('.faraday_inspector table .request').removeClass('open');
42
+ }
43
+
44
+ var showFaradayRequest = function(e) {
45
+ var $el = $(this).next().find('.request');
46
+ var isOpen = $el.hasClass('open');
47
+ closeAllRequests();
48
+ if(!isOpen) { $el.addClass('open'); }
49
+ }
50
+
51
+ var toggleFardayInspector = function(e) {
52
+ closeAllRequests();
53
+ $('.faraday_inspector').toggleClass('minimized');
54
+ $('.faraday_inspector').toggleClass('open');
55
+ };
56
+
57
+ $('.faraday_inspector tr.event').click(showFaradayRequest);
58
+ $('.faraday_inspector .prompt, .faraday_inspector .close').click(toggleFardayInspector);
59
+ //]]>
60
+ </script>
@@ -0,0 +1,72 @@
1
+ .faraday_inspector {
2
+ position: absolute;
3
+ bottom: 0;
4
+ left: 0;
5
+ background-color: #f2f2f2;
6
+ font-size: 12px;
7
+ font-family: 'Helvetica Neue', sans-serif;
8
+
9
+ &.minimized {
10
+ border: 1px solid #eee;
11
+ border-top-right-radius: 8px;
12
+ padding: 8px;
13
+ table { display: none; }
14
+ .request { display: none; }
15
+ .close { display: none; }
16
+ }
17
+
18
+ &.open {
19
+ width: 100%;
20
+ border-top: 1px solid #eee;
21
+ .prompt { display: none; }
22
+ .close {
23
+ display: inline-block;
24
+ position: absolute;
25
+ top: 0;
26
+ right: 0;
27
+ padding: 6px;
28
+ font-size: 14px;
29
+ }
30
+ }
31
+
32
+ .event {
33
+ .method { font-weight: bold; }
34
+ .host, .duration, .status { color: #aaa; }
35
+ .host, .duration, .status, .method { width: 60px; }
36
+ .host, .duration, .status { text-align: right; }
37
+ .method { text-align: center; }
38
+ }
39
+
40
+ .request {
41
+ max-height: 400px;
42
+ text-overflow: scroll;
43
+ overflow: scroll;
44
+ font-family: 'Source Code Pro Light', 'Menlo', monospace;
45
+ margin: 10px;
46
+ background-color: #fff;
47
+ // border: 1px solid #aaa;
48
+ padding: 8px;
49
+ }
50
+
51
+ .prompt, .close {
52
+ cursor: pointer;
53
+ cursor: hand;
54
+ font-weight: bold;
55
+ }
56
+
57
+ table {
58
+ width: 100%;
59
+ font-family: 'Source Code Pro Light', 'Menlo', monospace;
60
+ .request {
61
+ display: none;
62
+ }
63
+ .request.open {
64
+ display: block;
65
+ }
66
+ tr.event:hover {
67
+ cursor: hand;
68
+ cursor: pointer;
69
+ background-color: #ffe;
70
+ }
71
+ }
72
+ }
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rack/faraday_inspector"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,12 @@
1
+ require 'faraday_middleware/instrumentation'
2
+
3
+ module Faraday
4
+ class Inspector < FaradayMiddleware::Instrumentation
5
+ def initialize(app, options = {})
6
+ super(app)
7
+ @name = options.fetch(:name, 'request.faraday.inspector')
8
+ end
9
+ end
10
+ end
11
+
12
+ Faraday::Middleware.register_middleware inspector: -> { Faraday::Inspector }
@@ -0,0 +1,4 @@
1
+ require 'faraday/inspector'
2
+ require 'rack/faraday_inspector/version'
3
+ require 'rack/faraday_inspector/middleware'
4
+ require 'rack/faraday_inspector/railtie'
@@ -0,0 +1,67 @@
1
+ module Rack
2
+ module FaradayInspector
3
+ class Middleware
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def template_path(*paths)
9
+ ::File.expand_path(::File.join(::File.dirname(__FILE__), '..', '..', '..', 'assets', *paths))
10
+ end
11
+
12
+ def css_template_path
13
+ template_path('sass', 'styles.scss')
14
+ end
15
+
16
+ def css_template
17
+ ::File.open(css_template_path).read
18
+ end
19
+
20
+ def render_css
21
+ @css = Sass::Engine.new(css_template,{
22
+ cache: true,
23
+ syntax: :scss,
24
+ style: :compressed,
25
+ filename: css_template_path,
26
+ }).render
27
+ end
28
+
29
+ def template
30
+ ::File.open(template_path('html', 'inspector.html.erb')).read
31
+ end
32
+
33
+ def content
34
+ render_css
35
+ ERB.new(template).result(binding)
36
+ end
37
+
38
+ def clear_requests
39
+ Thread.current[:faraday_requests] = nil
40
+ end
41
+
42
+ def call(env)
43
+ status, headers, response = @app.call(env)
44
+
45
+ if headers['Content-Type'].to_s.include? 'text/html'
46
+ new_response = []
47
+ response.each do |body|
48
+ partition = body.rpartition('</body>')
49
+ partition[0] += content.to_s
50
+ new_response << partition.join
51
+ end
52
+
53
+ # Update the content-length
54
+ headers['Content-Length'] = new_response.inject(0) do |len, body|
55
+ len += body.bytesize
56
+ end.to_s
57
+
58
+ [status, headers, new_response]
59
+ else
60
+ [status, headers, response]
61
+ end
62
+ ensure
63
+ clear_requests
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,21 @@
1
+ module Rack
2
+ module FaradayInspector
3
+ class Railtie < Rails::Railtie
4
+ initializer 'rack.faraday_inspector.subscribe' do
5
+ next unless Rails.env.development?
6
+ ActiveSupport::Notifications.subscribe('request.faraday.inspector') do |name, starts, ends, _, env|
7
+ Thread.current[:faraday_requests] ||= []
8
+ Thread.current[:faraday_requests] << {
9
+ env: env,
10
+ duration: ends - starts
11
+ }
12
+ end
13
+ end
14
+
15
+ initializer "rack.faraday_inspector.inject_middleware" do
16
+ next unless Rails.env.development?
17
+ Rails.application.middleware.use Rack::FaradayInspector::Middleware
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module FaradayInspector
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'rack/faraday_inspector'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/faraday_inspector/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-faraday_inspector"
8
+ spec.version = Rack::FaradayInspector::VERSION
9
+ spec.authors = ["Chris Bielinski"]
10
+ spec.email = ["chris@shadow.io"]
11
+
12
+ spec.summary = %q{View Faraday requests made in your controllers on rendered pages}
13
+ spec.description = %q{View Faraday requests made in your controllers on rendered pages}
14
+ spec.homepage = "https://github.com/chrisb/rack-faraday_inspector"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'faraday_middleware'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-faraday_inspector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Bielinski
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday_middleware
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: View Faraday requests made in your controllers on rendered pages
56
+ email:
57
+ - chris@shadow.io
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - README.md
65
+ - Rakefile
66
+ - assets/html/inspector.html.erb
67
+ - assets/sass/styles.scss
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/faraday/inspector.rb
71
+ - lib/rack/faraday_inspector.rb
72
+ - lib/rack/faraday_inspector/middleware.rb
73
+ - lib/rack/faraday_inspector/railtie.rb
74
+ - lib/rack/faraday_inspector/version.rb
75
+ - lib/rack_faraday_inspector.rb
76
+ - rack-faraday_inspector.gemspec
77
+ homepage: https://github.com/chrisb/rack-faraday_inspector
78
+ licenses: []
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.5.1
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: View Faraday requests made in your controllers on rendered pages
100
+ test_files: []
101
+ has_rdoc: