crashdesk 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+ require 'rspec/core/rake_task'
6
+
7
+ Bundler::GemHelper.install_tasks
8
+
9
+ desc 'Default: run specs.'
10
+ task :default => :spec
11
+
12
+ desc "Run specs"
13
+ RSpec::Core::RakeTask.new do |t|
14
+ t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
15
+ # Put spec opts in a file named .rspec in root
16
+ end
17
+
18
+ desc "Run tests with SimpleCov"
19
+ RSpec::Core::RakeTask.new('coverage') do |t|
20
+ ENV['COVERAGE'] = "true"
21
+ end
data/crashdesk.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
8
8
  gem.summary = "Crashde.sk is helping you track crashes of your application online"
9
9
  gem.description = "crashdesk is the Ruby gem for containing core functionality for communication with crashde.sk servers"
10
10
  gem.email = "info@crashde.sk"
11
- gem.files = Dir['lib/**/*'] + Dir['spec/**/*'] + Dir['*.rb'] + ["crashdesk.gemspec"]
11
+ gem.files = Dir['lib/**/*'] + Dir['spec/**/*'] + Dir['*.rb'] + ["crashdesk.gemspec", 'Rakefile']
12
12
  gem.homepage = "http://crashde.sk"
13
13
  gem.require_paths = ["lib"]
14
14
  gem.rubyforge_project = "crashdesk"
data/lib/crashdesk.rb CHANGED
@@ -10,8 +10,9 @@ require 'crashdesk/context_base'
10
10
  require 'crashdesk/crashlog'
11
11
  require 'crashdesk/backtrace'
12
12
  require 'crashdesk/environment'
13
+ require 'crashdesk/report_manager'
13
14
  require 'crashdesk/reporters/remote'
14
- require 'crashdesk/serializers/json'
15
+ require 'crashdesk/reporters/logger'
15
16
 
16
17
  module Crashdesk
17
18
 
@@ -37,8 +38,8 @@ module Crashdesk
37
38
  @configuration ||= Configuration.new
38
39
  end
39
40
 
40
- def log(message)
41
- configuration.logger.info(LOG_PREFIX + message) if configuration.logger
41
+ def log(message, severity = :info)
42
+ configuration.logger.send(severity, LOG_PREFIX + message) if configuration.logger
42
43
  end
43
44
 
44
45
  # Main method how to build crashlog from exception, context, and environment
@@ -26,7 +26,7 @@ module Crashdesk
26
26
  attr_accessor :environment_name, :project_root
27
27
 
28
28
  def initialize
29
- @host = 'crashde.sk'
29
+ @host = 'crashde.sk'
30
30
  end
31
31
 
32
32
  # Hash like access
@@ -42,6 +42,11 @@ module Crashdesk
42
42
  'http'
43
43
  end
44
44
 
45
+ def reporters
46
+ reporters = [:remote]
47
+ reporters << :logger if logger
48
+ end
49
+
45
50
  private
46
51
 
47
52
  def default_port
@@ -4,7 +4,7 @@ module Crashdesk
4
4
 
5
5
  attr_accessor :options, :exception, :backtrace, :environment, :exception_class,
6
6
  :exception_message, :context, :occurred_at,
7
- :reporter
7
+ :reporters
8
8
 
9
9
  def initialize(exception, request, context, options = {})
10
10
  self.options = options
@@ -18,16 +18,14 @@ module Crashdesk
18
18
  # Environment
19
19
  self.environment = Crashdesk::Environment.new(
20
20
  :environment_name => options[:environment_name],
21
- :project_root => Crashdesk.configure.project_root)
21
+ :project_root => config.project_root)
22
22
 
23
23
  # Context
24
24
  self.context = context
25
- self.occurred_at = Time.now.utc.iso8601
25
+ self.occured_at = Time.now.utc.iso8601
26
26
 
27
27
  # How to report?
28
- self.reporter = options[:reporter] ||
29
- Crashdesk::Reporter::Remote.new(:host => 'localhost',
30
- :port => 4567) # Need to pass HTTP params
28
+ self.reporters = options[:reporters] || config.reporters
31
29
  end
32
30
 
33
31
  def to_hash
@@ -35,7 +33,7 @@ module Crashdesk
35
33
  :api_key => Crashdesk.configuration.api_key,
36
34
  :hash_id => backtrace.hash_id,
37
35
  :crc => backtrace.crc,
