comsat 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -68,7 +68,7 @@ Or install it yourself as:
68
68
 
69
69
  ## Usage
70
70
 
71
- ```
71
+ ```ruby
72
72
  client = Comsat::Client.new
73
73
  client.create_route("notify_on_exception", "notice", ["campfire://<api_key>:X@blossom.campfirenow.com/Test%20Room"])
74
74
  client.notify("notify_on_exception", {
@@ -78,6 +78,22 @@ client.notify("notify_on_exception", {
78
78
  })
79
79
  ```
80
80
 
81
+ You can also instrument Comsat with your favorite logger (which should be
82
+ Scrolls by now :)):
83
+
84
+ ```ruby
85
+ require "scrolls"
86
+ require "comsat"
87
+
88
+ module MyLogger
89
+ def self.log(data, &blk)
90
+ Scrolls.log(data, &blk)
91
+ end
92
+ end
93
+
94
+ Comsat.instrument_with(MyLogger.method(:log))
95
+ ```
96
+
81
97
  ## Contributing
82
98
 
83
99
  1. Fork it
@@ -0,0 +1,59 @@
1
+ module Comsat
2
+ class Client
3
+ attr_accessor :routes
4
+
5
+ def initialize
6
+ @@routes = []
7
+ end
8
+
9
+ def routes
10
+ @@routes
11
+ end
12
+
13
+ def create_route(route, event_type, services)
14
+ start = Time.now
15
+ Comsat.log(:fn => :create_route, :at => :start)
16
+ unless routes.detect {|r| r.name == route }
17
+ routes << Route.new(route, event_type, services)
18
+ end
19
+ Comsat.log(:fn => :create_route, :at => :finish, :elapsed => Time.now - start)
20
+ end
21
+
22
+ def notify(route, message={})
23
+ start = Time.now
24
+ Comsat.log(:fn => :notify, :at => :start)
25
+ notify_route = @@routes.detect {|r| r.name == route } if message
26
+ event = notify_route.event_type
27
+ notify_route.services.each do |svc|
28
+ Comsat.log(:fn => :notify, :service => "#{svc.class.to_s.downcase}", :event => event)
29
+ svc.send("send_#{event}".to_sym, message)
30
+ end
31
+ Comsat.log(:fn => :notify, :at => :finish, :elapsed => Time.now - start)
32
+ end
33
+
34
+ def send_notice(data)
35
+ send_event(:notice, data)
36
+ end
37
+
38
+ def send_alert(data)
39
+ send_event(:alert, data)
40
+ end
41
+
42
+ def send_resolve(data)
43
+ send_event(:resolve, data)
44
+ end
45
+
46
+ private
47
+
48
+ def send_event(event_type, data)
49
+ @urls.each do |url|
50
+ service = ServiceFactory.create(url)
51
+ if service.respond_to?("send_#{event_type}")
52
+ service.send("send_#{event_type}".to_sym, data)
53
+ else
54
+ next
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -12,4 +12,14 @@ module Comsat
12
12
  end
13
13
  end
14
14
  end
15
+
16
+ class ServiceFactory
17
+ def self.create(url)
18
+ svc_name = URI.parse(url).scheme
19
+ if Comsat.const_defined?(svc_name.capitalize)
20
+ svc = Comsat.const_get(svc_name.capitalize)
21
+ svc.new(url)
22
+ end
23
+ end
24
+ end
15
25
  end
@@ -1,3 +1,3 @@
1
1
  module Comsat
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/comsat.rb CHANGED
@@ -1,9 +1,8 @@
1
1
  require "cgi"
2
2
  require "json"
3
3
  require "rest_client"
4
- require "scrolls"
5
4
 
6
- require "comsat/log"
5
+ require "comsat/client"
7
6
  require "comsat/route"
8
7
  require "comsat/service"
9
8
  require "comsat/version"
@@ -14,65 +13,20 @@ require "comsat/services/campfire"
14
13
  require "comsat/services/pagerduty"
15
14
 
16
15
  module Comsat
17
- class Client
18
16
 
19
- attr_accessor :routes
20
-
21
- def initialize
22
- @@routes = []
23
- end
24
-
25
- def routes
26
- @@routes
27
- end
28
-
29
- def create_route(route, event_type, services)
30
- unless routes.detect {|r| r.name == route }
31
- routes << Route.new(route, event_type, services)
32
- end
33
- end
34
-
35
- def notify(route, message={})
36
- notify_route = @@routes.detect {|r| r.name == route } if message
37
- event = notify_route.event_type
38
- notify_route.services.each do |svc|
39
- svc.send("send_#{event}".to_sym, message)
40
- end
41
- end
42
-
43
- def send_notice(data)
44
- send_event(:notice, data)
45
- end
46
-
47
- def send_alert(data)
48
- send_event(:alert, data)
49
- end
50
-
51
- def send_resolve(data)
52
- send_event(:resolve, data)
53
- end
17
+ def self.merge(data1, data2)
18
+ data1.merge(data2)
19
+ end
54
20
 
55
- private
21
+ def self.instrument_with(logger)
22
+ @logger = logger
23
+ end
56
24
 
57
- def send_event(event_type, data)
58
- @urls.each do |url|
59
- service = ServiceFactory.create(url)
60
- if service.respond_to?("send_#{event_type}")
61
- service.send("send_#{event_type}".to_sym, data)
62
- else
63
- next
64
- end
65
- end
66
- end
25
+ def self.logger
26
+ @logger || STDOUT.method(:puts)
67
27
  end
68
28
 
69
- class ServiceFactory
70
- def self.create(url)
71
- svc_name = URI.parse(url).scheme
72
- if Comsat.const_defined?(svc_name.capitalize)
73
- svc = Comsat.const_get(svc_name.capitalize)
74
- svc.new(url)
75
- end
76
- end
29
+ def self.log(data, &blk)
30
+ logger.call({:lib => :comsat}.merge(data), &blk)
77
31
  end
78
32
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,18 @@
1
1
  $: << File.expand_path("../../lib", __FILE__)
2
2
 
3
3
  require 'comsat'
4
+ require 'scrolls'
5
+ require 'stringio'
6
+
7
+ Scrolls::Log.start(StringIO.new)
8
+
9
+ module TestLogger
10
+ def self.log(data, &blk)
11
+ Scrolls.log(data, &blk)
12
+ end
13
+ end
14
+
15
+ Comsat.instrument_with(TestLogger.method(:log))
4
16
 
5
17
  RSpec.configure do |c|
6
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: comsat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-04-12 00:00:00.000000000 Z
13
+ date: 2012-04-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rest-client
17
- requirement: &70177295796840 !ruby/object:Gem::Requirement
17
+ requirement: &70103077796900 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70177295796840
25
+ version_requirements: *70103077796900
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: scrolls
28
- requirement: &70177295796100 !ruby/object:Gem::Requirement
28
+ requirement: &70103077794340 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *70177295796100
36
+ version_requirements: *70103077794340
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rake
39
- requirement: &70177295795660 !ruby/object:Gem::Requirement
39
+ requirement: &70103077792600 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70177295795660
47
+ version_requirements: *70103077792600
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rspec
50
- requirement: &70177295795180 !ruby/object:Gem::Requirement
50
+ requirement: &70103077791160 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *70177295795180
58
+ version_requirements: *70103077791160
59
59
  description: Notifications as a Gem
60
60
  email:
61
61
  - ops@heroku.com
@@ -70,8 +70,8 @@ files:
70
70
  - Rakefile
71
71
  - comsat.gemspec
72
72
  - lib/comsat.rb
73
+ - lib/comsat/client.rb
73
74
  - lib/comsat/helpers/auth_helper.rb
74
- - lib/comsat/log.rb
75
75
  - lib/comsat/route.rb
76
76
  - lib/comsat/service.rb
77
77
  - lib/comsat/services/campfire.rb
data/lib/comsat/log.rb DELETED
@@ -1,16 +0,0 @@
1
- module Comsat
2
- module Log
3
- def self.start
4
- Scrolls::Log.start
5
- log(ns: "log", fn: "start")
6
- end
7
-
8
- def self.merge(data1, data2)
9
- data1.merge(data2)
10
- end
11
-
12
- def self.log(data, &blk)
13
- Scrolls.log(merge({app: "comsat"}, data), &blk)
14
- end
15
- end
16
- end