toolmantim-toadhopper 0.2

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/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 reporting.
5
+
6
+ You can use this 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 (that's 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)
data/lib/toadhopper.rb ADDED
@@ -0,0 +1,81 @@
1
+ require 'net/http'
2
+ require 'yaml'
3
+ require 'timeout'
4
+
5
+ module Toadhopper
6
+ class PostTimeoutError < StandardError; end
7
+ class << self
8
+ # Set the API key
9
+ def api_key=(key)
10
+ @@api_key = key
11
+ end
12
+ # Returns the key set by Toadhopper.api_key=
13
+ def api_key
14
+ @@api_key
15
+ end
16
+ # Sets patterns to [FILTER] out sensitive data such as passwords, emails and credit card numbers.
17
+ #
18
+ # Toadhopper.filters = /password/, /email/, /credit_card_number/
19
+ def filters=(*filters)
20
+ @@filters = filters.flatten
21
+ end
22
+ # Returns the filters set by Toadhopper.filters=
23
+ def filters
24
+ [@@filters].flatten.compact
25
+ end
26
+ # Replaces the values of the keys matching Toadhopper.filters with [FILTERED]. Typically used on the params and environment hashes.
27
+ def filter(hash)
28
+ hash.inject({}) do |acc, (key, val)|
29
+ acc[key] = filters.any? {|f| key.to_s =~ Regexp.new(f)} ? "[FILTERED]" : val
30
+ acc
31
+ end
32
+ end
33
+ # Posts an error to Hoptoad
34
+ def post!(error, options={}, header_options={})
35
+ uri = URI.parse("http://hoptoadapp.com/notices/")
36
+ Net::HTTP.start(uri.host, uri.port) do |http|
37
+ headers = {
38
+ 'Content-type' => 'application/x-yaml',
39
+ 'Accept' => 'text/xml, application/xml',
40
+ 'X-Hoptoad-Client-Name' => 'Toadhopper',
41
+ }.merge(header_options)
42
+ http.read_timeout = 5 # seconds
43
+ http.open_timeout = 2 # seconds
44
+ begin
45
+ http.post uri.path, {"notice" => notice_params(error, options)}.to_yaml, headers
46
+ rescue Timeout::TimeoutError => e
47
+ raise ToadHopper::PostTimeoutError
48
+ end
49
+ end
50
+ end
51
+ def notice_params(error, options={}) # :nodoc:
52
+ clean_non_serializable_data(stringify_keys(
53
+ {
54
+ :api_key => api_key,
55
+ :error_class => error.class.name,
56
+ :error_message => error.message,
57
+ :backtrace => error.backtrace,
58
+ }.merge(options)
59
+ ))
60
+ end
61
+ def stringify_keys(hash) #:nodoc:
62
+ hash.inject({}) do |h, pair|
63
+ h[pair.first.to_s] = pair.last.is_a?(Hash) ? stringify_keys(pair.last) : pair.last
64
+ h
65
+ end
66
+ end
67
+ def serializable?(value) #:nodoc:
68
+ value.is_a?(Fixnum) ||
69
+ value.is_a?(Array) ||
70
+ value.is_a?(String) ||
71
+ value.is_a?(Hash) ||
72
+ value.is_a?(Bignum)
73
+ end
74
+ def clean_non_serializable_data(data) #:nodoc:
75
+ data.select{|k,v| serializable?(v) }.inject({}) do |h, pair|
76
+ h[pair.first] = pair.last.is_a?(Hash) ? clean_non_serializable_data(pair.last) : pair.last
77
+ h
78
+ end
79
+ end
80
+ end
81
+ end
@@ -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
@@ -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: toolmantim-toadhopper
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.2"
5
+ platform: ruby
6
+ authors:
7
+ - Tim Lucas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07: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:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: Library for posting Hoptoad notifications
59
+ test_files: []
60
+