38
- :occurred_at => occurred_at,
36
+ :occured_at => occured_at,
39
37
 
40
38
  :environment => environment.to_hash,
41
39
 
@@ -48,14 +46,20 @@ module Crashdesk
48
46
  end
49
47
 
50
48
  def report
51
- json = Crashdesk::Serializer::Json.new(self.to_hash).process
52
- reporter.run(json)
49
+ report_manager = Crashdesk::ReportManager.new(reporters)
50
+ report_manager.process(self)
53
51
  end
54
52
 
55
53
  def crc
56
54
  session_data.crc
57
55
  end
58
56
 
57
+ private
58
+
59
+ def config
60
+ Crashdesk.configuration
61
+ end
62
+
59
63
  end
60
64
 
61
65
  end
@@ -0,0 +1,42 @@
1
+ module Crashdesk
2
+ class ReportManager
3
+
4
+ attr_reader :reporters
5
+
6
+ def initialize(reporters)
7
+ @reporters = reporters.map do |reporter|
8
+ if reporter.respond_to? :run
9
+ reporter
10
+ else
11
+ constantize(reporter).new
12
+ end
13
+ end
14
+ end
15
+
16
+ def process(crashlog)
17
+ reporters.map do |reporter|
18
+ reporter.run(crashlog)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def constantize(reporter_name, prefix = "::Crashdesk::Reporters::")
25
+ klass = prefix + klass_name(reporter_name)
26
+ names = klass.split('::')
27
+ names.shift if names.empty? || names.first.empty?
28
+
29
+
30
+ constant = Object
31
+ names.each do |name|
32
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
33
+ end
34
+ constant
35
+ end
36
+
37
+ def klass_name(k)
38
+ k.to_s.split('_').collect!{ |w| w.capitalize }.join
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,13 @@
1
+ module Crashdesk
2
+ module Reporters
3
+ class Logger
4
+
5
+ def run(crashlog)
6
+ require 'pp'
7
+ hash = crashlog.to_hash
8
+ Crashdesk.log(pp(hash))
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -2,7 +2,7 @@ require 'net/http'
2
2
  require 'net/https'
3
3
 
4
4
  module Crashdesk
5
- module Reporter
5
+ module Reporters
6
6
 
7
7
  module QueryParams
8
8
  def self.encode(value, key = nil)
@@ -11,7 +11,7 @@ module Crashdesk
11
11
  when Array then value.map { |v| encode(v, "#{key}[]") }.join('&')
12
12
  when nil then ''
13
13
  else
14
- "#{key}=#{CGI.escape(value.to_s)}"
14
+ "#{key}=#{CGI.escape(value.to_s)}"
15
15
  end
16
16
  end
17
17
 
@@ -53,25 +53,36 @@ module Crashdesk
53
53
  end
54
54
  end
55
55
 
