Class: HoptoadNotifierClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/hoptoad_notifier_client.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ HoptoadNotifierClient

Returns a new instance of HoptoadNotifierClient.



32
33
34
35
# File 'lib/hoptoad_notifier_client.rb', line 32

def initialize(key)
  raise "API Key required." unless key
  @api_key = key
end

Class Attribute Details

.instancesObject

Returns the value of attribute instances.



12
13
14
# File 'lib/hoptoad_notifier_client.rb', line 12

def instances
  @instances
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



13
14
15
# File 'lib/hoptoad_notifier_client.rb', line 13

def configure
  yield self
end

.create(name) ⇒ Object



22
23
24
# File 'lib/hoptoad_notifier_client.rb', line 22

def self.create(name)
  @instances[name]
end

.notify(error, request = {}, session = {}) ⇒ Object

proxy existing class level calls to original api key



27
28
29
30
# File 'lib/hoptoad_notifier_client.rb', line 27

def self.notify(error, request = {}, session = {})
  raise "No :default project registered, register one to use this call." unless @instances.has_key?(:default)
  @instances[:default].notify(error, request, session)
end

.register(name, key) ⇒ Object



18
19
20
# File 'lib/hoptoad_notifier_client.rb', line 18

def self.register(name, key)
  @instances[name] = HoptoadNotifierClient.new(key)
end

Instance Method Details

#notify(error, request = {}, session = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hoptoad_notifier_client.rb', line 37

def notify(error, request = {}, session = {})
  
  error_type = error.respond_to?(:error_type) ? error.error_type : error.class.name
  error_message = error.respond_to?(:custom_message) ? error.custom_message : "#{error.class.name}: #{error.message}"
  
  data = {'notice' => {
          'api_key' => @api_key,
          'request' => request,
          'error_class' => error_type,
          'error_message' => error_message,
          'backtrace' => error.backtrace ||= [],
          'environment' => ENV.to_hash,
          'session' => session
        }}
  begin
    response = self.class.post "/notices/", 
                    :body => data.to_yaml,
                    :headers => { 
                                  "Content-type" => "application/x-yaml", 
                                  "Accept" => "text/xml, application/xml"
                                }
    
  rescue Exception => e
    puts "Something went wrong, is your API key correct? Exception was #{e}"
    # logger.error "HoptoadNotificationClient FAILED during notification: Exception was [#{e.to_s}]" if logger
  end
end