redphone 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.org CHANGED
@@ -1,27 +1,52 @@
1
1
  * Redphone, the monitoring service ruby library
2
2
  - Pagerduty support (integration, incidents)
3
+ - Loggly support (events)
3
4
  [[https://github.com/portertech/redphone/raw/master/redphone.jpg]]
4
5
  * Getting started
5
6
  : gem install redphone
6
7
  * Examples
7
8
  ** Pagerduty
8
9
  : require "redphone/pagerduty"
9
- : pagerduty = Redphone::Pagerduty.new(:service_key => SERVICE_KEY)
10
- Trigger an incident
11
- : pagerduty.trigger_incident(:description => "Testing Redphone Rubygem.", :incident_key => "redphone/test")
12
- Resolve an incident
13
- : pagerduty.resolve_incident(:incident_key => "redphone/test")
14
- List/query current and past incidents
15
- : incidents = Redphone::Pagerduty.incidents(
10
+ : pagerduty = Redphone::Pagerduty.new(
11
+ : :service_key => SERVICE_KEY,
16
12
  : :subdomain => SUBDOMAIN,
17
13
  : :user => USER,
18
- : :password => PASSWORD,
14
+ : :password => PASSWORD
15
+ : )
16
+ Trigger an incident
17
+ : pagerduty.trigger_incident(
18
+ : :description => "Testing Redphone Rubygem",
19
19
  : :incident_key => "redphone/test"
20
20
  : )
21
+ : # OR Redphone::Pagerduty.trigger_incident()
22
+ Resolve an incident
23
+ : pagerduty.resolve_incident(:incident_key => "redphone/test")
24
+ : # OR Redphone::Pagerduty.resolve_incident()
25
+ List/query current and past incidents
26
+ : incidents = pagerduty.incidents(:incident_key => "redphone/test")
21
27
  You can add the following options for a date range
22
28
  : :since => "2011-08-01",
23
29
  : :until => "2011-10-01"
24
30
  Or if you only care about unresolved incidents
25
31
  : :status => "triggered,acknowledged"
32
+ ** Loggly
33
+ : require "redphone/loggly"
34
+ : loggly = Redphone::Loggly.new(
35
+ : :subdomain => SUBDOMAIN,
36
+ : :user => USER,
37
+ : :password => PASSWORD
38
+ : )
39
+ Create an event
40
+ : loggly.send_event(
41
+ : :input_key => INPUT_KEY,
42
+ : :input_type => "json",
43
+ : :event => {
44
+ : :service => "redphone",
45
+ : :message => "test"
46
+ : }
47
+ : )
48
+ : # OR Redphone::Loggly.send_event()
49
+ Search for previous event occurrences
50
+ : events = loggly.search(:q => "json.service:redphone")
26
51
  * Contributors
27
52
  - [[http://portertech.ca][Sean Porter]]
@@ -10,7 +10,8 @@ def http_request(options={})
10
10
  uri = URI.parse(options[:uri])
11
11
  user = options[:user]
12
12
  password = options[:password]
13
- body = options[:body] || nil
13
+ headers = options[:headers] || Hash.new
14
+ body = options[:body]
14
15
  http = Net::HTTP.new(uri.host, uri.port)
15
16
  if options[:ssl] == true
16
17
  http.use_ssl = true
@@ -28,6 +29,9 @@ def http_request(options={})
28
29
  else
29
30
  raise "Unknown HTTP method: #{method}"
30
31
  end
32
+ headers.each do |header, value|
33
+ request.add_field(header, value)
34
+ end
31
35
  if user && password
32
36
  request.basic_auth user, password
33
37
  end
@@ -0,0 +1,48 @@
1
+ require File.join(File.dirname(__FILE__), 'helpers')
2
+
3
+ module Redphone
4
+ class Loggly
5
+ def initialize(options={})
6
+ raise "You must supply a subdomain" if options[:subdomain].nil?
7
+ raise "You must supply a user" if options[:user].nil?
8
+ raise "You must supply a password" if options[:password].nil?
9
+ @subdomain = options[:subdomain]
10
+ @user = options[:user]
11
+ @password = options[:password]
12
+ @input_key = options[:input_key]
13
+ @input_type = options[:input_type]
14
+ end
15
+
16
+ def search(options={})
17
+ raise "You must supply a query string" if options[:q].nil?
18
+ params = options.map { |key, value| "#{key}=#{CGI.escape(value)}" }.join("&")
19
+ response = http_request(
20
+ :user => @user,
21
+ :password => @password,
22
+ :ssl => true,
23
+ :uri => "https://#{@subdomain}.loggly.com/api/search?#{params}"
24
+ )
25
+ JSON.parse(response.body)
26
+ end
27
+
28
+ def self.send_event(options={})
29
+ raise "You must supply a input key" if options[:input_key].nil?
30
+ raise "You must supply an event hash" if options[:event].nil? || !options[:event].is_a?(Hash)
31
+ content_type = options[:input_type] == "json" ? "application/json" : "text/plain"
32
+ response = http_request(
33
+ :method => "post",
34
+ :ssl => true,
35
+ :uri => "https://logs.loggly.com/inputs/#{options[:input_key]}",
36
+ :headers => {"content-type" => content_type},
37
+ :body => options[:event].to_json
38
+ )
39
+ JSON.parse(response.body)
40
+ end
41
+
42
+ def send_event(options={})
43
+ options[:input_key] = options[:input_key] || @input_key
44
+ options[:input_type] = options[:input_type] || @input_type
45
+ self.class.send_event(options)
46
+ end
47
+ end
48
+ end
@@ -3,44 +3,56 @@ require File.join(File.dirname(__FILE__), 'helpers')
3
3
  module Redphone
4
4
  class Pagerduty
5
5
  def initialize(options={})
6
- raise "You must supply a service key" if options[:service_key].nil?
6
+ raise "You must supply a subdomain" if options[:subdomain].nil?
7
+ raise "You must supply a user" if options[:user].nil?
8
+ raise "You must supply a password" if options[:password].nil?
9
+ @subdomain = options[:subdomain]
10
+ @user = options[:user]
11
+ @password = options[:password]
7
12
  @service_key = options[:service_key]
8
13
  end
9
14
 
10
- def integration_api(request_body)
15
+ def self.integration_api(request_body)
11
16
  response = http_request(
12
17
  :method => "post",
13
18
  :ssl => true,
14
19
  :uri => "https://events.pagerduty.com/generic/2010-04-15/create_event.json",
15
- :body => request_body.merge({:service_key => @service_key}).to_json
20
+ :body => request_body.to_json
16
21
  )
17
22
  JSON.parse(response.body)
18
23
  end
19
24
 
20
- def trigger_incident(options={})
25
+ def self.trigger_incident(options={})
26
+ raise "You must supply a service key" if options[:service_key].nil?
21
27
  raise "You must supply a description" if options[:description].nil?
22
28
  request_body = options.merge!({:event_type => "trigger"})
23
29
  integration_api(request_body)
24
30
  end
25
31
 
26
- def resolve_incident(options={})
32
+ def trigger_incident(options={})
33
+ options[:service_key] = options[:service_key] || @service_key
34
+ self.class.trigger_incident(options)
35
+ end
36
+
37
+ def self.resolve_incident(options={})
38
+ raise "You must supply a service key" if options[:service_key].nil?
27
39
  raise "You must supply a incident key" if options[:incident_key].nil?
28
40
  request_body = options.merge!({:event_type => "resolve"})
29
41
  integration_api(request_body)
30
42
  end
31
43
 
32
- def self.incidents(options={})
33
- raise "You must supply a subdomain" if options[:subdomain].nil?
34
- raise "You must supply a user" if options[:user].nil?
35
- raise "You must supply a password" if options[:password].nil?
36
- params_hash = options.reject { |key, value| [:subdomain, :user, :password].include?(key) }
37
- params = params_hash.map { |key, value| "#{key}=#{CGI.escape(value)}"}.join("&")
44
+ def resolve_incident(options={})
45
+ options[:service_key] = options[:service_key] || @service_key
46
+ self.class.resolve_incident(options)
47
+ end
48
+
49
+ def incidents(options={})
50
+ params = options.map { |key, value| "#{key}=#{CGI.escape(value)}" }.join("&")
38
51
  response = http_request(
39
- :method => "get",
40
- :user => options[:user],
41
- :password => options[:password],
52
+ :user => @user,
53
+ :password => @password,
42
54
  :ssl => true,
43
- :uri => "https://#{options[:subdomain]}.pagerduty.com/api/v1/incidents?#{params}"
55
+ :uri => "https://#{@subdomain}.pagerduty.com/api/v1/incidents?#{params}"
44
56
  )
45
57
  JSON.parse(response.body)
46
58
  end
@@ -1,3 +1,3 @@
1
1
  module Redphone
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,39 @@
1
+ $: << File.dirname(__FILE__) + '/../lib' unless $:.include?(File.dirname(__FILE__) + '/../lib/')
2
+ require 'rubygems' if RUBY_VERSION < '1.9.0'
3
+ gem 'minitest'
4
+ require 'minitest/autorun'
5
+ require 'redphone/loggly'
6
+
7
+ LOGGLY_INPUT_KEY = File.open("loggly_input_key.txt", "rb").read.gsub("\n", "")
8
+ credentials = File.open("loggly_credentials.txt", "rb").read.split("\n")
9
+ LOGGLY_SUBDOMAIN, LOGGLY_USER, LOGGLY_PASSWORD = credentials.each { |row| row }
10
+
11
+ class TestRedphoneLoggly < MiniTest::Unit::TestCase
12
+ i_suck_and_my_tests_are_order_dependent!
13
+
14
+ def setup
15
+ @loggly = Redphone::Loggly.new(
16
+ #:input_key => LOGGLY_INPUT_KEY,
17
+ :subdomain => LOGGLY_SUBDOMAIN,
18
+ :user => LOGGLY_USER,
19
+ :password => LOGGLY_PASSWORD
20
+ )
21
+ end
22
+
23
+ def test_send_event
24
+ response = @loggly.send_event(
25
+ :input_key => LOGGLY_INPUT_KEY,
26
+ :input_type => "json",
27
+ :event => {
28
+ :service => "redphone",
29
+ :message => "test"
30
+ }
31
+ )
32
+ assert_equal 'ok', response['response']
33
+ end
34
+
35
+ def test_search
36
+ response = @loggly.search(:q => "json.service:redphone")
37
+ assert response['numFound'] > 0
38
+ end
39
+ end
@@ -4,34 +4,41 @@ gem 'minitest'
4
4
  require 'minitest/autorun'
5
5
  require 'redphone/pagerduty'
6
6
 
7
- SERVICE_KEY = File.open("pagerduty_service_key.txt", "rb").read.gsub("\n", "")
7
+ PAGERDUTY_SERVICE_KEY = File.open("pagerduty_service_key.txt", "rb").read.gsub("\n", "")
8
8
  credentials = File.open("pagerduty_credentials.txt", "rb").read.split("\n")
9
- SUBDOMAIN, USER, PASSWORD = credentials.each { |row| row }
9
+ PAGERDUTY_SUBDOMAIN, PAGERDUTY_USER, PAGERDUTY_PASSWORD = credentials.each { |row| row }
10
10
 
11
11
  class TestRedphonePagerduty < MiniTest::Unit::TestCase
12
12
  i_suck_and_my_tests_are_order_dependent!
13
13
 
14
14
  def setup
15
- @pagerduty = Redphone::Pagerduty.new(:service_key => SERVICE_KEY)
15
+ @pagerduty = Redphone::Pagerduty.new(
16
+ #:service_key => PAGERDUTY_SERVICE_KEY,
17
+ :subdomain => PAGERDUTY_SUBDOMAIN,
18
+ :user => PAGERDUTY_USER,
19
+ :password => PAGERDUTY_PASSWORD
20
+ )
16
21
  end
17
22
 
18
23
  def test_trigger_incident
19
- response = @pagerduty.trigger_incident(:description => "Testing Redphone Rubygem.", :incident_key => "redphone/test")
24
+ response = @pagerduty.trigger_incident(
25
+ :service_key => PAGERDUTY_SERVICE_KEY,
26
+ :description => "Testing Redphone Rubygem",
27
+ :incident_key => "redphone/test"
28
+ )
20
29
  assert_equal 'success', response['status']
21
30
  end
22
31
 
23
32
  def test_resolve_incident
24
- response = @pagerduty.resolve_incident(:incident_key => "redphone/test")
33
+ response = @pagerduty.resolve_incident(
34
+ :service_key => PAGERDUTY_SERVICE_KEY,
35
+ :incident_key => "redphone/test"
36
+ )
25
37
  assert_equal 'success', response['status']
26
38
  end
27
39
 
28
40
  def test_incidents
29
- response = Redphone::Pagerduty.incidents(
30
- :subdomain => SUBDOMAIN,
31
- :user => USER,
32
- :password => PASSWORD,
33
- :incident_key => "redphone/test"
34
- )
41
+ response = @pagerduty.incidents(:incident_key => "redphone/test")
35
42
  assert response['total'] > 0
36
43
  end
37
44
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redphone
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-10-08 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &70118051461720 !ruby/object:Gem::Requirement
16
+ requirement: &70194551818180 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70118051461720
24
+ version_requirements: *70194551818180
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70118051461080 !ruby/object:Gem::Requirement
27
+ requirement: &70194551817500 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70118051461080
35
+ version_requirements: *70194551817500
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: minitest
38
- requirement: &70118051460460 !ruby/object:Gem::Requirement
38
+ requirement: &70194551816900 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70118051460460
46
+ version_requirements: *70194551816900
47
47
  description: A rubygem for talking to monitoring service APIs
48
48
  email:
49
49
  - portertech@gmail.com
@@ -57,10 +57,12 @@ files:
57
57
  - Rakefile
58
58
  - lib/redphone.rb
59
59
  - lib/redphone/helpers.rb
60
+ - lib/redphone/loggly.rb
60
61
  - lib/redphone/pagerduty.rb
61
62
  - lib/redphone/version.rb
62
63
  - redphone.gemspec
63
64
  - redphone.jpg
65
+ - test/loggly_test.rb
64
66
  - test/pagerduty_test.rb
65
67
  homepage: https://github.com/portertech/redphone
66
68
  licenses: []
@@ -87,4 +89,5 @@ signing_key:
87
89
  specification_version: 3
88
90
  summary: A rubygem for talking to monitoring service APIs
89
91
  test_files:
92
+ - test/loggly_test.rb
90
93
  - test/pagerduty_test.rb