Module: Debugged

Defined in:
lib/debugged-ruby.rb,
lib/debugged-ruby/version.rb

Constant Summary collapse

VERSION =
'0.2.0'
@@api_key =
nil
@@log =
Logger.new('debugged.txt')

Class Method Summary collapse

Class Method Details

.api_keyObject



25
26
27
28
# File 'lib/debugged-ruby.rb', line 25

def self.api_key
  @@log.debug("API key set")
  @@api_key
end

.api_key=(api_key) ⇒ Object



21
22
23
# File 'lib/debugged-ruby.rb', line 21

def self.api_key=(api_key)
  @@api_key = api_key
end

.logObject



17
18
19
# File 'lib/debugged-ruby.rb', line 17

def self.log
  @@log
end

.report(e, data = nil) ⇒ Object



30
31
32
33
34
35
36
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/debugged-ruby.rb', line 30

def self.report(e, data=nil)

  unless data.is_a? Hash or data.nil?
    @@log.error("ERROR - data argument not passed as a Hash")
    raise ArgumentError, 'Data argument is not Hash'
  end

  unless e.is_a? Exception
    @@log.error("ERROR - exception argument not passed as an Exception")
    raise ArugumentError, 'Exception argument is not Exception'
  end

  data ||= {}

  time = Time.now.utc.to_i
  hash = Digest::SHA1.base64digest @@api_key.to_s+name+"ruby"+time.to_s


  @@log.debug(hash + " - sending report to Debugged")
  report = 
    {
    "created_at" => time.to_i,
    "identifier" => hash,
    "exception_name" => e.class.to_s,
    "sdk_version" => "ruby",
    "stack_trace" => e.backtrace.join("\n"),
    "message" => e.message,
    "line_number" => e.backtrace[0].split(":").last.to_i,
    "character_number" => -1,
    "filename" => e.backtrace[0].split("/").last.split(":").first,
    "user_custom_data" => data.to_json,
    "sdk_data" => {"ruby_version" => RUBY_VERSION.to_s,
      "platform" => RUBY_PLATFORM.to_s}
  }

  url = "http://debugged.herokuapp.com/report"
  uri = URI(url)

  req = Net::HTTP::Post.new(uri.request_uri)
  req.basic_auth @@api_key, 'pass'
  req.body = report.to_json

  pending = 1

  EM.run do
    begin
      http = EM::HttpRequest.new(url).post :body => report.to_json, :head => {'authorization' => [@@api_key, 'pass']}
      http.callback {
        @@log.debug(hash + " - sent report to Debugged")
        pending -= 1
        EM.stop if pending < 1
      }

      http.errback {
        @@log.error(http.error)

        pending -= 1
        EM.stop if pending < 1
      }   
    rescue Exception => e
      @@log.error(e.backtrace)
    end
  end

  "http://www.debugged.herokuapp.com/report/"+hash
end