56
- def run(data)
56
+ def run(crashlog)
57
+ hash = crashlog.to_hash
58
+ unless hash.respond_to? :to_json
59
+ require 'json'
60
+ unless hash.respond_to? :to_json
61
+ raise StandardError.new("You need a json gem/library installed to send errors to as JSON (Object.to_json not defined).
62
+ \nInstall json_pure, yajl-ruby, json-jruby, or the c-based json gem")
63
+ end
64
+ end
65
+ data = hash.to_json
66
+
57
67
  http = setup_http_connection
58
68
  headers = HEADERS.merge('X-Crashdesk-ApiKey' => Crashdesk.configuration.api_key)
59
69
 
60
70
  response = begin
71
+ log "Sending crash report to #{url} with data: #{data}"
61
72
  http.post(url.path, data, headers)
62
73
  rescue *HTTP_ERRORS => e
63
- logger.error "Unable to connect the Creashdesk server. HTTP Error=#{e}"
74
+ log "Unable to connect the Creashdesk server. HTTP Error=#{e}", :error
64
75
  nil
65
76
  end
66
77
 
67
78
  case response
68
79
  when Net::HTTPSuccess then
69
- logger.info "Success: #{response.class} #{response}"
80
+ log "Success: #{response.class} #{response}"
70
81
  else
71
- logger.info "Failure: #{response.class} #{response}"
82
+ log "Failure: #{response.class} #{response}"
72
83
  end
73
84
  rescue => e
74
- logger.error "Error sending: #{e.class} - #{e.message}\nBacktrace:\n#{e.backtrace.join("\n\t")}"
85
+ log "Error sending: #{e.class} - #{e.message}\nBacktrace:\n#{e.backtrace.join("\n\t")}", :error
75
86
  nil
76
87
  end
77
88
 
@@ -92,8 +103,8 @@ module Crashdesk
92
103
 
93
104
  private
94
105
 
95
- def logger
96
- Crashdesk.logger
106
+ def log(message, severity = :info)
107
+ Crashdesk.log(message, severity)
97
108
  end
98
109
 
99
110
  def url
@@ -1,11 +1,8 @@
1
1
  module Crashdesk
2
- module Reporter
2
+ module Reporters
3
3
 
4
4
  class Screen
5
5
 
6
- def initialize()
7
- end
8
-
9
6
  def run(data)
10
7
  data.to_json.to_s
11
8
  end
@@ -1,3 +1,3 @@
1
1
  module Crashdesk
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Crashdesk::ReportManager do
4
+
5
+ context "with predefined reporters" do
6
+ before(:all) do
7
+ crashlog_class = class MyCrashlog
8
+ def to_hash
9
+ {:name => 1, :string => 2}
10
+ end
11
+ end
12
+ class ::Crashdesk::Reporters::Echo
13
+ def run(crashlog)
14
+ crashlog.to_hash
15
+ end
16
+ end
17
+ end
18
+
19
+ before do
20
+ @crashlog = MyCrashlog.new
21
+ end
22
+
23
+ it "and with Logger should print it with PP" do
24
+ Crashdesk.should_receive(:log).with({:name => 1, :string => 2})
25
+ PP.should_receive(:pp).with(name: 1, string: 2)
26
+ @report_manager = Crashdesk::ReportManager.new([:logger])
27
+ @report_manager.process(@crashlog)
28
+ end
29
+
30
+ it "and with Foo logger should return echo and also with logger call PP" do
31
+ @report_manager = Crashdesk::ReportManager.new([:logger, :echo])
32
+ PP.should_receive(:pp).with(name: 1, string: 2)
33
+ @report_manager.process(@crashlog).should eq([nil, {name:1,string:2}])
34
+ end
35
+
36
+ it "must accept also constants as argument" do
37
+ @report_manager = Crashdesk::ReportManager.new([:logger, ::Crashdesk::Reporters::Echo.new])
38
+ PP.should_receive(:pp).with(name: 1, string: 2)
39
+ @report_manager.process(@crashlog).should eq([nil, {name:1,string:2}])
40
+ end
41
+
42
+ end
43
+
44
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crashdesk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-26 00:00:00.000000000 Z
12
+ date: 2012-09-03 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: crashdesk is the Ruby gem for containing core functionality for communication
15
15
  with crashde.sk servers
@@ -23,16 +23,19 @@ files:
23
23
  - lib/crashdesk/context_base.rb
24
24
  - lib/crashdesk/crashlog.rb
25
25
  - lib/crashdesk/environment.rb
26
+ - lib/crashdesk/report_manager.rb
27
+ - lib/crashdesk/reporters/logger.rb
26
28
  - lib/crashdesk/reporters/remote.rb
27
29
  - lib/crashdesk/reporters/screen.rb
28
- - lib/crashdesk/serializers/json.rb
29
30
  - lib/crashdesk/version.rb
30
31
  - lib/crashdesk.rb
31
32
  - spec/backtrace_spec.rb
32
33
  - spec/configuration_spec.rb
34
+ - spec/report_manager_spec.rb
33
35
  - spec/spec_helper.rb
34
36
  - spec/stubs/backtrace_basic.txt
35
37
  - crashdesk.gemspec
38
+ - Rakefile
36
39
  homepage: http://crashde.sk
37
40
  licenses: []
38
41
  post_install_message:
@@ -1,25 +0,0 @@
1
- module Crashdesk
2
- module Serializer
3
-
4
- class Json
5
-
6
- def initialize(data)
7
- unless data.respond_to? :to_json
8
- require 'json'
9
- unless data.respond_to? :to_json
10
- raise StandardError.new("You need a json gem/library installed to send errors to as JSON (Object.to_json not defined).
11
- \nInstall json_pure, yajl-ruby, json-jruby, or the c-based json gem")
12
- end
13
- end
14
-
15
- @data = data
16
- end
17
-
18
- def process
19
- @data.to_json
20
- end
21
-
22
- end
23
-
24
- end
25
- end