ht_retro 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ht_retro.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
data/ht_retro.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ gem "hoptoad_notifier"
4
+ require "ht_retro/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "ht_retro"
8
+ s.version = Hoptoad::Retro::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ["Jake Varghese"]
11
+ s.email = ["jake@flvorful.com"]
12
+ s.homepage = ""
13
+ s.summary = %q{HT retro}
14
+ s.description = %q{if you need it you will know :)}
15
+
16
+ s.rubyforge_project = "ht_retro"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ s.add_dependency "rails", "~> 3.0.6"
23
+ s.add_dependency "hoptoad_notifier", "~> 2.3"
24
+ s.add_development_dependency 'rspec', '~> 2.5'
25
+
26
+ end
@@ -0,0 +1,5 @@
1
+ module Hoptoad
2
+ module Retro
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/lib/ht_retro.rb ADDED
@@ -0,0 +1,155 @@
1
+ module HoptoadNotifier
2
+ HEADERS = {
3
+ "X-Hoptoad-Client-Name" =>"Hoptoad Notifier",
4
+ "Content-type" =>"application/x-yaml",
5
+ "X-Hoptoad-Client-Version" =>"1.2.4",
6
+ "Accept" =>"text/xml, application/xml"
7
+ }
8
+
9
+ class Sender
10
+ NOTICES_URI = '/notices/'.freeze
11
+ end
12
+
13
+ class << self
14
+
15
+ def notify_or_ignore(exception, opts = {})
16
+ #debugger
17
+ notice = build_notice_for(exception, opts)
18
+ send_notice(notice) if configuration.public?
19
+ end
20
+
21
+
22
+ private
23
+
24
+ def default_notice_options #:nodoc:
25
+ {
26
+ :api_key => HoptoadNotifier.configuration.api_key,
27
+ :error_message => 'Notification',
28
+ :backtrace => caller,
29
+ :request => {},
30
+ :session => {},
31
+ :environment => ENV.to_hash
32
+ }
33
+ end
34
+
35
+ def send_notice(notice)
36
+ if configuration.public?
37
+ sender.send_to_hoptoad(notice)
38
+ end
39
+ end
40
+
41
+ def build_notice_for(exception, opts = {})
42
+ notice = normalize_notice(exception)
43
+ notice = clean_notice(notice)
44
+ notice = yamlize_notice(notice)
45
+ end
46
+
47
+ def yamlize_notice(notice)
48
+ notice.stringify_keys.to_yaml
49
+ end
50
+
51
+
52
+ def normalize_notice(notice) #:nodoc:
53
+ case notice
54
+ when Hash
55
+ default_notice_options.merge(notice)
56
+ when Exception
57
+ default_notice_options.merge(exception_to_data(notice))
58
+ end
59
+ end
60
+
61
+ def clean_notice(notice)
62
+ notice[:backtrace] = clean_hoptoad_backtrace(notice[:backtrace])
63
+ if notice[:request].is_a?(Hash) && notice[:request][:params].is_a?(Hash)
64
+ notice[:request][:params] = filter_parameters(notice[:request][:params]) if respond_to?(:filter_parameters)
65
+ notice[:request][:params] = clean_hoptoad_params(notice[:request][:params])
66
+ end
67
+ if notice[:environment].is_a?(Hash)
68
+ notice[:environment] = filter_parameters(notice[:environment]) if respond_to?(:filter_parameters)
69
+ notice[:environment] = clean_hoptoad_environment(notice[:environment])
70
+ end
71
+ {:notice => clean_non_serializable_data(notice)}
72
+ end
73
+
74
+ def exception_to_data(exception)
75
+ data = {
76
+ :api_key => HoptoadNotifier.configuration.api_key,
77
+ :error_class => exception.class.name,
78
+ :error_message => "#{exception.class.name}: #{exception.message}",
79
+ :backtrace => exception.backtrace,
80
+ :environment => ENV.to_hash
81
+ }
82
+
83
+ if self.respond_to? :request
84
+ data[:request] = {
85
+ :params => request.parameters.to_hash,
86
+ :rails_root => File.expand_path(RAILS_ROOT),
87
+ :url => "#{request.protocol}#{request.host}#{request.request_uri}"
88
+ }
89
+ data[:environment].merge!(request.env.to_hash)
90
+ end
91
+
92
+ if self.respond_to? :session
93
+ data[:session] = {
94
+ :key => session.instance_variable_get("@session_id"),
95
+ :data => session.respond_to?(:to_hash) ?
96
+ session.to_hash :
97
+ session.instance_variable_get("@data")
98
+ }
99
+ end
100
+
101
+ data
102
+ end
103
+
104
+ def clean_hoptoad_backtrace backtrace #:nodoc:
105
+ if backtrace.to_a.size == 1
106
+ backtrace = backtrace.to_a.first.split(/\n\s*/)
107
+ end
108
+
109
+ filtered = backtrace.to_a.map do |line|
110
+ HoptoadNotifier.configuration.backtrace_filters.inject(line) do |line, proc|
111
+ proc.call(line)
112
+ end
113
+ end
114
+
115
+ filtered.compact
116
+ end
117
+
118
+ def clean_hoptoad_params params #:nodoc:
119
+ params.each do |k, v|
120
+ params[k] = "[FILTERED]" if HoptoadNotifier.configuration.params_filters.any? do |filter|
121
+ k.to_s.match(/#{filter}/)
122
+ end
123
+ end
124
+ end
125
+
126
+ def clean_hoptoad_environment env #:nodoc:
127
+ env.each do |k, v|
128
+ env[k] = "[FILTERED]" if HoptoadNotifier.configuration.environment_filters.any? do |filter|
129
+ k.to_s.match(/#{filter}/)
130
+ end
131
+ end
132
+ end
133
+
134
+ def clean_non_serializable_data(data) #:nodoc:
135
+ case data
136
+ when Hash
137
+ data.inject({}) do |result, (key, value)|
138
+ result.update(key => clean_non_serializable_data(value))
139
+ end
140
+ when Fixnum, Array, String, Bignum
141
+ data
142
+ else
143
+ data.to_s
144
+ end
145
+ end
146
+
147
+ def stringify_keys(hash) #:nodoc:
148
+ hash.inject({}) do |h, pair|
149
+ h[pair.first.to_s] = pair.last.is_a?(Hash) ? stringify_keys(pair.last) : pair.last
150
+ h
151
+ end
152
+ end
153
+ end
154
+ end
155
+
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe HoptoadNotifier do
4
+ describe '.notify_or_ignore' do
5
+
6
+ context 'with an exception' do
7
+ let(:api_url) { HoptoadNotifier::Sender.new(HoptoadNotifier.configuration).instance_eval { url }.to_s }
8
+ let(:build_notice) { HoptoadNotifier.instance_eval { build_notice_for(Exception.new("blah blah")) } }
9
+
10
+
11
+ it 'should send to the old url of /notices/' do
12
+ api_url.should match(/notices\//)
13
+ end
14
+
15
+ it "should not send to the new api url" do
16
+ api_url.should_not match(/notices_api/)
17
+ end
18
+
19
+ it "should send data to url in yaml form" do
20
+ build_notice.should match(/\-\-\-/)
21
+ end
22
+
23
+ it "should not send data xml form" do
24
+ build_notice.should_not match(/^\</)
25
+ end
26
+
27
+ it "should set headers to yaml content type" do
28
+ HoptoadNotifier::HEADERS["Content-type"].should match(/yaml/)
29
+ end
30
+
31
+ it "should not set headers to xml content type" do
32
+ HoptoadNotifier::HEADERS["Content-type"].should_not match(/xml/)
33
+ end
34
+
35
+ it "the root of the notice should be 'notice'" do
36
+ HoptoadNotifier.instance_eval do
37
+ clean_notice(normalize_notice(Exception.new("blah blah")))
38
+ end.keys.should == [:notice]
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+
5
+ require 'hoptoad_retro.rb'
6
+
7
+ RSpec.configure do |config|
8
+ config.color_enabled = true
9
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ht_retro
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jake Varghese
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-13 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 11
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 6
34
+ version: 3.0.6
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: hoptoad_notifier
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 5
46
+ segments:
47
+ - 2
48
+ - 3
49
+ version: "2.3"
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: rspec
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 9
61
+ segments:
62
+ - 2
63
+ - 5
64
+ version: "2.5"
65
+ type: :development
66
+ version_requirements: *id003
67
+ description: if you need it you will know :)
68
+ email:
69
+ - jake@flvorful.com
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ extra_rdoc_files: []
75
+
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - Rakefile
80
+ - ht_retro.gemspec
81
+ - lib/ht_retro.rb
82
+ - lib/ht_retro/version.rb
83
+ - spec/ht_retro_spec.rb
84
+ - spec/spec_helper.rb
85
+ has_rdoc: true
86
+ homepage: ""
87
+ licenses: []
88
+
89
+ post_install_message:
90
+ rdoc_options: []
91
+
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ requirements: []
113
+
114
+ rubyforge_project: ht_retro
115
+ rubygems_version: 1.6.2
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: HT retro
119
+ test_files:
120
+ - spec/ht_retro_spec.rb
121
+ - spec/spec_helper.rb