Class: Rack::Lilypad::Hoptoad

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/lilypad.rb

Defined Under Namespace

Classes: Backtrace

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, filters = [], log = false) ⇒ Hoptoad

Returns a new instance of Hoptoad.



45
46
47
48
49
# File 'lib/rack/lilypad.rb', line 45

def initialize(api_key, filters=[], log=false)
  @api_key = api_key
  @filters = filters
  @log = log
end

Class Method Details

.last_requestObject



152
153
154
# File 'lib/rack/lilypad.rb', line 152

def last_request
  @@last_request
end

Instance Method Details

#backtrace(exception) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/rack/lilypad.rb', line 51

def backtrace(exception)
  regex = %r{^([^:]+):(\d+)(?::in `([^']+)')?$}
  exception.backtrace.map do |line|
    _, file, number, method = line.match(regex).to_a
    Backtrace.new(file, number, method)
  end
end

#filter(hash) ⇒ Object



59
60
61
62
63
64
# File 'lib/rack/lilypad.rb', line 59

def filter(hash)
  hash.inject({}) do |acc, (key, val)|
    acc[key] = @filters.any? { |f| key.to_s =~ Regexp.new(f) } ? "[FILTERED]" : val
    acc
  end
end

#log(*msg) ⇒ Object



66
67
68
69
# File 'lib/rack/lilypad.rb', line 66

def log(*msg)
  msg = msg.compact.join("\n\n") + "\n\n"
  ::File.open(@log, 'a') { |f| f.write(msg) } if @log
end

#post(exception, env = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rack/lilypad.rb', line 71

def post(exception, env=nil)
  return unless Lilypad.production?
  uri = URI.parse("http://hoptoadapp.com:80/notifier_api/v2/notices")
  Net::HTTP.start(uri.host, uri.port) do |http|
    headers = {
      'Content-type' => 'text/xml',
      'Accept' => 'text/xml, application/xml'
    }
    http.read_timeout = 5 # seconds
    http.open_timeout = 2 # seconds
    response = begin
      http.post uri.path, xml(exception, env), headers
    rescue TimeoutError => e
    end
    case response
    when Net::HTTPSuccess
      env['hoptoad.notified'] = true if env
      log "Hoptoad Success: #{response.class}"
    else
      log "Hoptoad Failure:", (response.body rescue nil), @@last_request
    end
  end
end

#xml(exception, env) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rack/lilypad.rb', line 95

def xml(exception, env)
  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"
  xml.notice(:version => '2.0.0') do |n|
    n.tag! 'api-key', @api_key
    n.notifier do |n|
      n.name 'Lilypad'
      n.url 'http://github.com/winton/lilypad'
      n.version '0.1.7'
    end
    n.error do |e|
      e.tag! 'class', exception.class.name
      e.message exception.message
      e.backtrace do |b|
        backtrace(exception).each do |line|
          b.line(:method => line.method, :file => line.file, :number => line.number)
        end
      end
    end
    n.request do |r|
      r.action environment['rack.lilypad.action']
      r.component environment['rack.lilypad.component'] || request_path
      r.url request_path
      if request && request.params.any?
        r.params do |p|
          request.params.each do |key, value|
            p.var(value.to_s, :key => key)
          end
        end
      end
      if environment.any?
        r.tag!('cgi-data') do |c|
          environment.each do |key, value|
            c.var(value.to_s, :key => key)
          end
        end
      end
    end
    n.tag!('server-environment') do |s|
      s.tag! 'project-root', Dir.pwd
      s.tag! 'environment-name', ENV['RACK_ENV'] || 'development'
    end
  end
  @@last_request = xml.target!
end