lib/toadhopper.rb in toadhopper-0.9.5 vs lib/toadhopper.rb in toadhopper-0.9.7

- old
+ new

@@ -1,13 +1,12 @@ require 'net/http' -require 'haml' -require 'haml/engine' -require 'nokogiri' +require 'erb' +require 'ostruct' # Posts errors to the Hoptoad API class ToadHopper - VERSION = "0.9.5" + VERSION = "0.9.7" # Hoptoad API response class Response < Struct.new(:status, :body, :errors); end attr_reader :api_key @@ -32,11 +31,11 @@ # # @param [Hash] options # @option options [String] url The url for the request, required to post but not useful in a console environment # @option options [String] component Normally this is your Controller name in an MVC framework # @option options [String] action Normally the action for your request in an MVC framework - # @option options [#params] request An object that response to #params and returns a hash + # @option options [Hash] params A hash of the request's parameters # @option options [String] notifier_name Say you're a different notifier than ToadHopper # @option options [String] notifier_version Specify the version of your custom notifier # @option options [String] notifier_url Specify the project URL of your custom notifier # @option options [Hash] session A hash of the user session in a web request # @option options [String] framework_env The framework environment your app is running under @@ -67,44 +66,47 @@ response = http.post uri.path, document, {'Content-type' => 'text/xml', 'Accept' => 'text/xml, application/xml'}.merge(headers) Response.new response.code.to_i, response.body, - Nokogiri::XML.parse(response.body).xpath('//errors/error').map {|e| e.content} + response.body.scan(%r{<error>(.+)<\/error>}).flatten rescue TimeoutError => e Response.new(500, '', ['Timeout error']) end end end # @private def document_for(exception, options={}) - Haml::Engine.new(notice_template, :escape_html => true).render(Object.new, filtered_data(exception, options)) + data = filtered_data(exception, options) + scope = OpenStruct.new(data).extend(ERB::Util) + ERB.new(notice_template, nil, '-').result(scope.send(:binding)) end - def filtered_data(exception, options) + def filtered_data(error, options) defaults = { - :error => exception, + :error => error, :api_key => api_key, :environment => ENV.to_hash, - :backtrace => exception.backtrace.map {|l| backtrace_line(l)}, + :backtrace => error.backtrace.map {|l| backtrace_line(l)}, :url => 'http://localhost/', :component => 'http://localhost/', :action => nil, :request => nil, + :params => nil, :notifier_version => VERSION, :notifier_url => 'http://github.com/toolmantim/toadhopper', :session => {}, :framework_env => ENV['RACK_ENV'] || 'development', :project_root => Dir.pwd }.merge(options) - # Filter session and environment - [:session, :environment].each{|n| defaults[n] = clean(defaults[n]) if defaults[n] } + # Backwards compatibility + defaults[:params] ||= defaults[:request].params if defaults[:request] - # Filter params - defaults[:request].params = clean(defaults[:request].params) if defaults[:request] && defaults[:request].params + # Filter params, session and environment + [:params, :session, :environment].each{|n| defaults[n] = clean(defaults[n]) if defaults[n] } defaults end # @private @@ -112,10 +114,10 @@ Struct.new(:file, :number, :method).new(*line.match(%r{^([^:]+):(\d+)(?::in `([^']+)')?$}).captures) end # @private def notice_template - File.read(::File.join(::File.dirname(__FILE__), 'notice.haml')) + File.read(::File.join(::File.dirname(__FILE__), 'notice.erb')) end # @private def clean(hash) hash.inject({}) do |acc, (k, v)|