rack-spyup 0.0.2

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: 13a00d09007c24f2788b9b4ec6cc1bb932229d98
4
+ data.tar.gz: 5080110619f8bf7b52517355f3a3506739719723
5
+ SHA512:
6
+ metadata.gz: 8b5391158635c471a3df14d20ef7dc06346bbb4dff3e75fa30a232f21726d2bb407d9bdb2ff419b1cb67c10bd7a74c61029691e2e43b92bd453305f7710cdec9
7
+ data.tar.gz: 5232d7b4c384b333f9f69299dde0dc1e141e72f68979eceec87cae439700e73377dcc3e1d2b723665454ae392a64596bbb65efe04387321f885a68dd83341a7e
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-spyup.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Uchio KONDO
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,50 @@
1
+ # Rack::SpyUp
2
+
3
+ Spying request and json response
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rack-spyup'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rack-spyup
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ use Rack::SpyUp do |config|
23
+ config.logger = Logger.new(STDOUT)
24
+ end
25
+ ```
26
+
27
+ ### `spyup` command
28
+
29
+ rack-spyup is shipped with `spyup` command.
30
+ You can use usual config.ru with request and response spyed:
31
+
32
+ ```bash
33
+ $ spyup config.ru
34
+ ```
35
+
36
+ `spyup` has the same options as `rackup` (except `--builder`),
37
+ because she uses `Rack::Server` internally.
38
+
39
+ ## What you will see
40
+
41
+ ![Just like this](https://raw.github.com/udzura/rack-spyup/master/docs/spyup.png)
42
+
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/spyup ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby -*-
3
+ require 'rack'
4
+ require 'rack/server'
5
+
6
+ server = Rack::Server.new
7
+
8
+ builder_script = File.read(server.options.delete(:config) || 'config.ru')
9
+ builder_script.prepend "use Rack::SpyUp\n"
10
+ builder_script.prepend "require 'rack-spyup'\n"
11
+
12
+ server.options[:builder] = builder_script
13
+ server.start
data/docs/spyup.png ADDED
Binary file
data/examples/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "rack"
4
+ gem "rack-spyup", path: "../"
5
+ gem "pry"
@@ -0,0 +1,17 @@
1
+ require 'json'
2
+ require 'rack-spyup'
3
+ require 'pry'
4
+
5
+ App = lambda do |env|
6
+ json =
7
+ {
8
+ status: "OK",
9
+ body: {hello: "World", rand: rand(10000)},
10
+ array: ["this", "is", "array"]
11
+ }.to_json
12
+ [200, {"Content-Type" => "application/json"}, [json]]
13
+ end
14
+
15
+ # spyup this file and you will get the same log
16
+ # use Rack::SpyUp
17
+ run App
@@ -0,0 +1,16 @@
1
+ require 'json'
2
+ require 'rack-spyup'
3
+ require 'pry'
4
+
5
+ App = lambda do |env|
6
+ json =
7
+ {
8
+ status: "OK",
9
+ body: {hello: "World", rand: rand(10000)},
10
+ array: ["this", "is", "array"]
11
+ }.to_json
12
+ [200, {"Content-Type" => "application/json"}, [json]]
13
+ end
14
+
15
+ use Rack::SpyUp
16
+ run App
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class SpyUp
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
data/lib/rack/spyup.rb ADDED
@@ -0,0 +1,131 @@
1
+ require "rack/spyup/version"
2
+ require "logger"
3
+
4
+ module Rack
5
+ class SpyUp
6
+ def initialize(app, &configure)
7
+ @app = app
8
+ configure.call(self) if block_given?
9
+ end
10
+ attr_reader :app
11
+ attr_accessor :logger
12
+
13
+ def call(env)
14
+ return @app.call(env) if ignore_case?(env)
15
+
16
+ req = Rack::Request.new(env)
17
+ detected_logger(env).debug format_request_output(req)
18
+
19
+ status_code, headers, body = @app.call(env)
20
+ res = Rack::Response.new(body, status_code, headers)
21
+ if res.content_type =~ /json/
22
+ json = ""
23
+ body.each{|part| json << part }
24
+ detected_logger(env).debug format_response_output(res, json)
25
+ end
26
+ return res.finish
27
+ end
28
+
29
+ private
30
+ def ignore_case?(env)
31
+ # TODO more options to omit logging
32
+ return true if env["PATH_INFO"] =~ /favicon.ico$/
33
+ return false
34
+ end
35
+
36
+ # Do not cache - run detection by each call
37
+ def detected_logger(env)
38
+ _logger = logger || env['rack.logger'] || env['rack.error'] || ::Logger.new(STDOUT)
39
+ unless _logger.respond_to? :debug
40
+ def _logger.debug(str)
41
+ _logger.write(str)
42
+ end
43
+ end
44
+ _logger
45
+ end
46
+
47
+ def format_request_output(req)
48
+ body = req.body.read
49
+ req.body.rewind
50
+ <<-FORMAT % [to_request_line(req), compose_request_headers(req), body, format_cookies(req.cookies)]
51
+ ** REQUEST SPYED **
52
+ Request:
53
+ \t%s
54
+ Headers:
55
+ %s
56
+ Body:
57
+ \t%s
58
+ Cookies:
59
+ %s
60
+ FORMAT
61
+ end
62
+
63
+ def to_request_line(req)
64
+ "#{req.request_method} #{req.url}"
65
+ end
66
+
67
+ def compose_request_headers(req)
68
+ headers = <<-HEADERS.chomp
69
+ Content-Type: #{req.content_type}
70
+ Content-Length: #{req.content_length}
71
+ Remote: #{req.env['REMOTE_ADDR']}
72
+ HEADERS
73
+ req.env.each do |key, value|
74
+ parts = key.scan(/^HTTP_([A-Z_]+)/).flatten.first
75
+ next if parts.nil?
76
+ _key = parts.split('_')
77
+ .map{|part| part.sub(/(?<=^[A-Z])[A-Z]*/) {|m| m.downcase } }
78
+ .join("-")
79
+ headers << "\n" << "#{_key}: #{value}"
80
+ end
81
+ headers
82
+ .gsub(/^[-a-zA-Z]+:/m, red('\0'))
83
+ .gsub(/^/, "\t")
84
+ end
85
+
86
+ def format_cookies(cookies)
87
+ return gray("\t<none>") if cookies.empty?
88
+ cookies.map{|k, v|
89
+ "\t#{red(k)} = #{cyan(v)}"
90
+ }.join("\n")
91
+ end
92
+
93
+ def format_response_output(res, json)
94
+ <<-FORMAT % [res.status, compose_response_headers(res), pretty_json(json)]
95
+ ** RESPONSE SPYED **
96
+ Status Code: %d
97
+ Headers:
98
+ %s
99
+ Body:
100
+ %s
101
+ FORMAT
102
+ end
103
+
104
+ def pretty_json(json)
105
+ CodeRay.scan(JSON.pretty_unparse(JSON.parse(json)), :json)
106
+ .terminal
107
+ .gsub(/^/m, "\t")
108
+ end
109
+
110
+ def compose_response_headers(res)
111
+ res.headers.map {|k, v|
112
+ if v.include? "\n"
113
+ v = v.gsub(/\n/, "\n\t\t")
114
+ end
115
+ "\t#{red(k + ':')} #{v}"
116
+ }.join("\n")
117
+ end
118
+
119
+ def red(key)
120
+ "\e[31m#{key}\e[0m"
121
+ end
122
+
123
+ def cyan(value)
124
+ "\e[36m#{value}\e[0m"
125
+ end
126
+
127
+ def gray(value)
128
+ "\e[37m#{value}\e[0m"
129
+ end
130
+ end
131
+ end
data/lib/rack-spyup.rb ADDED
@@ -0,0 +1 @@
1
+ require 'rack/spyup'
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/spyup/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-spyup"
8
+ spec.version = Rack::SpyUp::VERSION
9
+ spec.authors = ["Uchio KONDO"]
10
+ spec.email = ["udzura@udzura.jp"]
11
+ spec.description = %q{Spying requests and responses with rack power}
12
+ spec.summary = %q{Spying requests and responses with rack power, like json, msgpack, xml, and so on...}
13
+ spec.homepage = "https://github.com/udzura/rack-spyup"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_runtime_dependency "rack", '>= 1.4'
22
+ spec.add_runtime_dependency "coderay"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "pry"
27
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-spyup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Uchio KONDO
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coderay
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
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
+ description: Spying requests and responses with rack power
84
+ email:
85
+ - udzura@udzura.jp
86
+ executables:
87
+ - spyup
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/spyup
97
+ - docs/spyup.png
98
+ - examples/Gemfile
99
+ - examples/config-spyup.ru
100
+ - examples/config.ru
101
+ - lib/rack-spyup.rb
102
+ - lib/rack/spyup.rb
103
+ - lib/rack/spyup/version.rb
104
+ - rack-spyup.gemspec
105
+ homepage: https://github.com/udzura/rack-spyup
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.0.14
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Spying requests and responses with rack power, like json, msgpack, xml, and
129
+ so on...
130
+ test_files: []