toadhopper 0.3 → 0.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.
- data/Readme.md +6 -9
- data/lib/toadhopper/test/methods.rb +19 -0
- data/lib/toadhopper/test.rb +1 -13
- data/lib/toadhopper.rb +68 -68
- metadata +3 -2
data/Readme.md
CHANGED
@@ -1,19 +1,16 @@
|
|
1
|
-
Toadhopper
|
2
|
-
----------
|
3
|
-
|
4
1
|
A base library for [Hoptoad](http://www.hoptoadapp.com/) error reporting.
|
5
2
|
|
6
3
|
Toadhopper can be used to report plain old Ruby exceptions or to build a library specific gem, such as the yet-to-be-built toadhopper-sinatra and toadhopper-rack gems (they're next on my list).
|
7
4
|
|
8
5
|
## Example
|
9
6
|
|
10
|
-
|
7
|
+
require 'rubygems'
|
11
8
|
|
12
|
-
|
13
|
-
|
9
|
+
gem 'toadhopper'
|
10
|
+
require 'toadhopper'
|
14
11
|
|
15
|
-
|
12
|
+
Toadhopper.api_key = "YOURAPIKEY"
|
16
13
|
|
17
|
-
|
14
|
+
error = begin; raise "Kaboom!"; rescue => e; e; end
|
18
15
|
|
19
|
-
|
16
|
+
puts Toadhopper.post!(error)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'toadhopper'
|
2
|
+
|
3
|
+
module Toadhopper
|
4
|
+
module Test
|
5
|
+
# Helper methods for testing Toadhopper posting
|
6
|
+
module Methods
|
7
|
+
# Stub the posting of the error, storing the post params for accessing via
|
8
|
+
# last_toadhopper_post_params
|
9
|
+
def stub_toadhopper_post!
|
10
|
+
def Toadhopper.post!(*args)
|
11
|
+
Toadhopper.instance_variable_set(:@last_post_arguments, args)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
def last_toadhopper_post_arguments
|
15
|
+
Toadhopper.instance_variable_get(:@last_post_arguments)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/toadhopper/test.rb
CHANGED
@@ -1,13 +1 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
# Helper methods for testing Toadhopper posting
|
4
|
-
module Toadhopper::Test
|
5
|
-
# Stub the posting of the error, storing the post params for accessing via
|
6
|
-
# last_toadhopper_post_params
|
7
|
-
def stub_toadhopper_post!
|
8
|
-
# TODO:
|
9
|
-
end
|
10
|
-
def last_toadhopper_post_params
|
11
|
-
# TODO:
|
12
|
-
end
|
13
|
-
end
|
1
|
+
require 'toadhopper/test/methods'
|
data/lib/toadhopper.rb
CHANGED
@@ -2,77 +2,77 @@ require 'net/http'
|
|
2
2
|
require 'yaml'
|
3
3
|
|
4
4
|
module Toadhopper
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
30
|
-
end
|
31
|
-
# Posts an error to Hoptoad
|
32
|
-
def post!(error, options={}, header_options={})
|
33
|
-
uri = URI.parse("http://hoptoadapp.com/notices/")
|
34
|
-
Net::HTTP.start(uri.host, uri.port) do |http|
|
35
|
-
headers = {
|
36
|
-
'Content-type' => 'application/x-yaml',
|
37
|
-
'Accept' => 'text/xml, application/xml',
|
38
|
-
'X-Hoptoad-Client-Name' => 'Toadhopper',
|
39
|
-
}.merge(header_options)
|
40
|
-
http.read_timeout = 5 # seconds
|
41
|
-
http.open_timeout = 2 # seconds
|
42
|
-
begin
|
43
|
-
http.post uri.path, {"notice" => notice_params(error, options)}.to_yaml, headers
|
44
|
-
rescue TimeoutError => e
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
def notice_params(error, options={}) # :nodoc:
|
49
|
-
clean_non_serializable_data(stringify_keys(
|
50
|
-
{
|
51
|
-
:api_key => api_key,
|
52
|
-
:error_class => error.class.name,
|
53
|
-
:error_message => error.message,
|
54
|
-
:backtrace => error.backtrace,
|
55
|
-
}.merge(options)
|
56
|
-
))
|
5
|
+
class << self
|
6
|
+
# Set the API key
|
7
|
+
def api_key=(key)
|
8
|
+
@@api_key = key
|
9
|
+
end
|
10
|
+
# Returns the key set by Toadhopper.api_key=
|
11
|
+
def api_key
|
12
|
+
@@api_key
|
13
|
+
end
|
14
|
+
# Sets patterns to [FILTER] out sensitive data such as passwords, emails and credit card numbers.
|
15
|
+
#
|
16
|
+
# Toadhopper.filters = /password/, /email/, /credit_card_number/
|
17
|
+
def filters=(*filters)
|
18
|
+
@@filters = filters.flatten
|
19
|
+
end
|
20
|
+
# Returns the filters set by Toadhopper.filters=
|
21
|
+
def filters
|
22
|
+
[@@filters].flatten.compact
|
23
|
+
end
|
24
|
+
# Replaces the values of the keys matching Toadhopper.filters with [FILTERED]. Typically used on the params and environment hashes.
|
25
|
+
def filter(hash)
|
26
|
+
hash.inject({}) do |acc, (key, val)|
|
27
|
+
acc[key] = filters.any? {|f| key.to_s =~ Regexp.new(f)} ? "[FILTERED]" : val
|
28
|
+
acc
|
57
29
|
end
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
30
|
+
end
|
31
|
+
# Posts an error to Hoptoad
|
32
|
+
def post!(error, options={}, header_options={})
|
33
|
+
uri = URI.parse("http://hoptoadapp.com/notices/")
|
34
|
+
Net::HTTP.start(uri.host, uri.port) do |http|
|
35
|
+
headers = {
|
36
|
+
'Content-type' => 'application/x-yaml',
|
37
|
+
'Accept' => 'text/xml, application/xml',
|
38
|
+
'X-Hoptoad-Client-Name' => 'Toadhopper',
|
39
|
+
}.merge(header_options)
|
40
|
+
http.read_timeout = 5 # seconds
|
41
|
+
http.open_timeout = 2 # seconds
|
42
|
+
begin
|
43
|
+
http.post uri.path, {"notice" => notice_params(error, options)}.to_yaml, headers
|
44
|
+
rescue TimeoutError => e
|
62
45
|
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
def notice_params(error, options={}) # :nodoc:
|
49
|
+
clean_non_serializable_data(stringify_keys(
|
50
|
+
{
|
51
|
+
:api_key => api_key,
|
52
|
+
:error_class => error.class.name,
|
53
|
+
:error_message => error.message,
|
54
|
+
:backtrace => error.backtrace,
|
55
|
+
}.merge(options)
|
56
|
+
))
|
57
|
+
end
|
58
|
+
def stringify_keys(hash) #:nodoc:
|
59
|
+
hash.inject({}) do |h, pair|
|
60
|
+
h[pair.first.to_s] = pair.last.is_a?(Hash) ? stringify_keys(pair.last) : pair.last
|
61
|
+
h
|
63
62
|
end
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
63
|
+
end
|
64
|
+
def serializable?(value) #:nodoc:
|
65
|
+
value.is_a?(Fixnum) ||
|
66
|
+
value.is_a?(Array) ||
|
67
|
+
value.is_a?(String) ||
|
68
|
+
value.is_a?(Hash) ||
|
69
|
+
value.is_a?(Bignum)
|
70
|
+
end
|
71
|
+
def clean_non_serializable_data(data) #:nodoc:
|
72
|
+
data.select{|k,v| serializable?(v) }.inject({}) do |h, pair|
|
73
|
+
h[pair.first] = pair.last.is_a?(Hash) ? clean_non_serializable_data(pair.last) : pair.last
|
74
|
+
h
|
76
75
|
end
|
77
76
|
end
|
77
|
+
end
|
78
78
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: toadhopper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "0.
|
4
|
+
version: "0.4"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tim Lucas
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-09-
|
12
|
+
date: 2009-09-16 00:00:00 +10:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -26,6 +26,7 @@ files:
|
|
26
26
|
- Rakefile
|
27
27
|
- lib/toadhopper.rb
|
28
28
|
- lib/toadhopper/test.rb
|
29
|
+
- lib/toadhopper/test/methods.rb
|
29
30
|
- test/test_filter.rb
|
30
31
|
- test/test_notice_params.rb
|
31
32
|
- test/test_setters.rb
|