bobble 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ require 'pony'
2
+
3
+ class Bobble::GmailNotifier
4
+ class << self
5
+
6
+ def send(message, url)
7
+ Pony.mail({
8
+ :to => ENV["BOBBLE_GMAIL_TO_EMAIL"],
9
+ :via => :smtp,
10
+ :via_options => {
11
+ :address => 'smtp.gmail.com',
12
+ :port => '587',
13
+ :enable_starttls_auto => true,
14
+ :user_name => ENV["BOBBLE_GMAIL_USERNAME"],
15
+ :password => ENV["BOBBLE_GMAIL_PASSWORD"],
16
+ :authentication => :plain, # :plain, :login, :cram_md5, no auth by default
17
+ :domain => "localhost.localdomain" # the HELO domain provided by the client to the server
18
+ },
19
+ :body => message,
20
+ :subject => "Bobble: #{url} is down"
21
+ })
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,81 @@
1
+ require 'bobble/util'
2
+
3
+ #
4
+ # Google Voice Notifier
5
+ # Adapted from: http://brettterpstra.com/sms-from-the-command-line-with-google-voice/
6
+ #
7
+ class Bobble::GoogleVoiceNotifier
8
+ class << self
9
+
10
+ def postit(uri_str, data, header = nil, limit = 3)
11
+ raise ArgumentError, 'HTTP redirect too deep' if limit == 0
12
+ url = URI.parse(uri_str)
13
+ http = Net::HTTP.new(url.host,443)
14
+ http.use_ssl = true
15
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
16
+ response,content = http.post(url.path,data,header)
17
+ case response
18
+ when Net::HTTPSuccess then content
19
+ when Net::HTTPRedirection then postit(response['location'],data,header, limit - 1)
20
+ else
21
+ puts response.inspect
22
+ response.error!
23
+ end
24
+ end
25
+
26
+ def getit(uri_str, header, limit = 3)
27
+ raise ArgumentError, 'HTTP redirect too deep' if limit == 0
28
+ url = URI.parse(uri_str)
29
+ http = Net::HTTP.new(url.host,url.port)
30
+ http.use_ssl = true
31
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
32
+ response,content = http.get(url.path,header)
33
+ case response
34
+ when Net::HTTPSuccess then content
35
+ when Net::HTTPRedirection then getit(response['location'],header, limit - 1)
36
+ else
37
+ response.error!
38
+ end
39
+ end
40
+
41
+ def send(message)
42
+ message = Bobble::Util.shorten_to_text_message(message)
43
+
44
+ username = ENV["BOBBLE_GVOICE_USERNAME"]
45
+ password = ENV["BOBBLE_GVOICE_PASSWORD"]
46
+ # TODO: support multiple numbers
47
+ number = ENV["BOBBLE_GVOICE_TO_PHONENUMBER"]
48
+ numbers = [number]
49
+
50
+ data = "accountType=GOOGLE&Email=#{username}&Passwd=#{password}&service=grandcentral&source=brettterpstra-CLISMS-2.0"
51
+ res = postit('https://www.google.com/accounts/ClientLogin',data)
52
+
53
+ if res
54
+ authcode = res.match(/Auth=(.+)/)[1]
55
+ header = {'Authorization' => "GoogleLogin auth=#{authcode.strip}",'Content-Length' => '0'}
56
+ newres = getit('https://www.google.com/voice',header)
57
+
58
+ if newres
59
+ rnrse = newres.match(/'_rnr_se': '([^']+)'/)[1]
60
+ numbers.each do |num|
61
+ data = "_rnr_se=#{rnrse}&phoneNumber=#{num.strip}&text=#{message}&id="
62
+ finalres = postit('https://www.google.com/voice/sms/send/',data,header)
63
+
64
+ if finalres["ok"]
65
+ puts "Message sent to #{num}"
66
+ else
67
+ puts "Error sending to #{num}"
68
+ end
69
+ end
70
+
71
+ else
72
+ newres.error!
73
+ end
74
+
75
+ else
76
+ res.error!
77
+ end
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,31 @@
1
+ require 'twilio-ruby'
2
+ require 'bobble/util'
3
+
4
+ class Bobble::TwilioNotifier
5
+ class << self
6
+
7
+ @@client = nil
8
+
9
+ def create_client
10
+ return if @@client
11
+
12
+ account_sid = ENV['BOBBLE_TWILIO_SID']
13
+ auth_token = ENV['BOBBLE_TWILIO_TOKEN']
14
+
15
+ @@client = Twilio::REST::Client.new account_sid, auth_token
16
+ end
17
+
18
+ def send(message)
19
+ create_client
20
+ message = Bobble::Util.shorten_to_text_message(message)
21
+
22
+ params = {
23
+ :from => ENV['BOBBLE_TWILIO_FROM_PHONENUMBER'],
24
+ :to => ENV['BOBBLE_TWILIO_TO_PHONENUMBER'],
25
+ :body => message
26
+ }
27
+ @@client.account.sms.messages.create(params)
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,13 @@
1
+ class Bobble::Util
2
+ class << self
3
+
4
+ def shorten_to_text_message(message)
5
+ text_message = message
6
+ if message.length > 140
7
+ text_message = message[0..136] + "..."
8
+ end
9
+ return message
10
+ end
11
+
12
+ end
13
+ end
data/lib/bobble.rb ADDED
@@ -0,0 +1,63 @@
1
+ class Bobble
2
+ class << self
3
+
4
+ # default options are guessed based on what environment variables are present
5
+ @@options = {
6
+ :twilio => !!ENV["BOBBLE_TWILIO_SID"],
7
+ :google_voice => !!ENV["BOBBLE_GVOICE_USERNAME"],
8
+ :gmail => !!ENV["BOBBLE_GMAIL_USERNAME"]
9
+ }
10
+
11
+ def options(o)
12
+ @@options.update(o)
13
+ end
14
+
15
+ def check(url)
16
+ begin
17
+ response = Net::HTTP.get(URI.parse(url))
18
+ raise Exception.new("empty response") if response == ""
19
+ puts "Successful!: #{url}"
20
+ rescue Exception => e
21
+ message = "FAILED: #{url} - #{e.message}"
22
+ puts message
23
+
24
+ send_notification(message, url)
25
+ end
26
+ end
27
+
28
+ def send_notification(message, url)
29
+
30
+ if @@options[:gmail]
31
+ begin
32
+ GmailNotifier.send(message, url)
33
+ rescue Exception => e
34
+ puts "Gmail Notifier failed: #{e.message}"
35
+ end
36
+ end
37
+
38
+ if @@options[:twilio]
39
+ begin
40
+ TwilioNotifier.send(message)
41
+ rescue Exception => e
42
+ puts "Twilio Notifier failed: #{e.message}"
43
+ end
44
+ end
45
+
46
+ if @@options[:google_voice]
47
+ begin
48
+ GoogleVoiceNotifier.send(message)
49
+ rescue Exception => e
50
+ puts "Google Voice Notifier failed: #{e.message}"
51
+ end
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+ end
58
+
59
+ require 'bobble/twilio_notifier'
60
+ require 'bobble/google_voice_notifier'
61
+ require 'bobble/gmail_notifier'
62
+
63
+
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bobble
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Farmer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-24 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: twilio
16
+ requirement: &70093003528280 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70093003528280
25
+ - !ruby/object:Gem::Dependency
26
+ name: pony
27
+ requirement: &70093003527240 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '1.4'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70093003527240
36
+ description: For freely pinging your favorite web services & freely or cheaply getting
37
+ email/SMS notifications when they're down.
38
+ email: ahfarmer@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - lib/bobble.rb
44
+ - lib/bobble/gmail_notifier.rb
45
+ - lib/bobble/google_voice_notifier.rb
46
+ - lib/bobble/twilio_notifier.rb
47
+ - lib/bobble/util.rb
48
+ homepage: https://github.com/ahfarmer/bobble
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.11
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Bobble
72
+ test_files: []