ratchetio 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +38 -0
- data/Rakefile +2 -0
- data/lib/ratchetio/configuration.rb +25 -0
- data/lib/ratchetio/rails/controller_methods.rb +55 -0
- data/lib/ratchetio/rails/middleware/exception_catcher.rb +27 -0
- data/lib/ratchetio/rails.rb +22 -0
- data/lib/ratchetio/railtie.rb +36 -0
- data/lib/ratchetio/version.rb +3 -0
- data/lib/ratchetio.rb +122 -0
- data/ratchetio.gemspec +19 -0
- data/spec/ratchetio_spec.rb +3 -0
- metadata +60 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Brian Rue
|
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
|
+
# Ratchetio
|
2
|
+
|
3
|
+
Ruby gem for Ratchet.io, for reporting exceptions in Rails 3 to Ratchet.io. Requires a Ratchet.io account (you can sign up for free).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'ratchetio'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install ratchetio
|
18
|
+
|
19
|
+
Then, create a file config/initializers/ratchetio.rb containing the following:
|
20
|
+
|
21
|
+
```
|
22
|
+
require 'ratchetio/rails'
|
23
|
+
Ratchetio.configure do |config|
|
24
|
+
config.access_token = 'YOUR_RATCHETIO_PROJECT_ACCESS_TOKEN'
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
This gem installs an exception handler into Rails. You don't need to do anything else for it to work.
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
1. Fork it
|
35
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
36
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
37
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
38
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Ratchetio
|
2
|
+
class Configuration
|
3
|
+
|
4
|
+
attr_accessor :access_token
|
5
|
+
attr_accessor :environment
|
6
|
+
attr_accessor :root
|
7
|
+
attr_accessor :branch
|
8
|
+
attr_accessor :framework
|
9
|
+
attr_accessor :endpoint
|
10
|
+
|
11
|
+
attr_accessor :logger
|
12
|
+
|
13
|
+
DEFAULT_ENDPOINT = "https://submit.ratchet.io/api/1/item/"
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@endpoint = DEFAULT_ENDPOINT
|
17
|
+
@framework = 'Plain'
|
18
|
+
end
|
19
|
+
|
20
|
+
# allow params to be read like a hash
|
21
|
+
def [](option)
|
22
|
+
send(option)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Ratchetio
|
2
|
+
module Rails
|
3
|
+
module ControllerMethods
|
4
|
+
|
5
|
+
def ratchetio_request_data
|
6
|
+
{
|
7
|
+
:params => params.to_hash,
|
8
|
+
:url => ratchetio_request_url,
|
9
|
+
:user_ip => ratchetio_user_ip,
|
10
|
+
:headers => ratchetio_request_headers,
|
11
|
+
:GET => request.GET.to_hash,
|
12
|
+
# leaving out POST for now
|
13
|
+
:method => request.method,
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def ratchetio_request_url
|
20
|
+
url = "#{request.protocol}#{request.host}"
|
21
|
+
unless [80, 443].include?(request.port)
|
22
|
+
url << ":#{request.port}"
|
23
|
+
end
|
24
|
+
url << request.fullpath
|
25
|
+
url
|
26
|
+
end
|
27
|
+
|
28
|
+
def ratchetio_user_ip
|
29
|
+
# priority: X-Real-Ip, then X-Forwarded-For, then request.remote_ip
|
30
|
+
real_ip = request.env["HTTP_X_REAL_IP"]
|
31
|
+
if real_ip
|
32
|
+
return real_ip
|
33
|
+
end
|
34
|
+
forwarded_for = request.env["HTTP_X_FORWARDED_FOR"]
|
35
|
+
if forwarded_for
|
36
|
+
return forwarded_for
|
37
|
+
end
|
38
|
+
request.remote_ip
|
39
|
+
end
|
40
|
+
|
41
|
+
def ratchetio_request_headers
|
42
|
+
headers = {}
|
43
|
+
request.env.each_pair do |k,v|
|
44
|
+
if k.match(/^HTTP_/)
|
45
|
+
# convert HTTP_CONTENT_TYPE to Content-Type, etc.
|
46
|
+
name = k.split("_", 2)[1].sub("_", "-").split(/(\W)/).map(&:capitalize).join
|
47
|
+
headers[name] = v
|
48
|
+
end
|
49
|
+
end
|
50
|
+
headers
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Ratchetio
|
2
|
+
module Rails
|
3
|
+
module Middleware
|
4
|
+
module ExceptionCatcher
|
5
|
+
def self.included(base)
|
6
|
+
base.send(:alias_method_chain, :render_exception, :ratchetio)
|
7
|
+
end
|
8
|
+
|
9
|
+
def render_exception_with_ratchetio(env, exception)
|
10
|
+
# wrap everything in a begin-rescue block
|
11
|
+
begin
|
12
|
+
controller = env['action_controller.instance']
|
13
|
+
request_data = controller.try(:ratchetio_request_data)
|
14
|
+
Ratchetio.report_request_exception(env, exception, request_data)
|
15
|
+
rescue Exception => exc
|
16
|
+
# TODO use logger here?
|
17
|
+
puts "[Ratchet.io] Exception while reporting exception to Ratchet.io: "
|
18
|
+
puts exc
|
19
|
+
end
|
20
|
+
|
21
|
+
# now continue as normal
|
22
|
+
render_exception_without_ratchetio(env, exception)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'ratchetio'
|
2
|
+
|
3
|
+
module Ratchetio
|
4
|
+
module Rails
|
5
|
+
def self.initialize
|
6
|
+
rails_logger = if defined?(::Rails.logger)
|
7
|
+
::Rails.logger
|
8
|
+
elsif defined?(RAILS_DEFAULT_LOGGER)
|
9
|
+
RAILS_DEFAULT_LOGGER
|
10
|
+
end
|
11
|
+
|
12
|
+
Ratchetio.configure do |config|
|
13
|
+
config.logger = rails_logger
|
14
|
+
config.environment = defined?(::Rails.env) && ::Rails.env || defined?(RAILS_ENV) && RAILS_ENV
|
15
|
+
config.root = defined?(::Rails.root) && ::Rails.root || defined?(RAILS_ROOT) && RAILS_ROOT
|
16
|
+
config.framework = defined?(::Rails.version) && "Rails: #{::Rails.version}" || defined?(::Rails::VERSION::STRING) && "Rails: #{::Rails::VERSION::STRING}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
Ratchetio::Rails.initialize
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'ratchetio'
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
module Ratchetio
|
5
|
+
class Railtie < ::Rails::Railtie
|
6
|
+
rake_tasks do
|
7
|
+
end
|
8
|
+
|
9
|
+
config.after_initialize do
|
10
|
+
Ratchetio.configure do |config|
|
11
|
+
config.logger ||= ::Rails.logger
|
12
|
+
config.environment ||= ::Rails.env
|
13
|
+
config.root ||= ::Rails.root
|
14
|
+
config.framework = "Rails: #{::Rails::VERSION::STRING}"
|
15
|
+
end
|
16
|
+
|
17
|
+
ActiveSupport.on_load(:action_controller) do
|
18
|
+
# lazily load action_controller methods
|
19
|
+
require 'ratchetio/rails/controller_methods'
|
20
|
+
include Ratchetio::Rails::ControllerMethods
|
21
|
+
end
|
22
|
+
|
23
|
+
if defined?(::ActionDispatch::DebugExceptions)
|
24
|
+
# rails 3.2.x
|
25
|
+
require 'ratchetio/rails/middleware/exception_catcher'
|
26
|
+
::ActionDispatch::DebugExceptions.send(:include, Ratchetio::Rails::Middleware::ExceptionCatcher)
|
27
|
+
elsif defined?(::ActionDispatch::ShowExceptions)
|
28
|
+
# rails 3.0.x and 3.1.x
|
29
|
+
require 'ratchetio/rails/middleware/exception_catcher'
|
30
|
+
::ActionDispatch::ShowExceptions.send(:include, Ratchetio::Rails::Middleware::ExceptionCatcher)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
data/lib/ratchetio.rb
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'socket'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
require 'ratchetio/version'
|
6
|
+
require 'ratchetio/configuration'
|
7
|
+
require 'ratchetio/railtie'
|
8
|
+
|
9
|
+
module Ratchetio
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_writer :configuration
|
13
|
+
|
14
|
+
def configure
|
15
|
+
yield(configuration)
|
16
|
+
end
|
17
|
+
|
18
|
+
def configuration
|
19
|
+
@configuration ||= Configuration.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def report_request_exception(env, exception, request_data)
|
23
|
+
data = base_data
|
24
|
+
|
25
|
+
# parse backtrace
|
26
|
+
frames = []
|
27
|
+
exception.backtrace.each { |frame|
|
28
|
+
# parse the line
|
29
|
+
match = frame.match(/(.*):(\d+)(?::in `([^']+)')?/)
|
30
|
+
frames.push({ :filename => match[1], :lineno => match[2].to_i, :method => match[3] })
|
31
|
+
}
|
32
|
+
# reverse so that the order is as ratchet expects
|
33
|
+
frames.reverse!
|
34
|
+
|
35
|
+
data[:body] = {
|
36
|
+
:trace => {
|
37
|
+
:frames => frames,
|
38
|
+
:exception => {
|
39
|
+
:class => exception.class.name,
|
40
|
+
:message => exception.message
|
41
|
+
}
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
data[:server] = server_data
|
46
|
+
data[:request] = request_data
|
47
|
+
|
48
|
+
payload = build_payload(data)
|
49
|
+
send_payload(payload)
|
50
|
+
end
|
51
|
+
|
52
|
+
def report_message()
|
53
|
+
# TODO
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def logger
|
59
|
+
configuration.logger
|
60
|
+
end
|
61
|
+
|
62
|
+
def send_payload(payload)
|
63
|
+
logger.info "[Ratchet.io] Sending payload"
|
64
|
+
|
65
|
+
uri = URI.parse(configuration.endpoint)
|
66
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
67
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
68
|
+
request.body = payload
|
69
|
+
response = http.request(request)
|
70
|
+
|
71
|
+
if respons.code == '200'
|
72
|
+
logger.info "[Ratchet.io] Success"
|
73
|
+
else
|
74
|
+
logger.warning "[Ratchet.io] Got unexpected status code from Ratchet.io api: " + response.code
|
75
|
+
logger.info "[Ratchet.io] Response:"
|
76
|
+
logger.info response.body
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def build_payload(data)
|
81
|
+
payload = {
|
82
|
+
:access_token => configuration.access_token,
|
83
|
+
:data => data
|
84
|
+
}
|
85
|
+
ActiveSupport::JSON.encode(payload)
|
86
|
+
end
|
87
|
+
|
88
|
+
def base_data(level="error")
|
89
|
+
config = configuration
|
90
|
+
{
|
91
|
+
:timestamp => Time.now.to_i,
|
92
|
+
:environment => config.environment,
|
93
|
+
:level => level,
|
94
|
+
:language => "ruby",
|
95
|
+
:framework => config.framework,
|
96
|
+
:notifier => {
|
97
|
+
:name => "ratchetio-gem",
|
98
|
+
:version => VERSION
|
99
|
+
}
|
100
|
+
}
|
101
|
+
end
|
102
|
+
|
103
|
+
def server_data
|
104
|
+
config = configuration
|
105
|
+
data = {
|
106
|
+
:host => Socket.gethostname
|
107
|
+
}
|
108
|
+
if config.root
|
109
|
+
data[:root] = config.root.to_s
|
110
|
+
end
|
111
|
+
if config.branch
|
112
|
+
data[:branch] = config.branch
|
113
|
+
end
|
114
|
+
data
|
115
|
+
end
|
116
|
+
|
117
|
+
def request_data
|
118
|
+
{}
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
data/ratchetio.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/ratchetio/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Brian Rue"]
|
6
|
+
gem.email = ["brian@ratchet.io"]
|
7
|
+
gem.description = %q{Official ruby gem for Ratchet.io}
|
8
|
+
gem.summary = %q{Reports exceptions to Ratchet.io}
|
9
|
+
gem.homepage = "https://ratchet.io/"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "ratchetio"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Ratchetio::VERSION
|
17
|
+
|
18
|
+
#gem.add_development_dependency "rspec", "~> 2.6"
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ratchetio
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Brian Rue
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-27 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Official ruby gem for Ratchet.io
|
15
|
+
email:
|
16
|
+
- brian@ratchet.io
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/ratchetio.rb
|
27
|
+
- lib/ratchetio/configuration.rb
|
28
|
+
- lib/ratchetio/rails.rb
|
29
|
+
- lib/ratchetio/rails/controller_methods.rb
|
30
|
+
- lib/ratchetio/rails/middleware/exception_catcher.rb
|
31
|
+
- lib/ratchetio/railtie.rb
|
32
|
+
- lib/ratchetio/version.rb
|
33
|
+
- ratchetio.gemspec
|
34
|
+
- spec/ratchetio_spec.rb
|
35
|
+
homepage: https://ratchet.io/
|
36
|
+
licenses: []
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.8.24
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: Reports exceptions to Ratchet.io
|
59
|
+
test_files:
|
60
|
+
- spec/ratchetio_spec.rb
|