meta_request 0.1.4

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.

Potentially problematic release.


This version of meta_request might be problematic. Click here for more details.

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,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rails'
6
+ gem 'rack-contrib'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Dejan Simic
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,42 @@
1
+ # MetaRequest
2
+
3
+ Supporting gem for [Rails Panel (Google Chrome extension for Rails development)](https://github.com/dejan/rails_panel).
4
+
5
+ ## Installation
6
+
7
+ Add meta_request gem to development group in Gemfile:
8
+
9
+ ```ruby
10
+ group :development do
11
+ gem 'meta_request'
12
+ end
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ See [Rails Panel extension](https://github.com/dejan/rails_panel).
18
+
19
+ ## Licence
20
+
21
+ Copyright (c) 2012 Dejan Simic
22
+
23
+ MIT License
24
+
25
+ Permission is hereby granted, free of charge, to any person obtaining
26
+ a copy of this software and associated documentation files (the
27
+ "Software"), to deal in the Software without restriction, including
28
+ without limitation the rights to use, copy, modify, merge, publish,
29
+ distribute, sublicense, and/or sell copies of the Software, and to
30
+ permit persons to whom the Software is furnished to do so, subject to
31
+ the following conditions:
32
+
33
+ The above copyright notice and this permission notice shall be
34
+ included in all copies or substantial portions of the Software.
35
+
36
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
37
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
38
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
39
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
40
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
41
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
42
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,58 @@
1
+ module MetaRequest
2
+ class AppRequest
3
+ attr_reader :id, :events
4
+
5
+ def initialize(id, events = [])
6
+ @id = id
7
+ @events = events
8
+ end
9
+
10
+ def self.current
11
+ Thread.current[:meta_request_id]
12
+ end
13
+
14
+ def self.find(id)
15
+ new(id, storage.read(id))
16
+ end
17
+
18
+ def current!
19
+ Thread.current[:meta_request_id] = self
20
+ end
21
+
22
+ def save
23
+ storage.write(id, events)
24
+ maintain_key_pool(id)
25
+ self
26
+ end
27
+
28
+ def destroy
29
+ storage.delete(id)
30
+ end
31
+
32
+ private
33
+ # keeps cache size small
34
+ def maintain_key_pool(id, size=10)
35
+ key_pool = storage.read(:key_pool) || []
36
+ key_pool.unshift(id)
37
+ keep, clear = key_pool.each_slice(size).to_a
38
+ (clear||[]).each do |key|
39
+ storage.delete(key)
40
+ end
41
+ storage.write(:key_pool, keep)
42
+ end
43
+
44
+ def storage
45
+ self.class.storage
46
+ end
47
+
48
+ def self.storage
49
+ @@storage ||= build_storage
50
+ end
51
+
52
+ def self.build_storage
53
+ dir_path = File.join(Dir.pwd, 'tmp/cache/meta_request')
54
+ FileUtils.mkdir_p dir_path
55
+ ActiveSupport::Cache::FileStore.new(dir_path)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,23 @@
1
+ module MetaRequest
2
+ class Event < ActiveSupport::Notifications::Event
3
+
4
+ def initialize(name, start, ending, transaction_id, payload)
5
+ super(name, start, ending, transaction_id, payload)
6
+ case @name
7
+ when 'process_action.action_controller'
8
+ @payload[:status] = '500' if @payload[:exception]
9
+ end
10
+ end
11
+
12
+ def self.events_for_exception(exception_wrapper)
13
+ exception = exception_wrapper.exception
14
+ trace = exception_wrapper.application_trace
15
+ trace = exception_wrapper.framework_trace if trace.empty?
16
+ trace.unshift "#{exception.class} (#{exception.message})"
17
+ trace.each do |call|
18
+ Event.new('process_action.action_controller.exception', 0, 0, nil, {:call => call})
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ module MetaRequest
2
+ module Middlewares
3
+ class AppRequestHandler
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ app_request = AppRequest.new env["action_dispatch.request_id"]
10
+ app_request.current!
11
+ @app.call(env)
12
+ rescue Exception => exception
13
+ wrapper = ActionDispatch::ExceptionWrapper.new(env, exception)
14
+ app_request.events.push(*Event.events_for_exception(wrapper))
15
+ raise
16
+ ensure
17
+ app_request.save
18
+ end
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,26 @@
1
+ require 'rack/contrib/response_headers'
2
+
3
+ module MetaRequest
4
+ module Middlewares
5
+ class Headers
6
+ def initialize(app, app_config)
7
+ @app = app
8
+ @app_config = app_config
9
+ end
10
+
11
+ def call(env)
12
+ request_path = env['PATH_INFO']
13
+ middleware = Rack::ResponseHeaders.new(@app) do |headers|
14
+ headers['X-Meta-Request-Version'] = MetaRequest::VERSION unless request_path.start_with?(assets_prefix)
15
+ end
16
+ middleware.call(env)
17
+ end
18
+
19
+ private
20
+ def assets_prefix
21
+ "/#{@app_config.assets.prefix[/\A\/?(.*?)\/?\z/, 1]}/"
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ module MetaRequest
2
+ module Middlewares
3
+ class MetaRequestHandler
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ request_id = env["PATH_INFO"][%r{^/__meta_request/(.+)\.json$}, 1]
10
+ if request_id
11
+ events_json(request_id)
12
+ else
13
+ @app.call(env)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def events_json(request_id)
20
+ app_request = AppRequest.find(request_id)
21
+ [200, { "Content-Type" => "text/plain; charset=utf-8" }, [app_request.events.to_json]]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ module MetaRequest
2
+ module Middlewares
3
+ autoload :Headers, "meta_request/middlewares/headers"
4
+ autoload :AppRequestHandler, "meta_request/middlewares/app_request_handler"
5
+ autoload :MetaRequestHandler, "meta_request/middlewares/meta_request_handler"
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ require 'rails/railtie'
2
+
3
+ module MetaRequest
4
+ class Railtie < ::Rails::Railtie
5
+
6
+ initializer 'meta_request.inject_middlewares' do |app|
7
+ app.middleware.use Middlewares::MetaRequestHandler
8
+ app.middleware.use Middlewares::Headers, app.config
9
+ app.middleware.use Middlewares::AppRequestHandler
10
+ end
11
+
12
+ initializer 'meta_request.subscribe_to_notifications' do
13
+ ActiveSupport::Notifications.subscribe do |*args|
14
+ AppRequest.current.events << Event.new(*args) if AppRequest.current
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+
21
+
@@ -0,0 +1,3 @@
1
+ module MetaRequest
2
+ VERSION = "0.1.4"
3
+ end
@@ -0,0 +1,8 @@
1
+ module MetaRequest
2
+ autoload :VERSION, "meta_request/version"
3
+ autoload :Event, "meta_request/event.rb"
4
+ autoload :AppRequest, "meta_request/app_request"
5
+ autoload :Middlewares, "meta_request/middlewares"
6
+ end
7
+
8
+ require "meta_request/railtie"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'meta_request/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "meta_request"
8
+ gem.version = MetaRequest::VERSION
9
+ gem.authors = ["Dejan Simic"]
10
+ gem.email = ["desimic@gmail.com"]
11
+ gem.description = %q{Request your request}
12
+ gem.summary = %q{Supporting gem for Rails Panel (Google Chrome extension for Rails development)}
13
+ gem.homepage = "https://github.com/dejan/rails_panel/tree/master/meta_request"
14
+
15
+ gem.add_dependency('rails')
16
+ gem.add_dependency('rack-contrib')
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.require_paths = ["lib"]
22
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: meta_request
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dejan Simic
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rack-contrib
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Request your request
47
+ email:
48
+ - desimic@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/meta_request.rb
59
+ - lib/meta_request/app_request.rb
60
+ - lib/meta_request/event.rb
61
+ - lib/meta_request/middlewares.rb
62
+ - lib/meta_request/middlewares/app_request_handler.rb
63
+ - lib/meta_request/middlewares/headers.rb
64
+ - lib/meta_request/middlewares/meta_request_handler.rb
65
+ - lib/meta_request/railtie.rb
66
+ - lib/meta_request/version.rb
67
+ - meta_request.gemspec
68
+ homepage: https://github.com/dejan/rails_panel/tree/master/meta_request
69
+ licenses: []
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.24
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Supporting gem for Rails Panel (Google Chrome extension for Rails development)
92
+ test_files: []
93
+ has_rdoc: