Class: Crashdesk::Reporters::Remote

Inherits:
Object
  • Object
show all
Defined in:
lib/crashdesk/reporters/remote.rb

Constant Summary collapse

NOTICES_URI =
'/v1/crashes'.freeze
HTTP_ERRORS =
[Timeout::Error,
Errno::EINVAL,
Errno::ECONNRESET,
EOFError,
Net::HTTPBadResponse,
Net::HTTPHeaderSyntaxError,
Net::ProtocolError,
Errno::ECONNREFUSED].freeze
HEADERS =
{
  'Content-type' => 'application/json',
  'Accept'       => 'application/json'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Remote

Returns a new instance of Remote.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/crashdesk/reporters/remote.rb', line 39

def initialize(options = {})
  [ :proxy_host,
    :proxy_port,
    :proxy_user,
    :proxy_pass,
    :protocol,
    :host,
    :port,
    :secure,
    :use_system_ssl_cert_chain,
    :http_open_timeout,
    :http_read_timeout
  ].each do |option|
    instance_variable_set("@#{option}", options[option])
  end
end

Instance Attribute Details

#http_open_timeoutObject (readonly)

Returns the value of attribute http_open_timeout.



89
90
91
# File 'lib/crashdesk/reporters/remote.rb', line 89

def http_open_timeout
  @http_open_timeout
end

#http_read_timeoutObject (readonly)

Returns the value of attribute http_read_timeout.



89
90
91
# File 'lib/crashdesk/reporters/remote.rb', line 89

def http_read_timeout
  @http_read_timeout
end

#proxy_hostObject (readonly)

Returns the value of attribute proxy_host.



89
90
91
# File 'lib/crashdesk/reporters/remote.rb', line 89

def proxy_host
  @proxy_host
end

#proxy_passObject (readonly)

Returns the value of attribute proxy_pass.



89
90
91
# File 'lib/crashdesk/reporters/remote.rb', line 89

def proxy_pass
  @proxy_pass
end

#proxy_portObject (readonly)

Returns the value of attribute proxy_port.



89
90
91
# File 'lib/crashdesk/reporters/remote.rb', line 89

def proxy_port
  @proxy_port
end

#proxy_userObject (readonly)

Returns the value of attribute proxy_user.



89
90
91
# File 'lib/crashdesk/reporters/remote.rb', line 89

def proxy_user
  @proxy_user
end

#secureObject (readonly) Also known as: secure?

Returns the value of attribute secure.



89
90
91
# File 'lib/crashdesk/reporters/remote.rb', line 89

def secure
  @secure
end

#use_system_ssl_cert_chainObject (readonly) Also known as: use_system_ssl_cert_chain?

Returns the value of attribute use_system_ssl_cert_chain.



89
90
91
# File 'lib/crashdesk/reporters/remote.rb', line 89

def use_system_ssl_cert_chain
  @use_system_ssl_cert_chain
end

Instance Method Details

#run(crashlog) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/crashdesk/reporters/remote.rb', line 56

def run(crashlog)
  hash = crashlog.to_hash
  unless hash.respond_to? :to_json
    require 'json'
    unless hash.respond_to? :to_json
      raise StandardError.new("You need a json gem/library installed to send errors to as JSON (Object.to_json not defined).
                              \nInstall json_pure, yajl-ruby, json-jruby, or the c-based json gem")
    end
  end
  data = hash.to_json

  http = setup_http_connection
  headers = HEADERS.merge('X-Crashdesk-AppKey' => Crashdesk.configuration.app_key)

  response = begin
              log "Sending crash report to #{url} with data: #{data}"
              http.post(url.path, data, headers)
            rescue *HTTP_ERRORS => e
              log "Unable to connect the Creashdesk server. HTTP Error=#{e}", :error
              nil
            end

  case response
  when Net::HTTPSuccess then
    log "Success: #{response.class} #{response}"
  else
    log "Failure: #{response.class} #{response}"
  end
rescue => e
  log "Error sending: #{e.class} - #{e.message}\nBacktrace:\n#{e.backtrace.join("\n\t")}", :error
  nil
end