toadhopper 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << "test"
5
+ t.test_files = FileList['test/test*.rb']
6
+ t.verbose = true
7
+ end
8
+
9
+ begin
10
+ gem "sr-mg", "<= 0.0.5"
11
+ require "mg"
12
+ MG.new("toadhopper.gemspec")
13
+ rescue LoadError
14
+ end
data/Readme.md ADDED
@@ -0,0 +1,19 @@
1
+ Toadhopper
2
+ ----------
3
+
4
+ A base library for [Hoptoad](http://www.hoptoadapp.com/) error reporting.
5
+
6
+ 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
+
8
+ ## Example
9
+
10
+ require 'rubygems'
11
+
12
+ gem 'toadhopper'
13
+ require 'toadhopper'
14
+
15
+ Toadhopper.api_key = "YOURAPIKEY"
16
+
17
+ error = begin; raise "Kaboom!"; rescue => e; e; end
18
+
19
+ puts Toadhopper.post!(error)
@@ -0,0 +1,13 @@
1
+ require File.dirname(__FILE__) + "/../toadhopper"
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
data/lib/toadhopper.rb ADDED
@@ -0,0 +1,78 @@
1
+ require 'net/http'
2
+ require 'yaml'
3
+
4
+ module Toadhopper
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
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
+ ))
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
62
+ end
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
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,28 @@
1
+ require File.dirname(__FILE__) + "/../lib/toadhopper"
2
+
3
+ require 'test/unit'
4
+
5
+ class Toadhopper::TestFilter < Test::Unit::TestCase
6
+ def setup
7
+ Toadhopper.filters = nil
8
+ end
9
+ def test_no_filters
10
+ assert_equal( {:id => "myid", :password => "mypassword"},
11
+ Toadhopper.filter(:id => "myid", :password => "mypassword"))
12
+ end
13
+ def test_string_filter
14
+ Toadhopper.filters = "pass"
15
+ assert_equal( {:id => "myid", :password => "[FILTERED]"},
16
+ Toadhopper.filter(:id => "myid", :password => "mypassword"))
17
+ end
18
+ def test_regex_filter
19
+ Toadhopper.filters = /pas{2}/
20
+ assert_equal( {:id => "myid", :password => "[FILTERED]"},
21
+ Toadhopper.filter(:id => "myid", :password => "mypassword"))
22
+ end
23
+ def test_multiple_filters
24
+ Toadhopper.filters = "email", /pas{2}/
25
+ assert_equal( {:id => "myid", :email => "[FILTERED]", :password => "[FILTERED]"},
26
+ Toadhopper.filter(:id => "myid", :email => "myemail", :password => "mypassword"))
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + "/../lib/toadhopper"
2
+
3
+ require 'test/unit'
4
+
5
+ class Toadhopper::TestNoticeParams < Test::Unit::TestCase
6
+ def test_notice_params
7
+ Toadhopper.api_key = "abc123"
8
+ error = begin; raise "Kaboom!"; rescue => e; e end
9
+ def error.backtrace; ["backtrace line 1", "backtrace line 2"] end
10
+ assert_equal({
11
+ "api_key" => "abc123",
12
+ "error_class" => "RuntimeError",
13
+ "error_message" => "Kaboom!",
14
+ "backtrace" => ["backtrace line 1", "backtrace line 2"],
15
+ "request" => {"request_var" => "request_val"},
16
+ "environment" => {"env_var" => "env_val"},
17
+ "session" => {"session_var" => "session_val"},
18
+ },
19
+ Toadhopper.notice_params(
20
+ error,
21
+ {
22
+ "request" => {"request_var" => "request_val"},
23
+ "environment" => {"env_var" => "env_val"},
24
+ "session" => {"session_var" => "session_val"}
25
+ }
26
+ )
27
+ )
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ require File.dirname(__FILE__) + "/../lib/toadhopper"
2
+
3
+ require 'test/unit'
4
+
5
+ class Toadhopper::TestSetters < Test::Unit::TestCase
6
+ def setup
7
+ Toadhopper.api_key = nil
8
+ Toadhopper.filters = nil
9
+ end
10
+ def test_setting_api_key
11
+ Toadhopper.api_key = "abc123"
12
+ assert_equal "abc123", Toadhopper.api_key
13
+ end
14
+ def test_setting_single_filter
15
+ Toadhopper.filters = /password/
16
+ assert_equal [/password/], Toadhopper.filters
17
+ end
18
+ def test_setting_multple_filters
19
+ Toadhopper.filters = /password/, /email/
20
+ assert_equal [/password/, /email/], Toadhopper.filters
21
+ end
22
+ end
data/test/test_test.rb ADDED
@@ -0,0 +1,12 @@
1
+ require File.dirname(__FILE__) + "/../lib/toadhopper/test"
2
+
3
+ require 'test/unit'
4
+
5
+ class Toadhopper::TestTest < Test::Unit::TestCase
6
+ def test_stub_toadhopper_post!
7
+ # TODO:
8
+ end
9
+ def test_last_toadhopper_post_params
10
+ # TODO:
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: toadhopper
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.3"
5
+ platform: ruby
6
+ authors:
7
+ - Tim Lucas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-11 00:00:00 +10:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Library for posting Hoptoad notifications
17
+ email: t.lucas@toolmantim.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - Readme.md
24
+ files:
25
+ - Readme.md
26
+ - Rakefile
27
+ - lib/toadhopper.rb
28
+ - lib/toadhopper/test.rb
29
+ - test/test_filter.rb
30
+ - test/test_notice_params.rb
31
+ - test/test_setters.rb
32
+ - test/test_test.rb
33
+ has_rdoc: true
34
+ homepage: http://github.com/toolmantim/toadhopper
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project: toadhopper
55
+ rubygems_version: 1.3.1
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: Library for posting Hoptoad notifications
59
+ test_files: []
60
+