Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Providing a method for direct access to the Hoptoad API

  • Loading branch information...
commit d4f7012bc4dd349c69e51eaf364a2bb5c1e46667 1 parent 3daa1b8
@winton authored
View
21 README.markdown
@@ -13,13 +13,12 @@ sudo gem install lilypad --source http://gemcutter.org
Rails
-----
-**config/environment.rb**:
+**config/environment.rb**
<pre>
require 'rack/lilypad'
Rails::Initializer.run do |config|
- ENV['RACK_ENV'] = ENV['RAILS_ENV']
config.middleware.insert_after(ActionController::Failsafe, Rack::Lilypad, 'hoptoad_api_key_goes_here')
end
</pre>
@@ -31,6 +30,7 @@ class ApplicationController < ActionController::Base
def rescue_action(exception)
super
+ ENV['RACK_ENV'] = ENV['RAILS_ENV']
request.env['rack.lilypad.component'] = params[:controller]
request.env['rack.lilypad.action'] = params[:action]
raise exception
@@ -61,8 +61,21 @@ use Rack::Lilypad, 'hoptoad_api_key_goes_here' do |hoptoad|
end
</pre>
-Debug
------
+Direct Access
+-------------
+
+Send exceptions to Hoptoad from a rescue block.
+
+<pre>
+begin
+ raise 'Test'
+rescue Exception => e
+ Rack::Lilypad.notify(e)
+end
+</pre>
+
+Log
+---
See what you are sending and receiving from Hoptoad.
View
2  gemspec.rb
@@ -14,5 +14,5 @@
s.name = GEM_NAME
s.platform = Gem::Platform::RUBY
s.require_path = "lib"
- s.version = "0.2.1"
+ s.version = "0.2.2"
end
View
26 lib/rack/lilypad.rb
@@ -14,7 +14,7 @@ def initialize(app, api_key = nil)
@log = false
yield self if block_given?
@filters.flatten!
- @hoptoad = Hoptoad.new(api_key, @filters, @log)
+ @@hoptoad = @hoptoad = Hoptoad.new(api_key, @filters, @log)
end
def call(env)
@@ -28,6 +28,13 @@ def call(env)
@hoptoad.post(env['rack.exception'], env) if env['rack.exception']
[ status, headers, body ]
end
+
+ class <<self
+
+ def notify(e)
+ @@hoptoad.post(e)
+ end
+ end
class Hoptoad
@@ -57,7 +64,7 @@ def log(*msg)
::File.open(@log, 'a') { |f| f.write(msg) } if @log
end
- def post(exception, env)
+ def post(exception, env=nil)
return unless production?
uri = URI.parse("http://hoptoadapp.com:80/notifier_api/v2/notices")
Net::HTTP.start(uri.host, uri.port) do |http|
@@ -73,7 +80,7 @@ def post(exception, env)
end
case response
when Net::HTTPSuccess
- env['hoptoad.notified'] = true
+ env['hoptoad.notified'] = true if env
log "Hoptoad Success: #{response.class}"
else
log "Hoptoad Failure:", (response.body rescue nil), @@last_request
@@ -86,9 +93,14 @@ def production?
end
def xml(exception, env)
- environment = filter(ENV.to_hash.merge(env))
- request = Rack::Request.new(env)
- request_path = request.script_name + request.path_info
+ environment = filter(ENV.to_hash.merge(env || {}))
+ if env
+ request = Rack::Request.new(env)
+ request_path = request.script_name + request.path_info
+ else
+ request = nil
+ request_path = 'internal'
+ end
xml = ::Builder::XmlMarkup.new
xml.instruct! :xml, :version => "1.0", :encoding => "UTF-8"
@@ -112,7 +124,7 @@ def xml(exception, env)
r.action environment['rack.lilypad.action']
r.component environment['rack.lilypad.component'] || request_path
r.url request_path
- if request.params.any?
+ if request && request.params.any?
r.params do |p|
request.params.each do |key, value|
p.var(value.to_s, :key => key)
View
1  spec/fixtures/rails/app/controllers/application_controller.rb
@@ -10,6 +10,7 @@ class ApplicationController < ActionController::Base
def rescue_action(exception)
super
+ ENV['RACK_ENV'] = ENV['RAILS_ENV']
request.env['rack.lilypad.component'] = params[:controller]
request.env['rack.lilypad.action'] = params[:action]
raise exception
View
1  spec/fixtures/rails/config/environment.rb
@@ -39,6 +39,5 @@
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
# config.i18n.default_locale = :de
- ENV['RACK_ENV'] = ENV['RAILS_ENV']
config.middleware.insert_after(ActionController::Failsafe, Rack::Lilypad, 'xxx')
end
View
21 spec/rack/lilypad_spec.rb
@@ -5,7 +5,7 @@
include Rack::Test::Methods
before(:each) do
- ENV['RACK_ENV'] = 'production'
+ ENV['RAILS_ENV'] = ENV['RACK_ENV'] = 'production'
@app = lambda { |env| raise TestError, 'Test' }
@env = Rack::MockRequest.env_for("/pulse")
@http = mock(:http)
@@ -52,16 +52,17 @@
notifier = Rack::Lilypad.new(@app, '')
notifier.call(@env) rescue nil
-
- # Validate XML
- xsd = Nokogiri::XML::Schema(File.read(SPEC + '/fixtures/hoptoad_2_0.xsd'))
- doc = Nokogiri::XML(Rack::Lilypad::Hoptoad.last_request)
-
- errors = xsd.validate(doc)
- errors.each do |error|
- puts error.message
+ validate_xml
+ end
+
+ it "should allow direct access to the post method" do
+ @http.should_receive(:post)
+ begin
+ raise TestError, 'Test'
+ rescue Exception => e
+ Rack::Lilypad.notify(e)
end
- errors.length.should == 0
+ validate_xml
end
describe 'Rails' do
View
11 spec/spec_helper.rb
@@ -23,5 +23,16 @@ def debug(object)
puts "</pre>"
end
+def validate_xml
+ xsd = Nokogiri::XML::Schema(File.read(SPEC + '/fixtures/hoptoad_2_0.xsd'))
+ doc = Nokogiri::XML(Rack::Lilypad::Hoptoad.last_request)
+
+ errors = xsd.validate(doc)
+ errors.each do |error|
+ puts error.message
+ end
+ errors.length.should == 0
+end
+
class TestError < RuntimeError
end
Please sign in to comment.
Something went wrong with that request. Please try again.