messenger 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +36 -0
- data/bin/messenger +1 -1
- data/lib/messenger.rb +8 -75
- data/lib/messenger/campfire.rb +34 -38
- data/lib/messenger/email.rb +25 -29
- data/lib/messenger/jabber.rb +33 -37
- data/lib/messenger/messenger.rb +69 -0
- data/lib/messenger/notifo.rb +30 -35
- data/lib/messenger/result.rb +17 -21
- data/lib/messenger/version.rb +3 -0
- data/lib/messenger/web.rb +31 -35
- metadata +43 -90
- data/VERSION.yml +0 -5
- data/test/test_campfire.rb +0 -73
- data/test/test_email.rb +0 -55
- data/test/test_helper.rb +0 -11
- data/test/test_jabber.rb +0 -66
- data/test/test_messenger.rb +0 -41
- data/test/test_notifo.rb +0 -40
- data/test/test_result.rb +0 -25
- data/test/test_web.rb +0 -65
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
Rake::TestTask.new(:test) do |test|
|
6
|
+
test.libs << 'lib' << 'test'
|
7
|
+
test.pattern = 'test/**/*_test.rb'
|
8
|
+
test.verbose = true
|
9
|
+
end
|
10
|
+
|
11
|
+
task :default => :test
|
12
|
+
|
13
|
+
require 'rake/rdoctask'
|
14
|
+
|
15
|
+
Rake::RDocTask.new do |rdoc|
|
16
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
17
|
+
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = "messenger #{version}"
|
20
|
+
rdoc.rdoc_files.include('README*')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
23
|
+
|
24
|
+
begin
|
25
|
+
require 'rcov/rcovtask'
|
26
|
+
Rcov::RcovTask.new do |test|
|
27
|
+
test.libs << 'test'
|
28
|
+
test.pattern = 'test/**/*_test.rb'
|
29
|
+
test.verbose = true
|
30
|
+
test.rcov_opts << '--exclude "gems/*"'
|
31
|
+
end
|
32
|
+
rescue LoadError
|
33
|
+
task :rcov do
|
34
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install rcov"
|
35
|
+
end
|
36
|
+
end
|
data/bin/messenger
CHANGED
data/lib/messenger.rb
CHANGED
@@ -1,76 +1,9 @@
|
|
1
|
-
$:.unshift File.dirname(__FILE__)
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
1
|
require 'messenger/errors'
|
5
|
-
require '
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
def self.version
|
14
|
-
MESSAGER_VERSION.join(".")
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.root
|
18
|
-
APP_ROOT
|
19
|
-
end
|
20
|
-
|
21
|
-
|
22
|
-
def self.valid_url?(url)
|
23
|
-
service_handler = handler(url)
|
24
|
-
service_handler.valid_url?(url)
|
25
|
-
rescue ProtocolError
|
26
|
-
false
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.send(url, message, options={})
|
30
|
-
service_handler = handler(url)
|
31
|
-
SystemTimer.timeout_after(options[:timeout] || 15) do
|
32
|
-
service_handler.send(url, message, options)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def self.obfuscate(url)
|
37
|
-
service_handler = handler(url)
|
38
|
-
service_handler.obfuscate(url)
|
39
|
-
end
|
40
|
-
|
41
|
-
|
42
|
-
def self.protocol(url)
|
43
|
-
# TODO: More services
|
44
|
-
# sms://1231231234
|
45
|
-
# twitter://username
|
46
|
-
# aim://username
|
47
|
-
case url
|
48
|
-
when /^http/: :http
|
49
|
-
when /^campfire/: :campfire
|
50
|
-
when /^jabber/: :jabber
|
51
|
-
when /^notifo/: :notifo
|
52
|
-
when /^mailto|@+/: :email
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def self.handler(url)
|
57
|
-
case protocol(url)
|
58
|
-
when :email: Email
|
59
|
-
when :http: Web
|
60
|
-
when :campfire: Campfire
|
61
|
-
when :jabber: Jabber
|
62
|
-
when :notifo: Notifo
|
63
|
-
else
|
64
|
-
raise ProtocolError, "Malformed service URL: #{url}. Either this syntax is wrong or this service type is not yet implemented."
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
|
69
|
-
autoload :Result, "messenger/result"
|
70
|
-
autoload :Email, "messenger/email"
|
71
|
-
autoload :Web, "messenger/web"
|
72
|
-
autoload :Campfire, "messenger/campfire"
|
73
|
-
autoload :Jabber, "messenger/jabber"
|
74
|
-
autoload :Notifo, "messenger/notifo"
|
75
|
-
|
76
|
-
end
|
2
|
+
require 'messenger/messenger'
|
3
|
+
require 'messenger/result'
|
4
|
+
require 'messenger/email'
|
5
|
+
require 'messenger/web'
|
6
|
+
require 'messenger/campfire'
|
7
|
+
require 'messenger/jabber'
|
8
|
+
require 'messenger/notifo'
|
9
|
+
require 'messenger/version'
|
data/lib/messenger/campfire.rb
CHANGED
@@ -1,51 +1,47 @@
|
|
1
|
-
require '
|
1
|
+
require 'typhoeus'
|
2
2
|
require 'json'
|
3
3
|
|
4
|
-
|
4
|
+
class Messenger::Campfire
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
false
|
12
|
-
end
|
6
|
+
def self.valid_url?(url)
|
7
|
+
!!matcher(url)
|
8
|
+
rescue NoMethodError
|
9
|
+
false
|
10
|
+
end
|
13
11
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
12
|
+
# URL format:
|
13
|
+
# campfire://api-key:room-id@subdomain.campfirenow.com
|
14
|
+
def self.deliver(url, body, options={})
|
15
|
+
raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
|
16
|
+
ssl, api_key, room, subdomain = matcher(url)
|
17
|
+
options[:headers] ||= {}
|
18
|
+
response = Typhoeus::Request.post(
|
19
|
+
"http#{ssl ? "s" : ""}://#{api_key}:x@#{subdomain}.campfirenow.com/room/#{room}/speak.json",
|
20
|
+
:headers => { "Content-Type" => "application/json"}.merge(options[:headers]),
|
21
|
+
:body => { "message" => { "body" => body } }.to_json
|
22
|
+
)
|
23
|
+
Messenger::Result.new(success?(response), response)
|
24
|
+
end
|
27
25
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
26
|
+
def self.obfuscate(url)
|
27
|
+
raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
|
28
|
+
ssl, api_key, room, subdomain = matcher(url)
|
29
|
+
"campfire#{ssl ? "-ssl" : ""}://xxxx:#{room}@#{subdomain}.campfirenow.com"
|
30
|
+
end
|
33
31
|
|
34
32
|
|
35
|
-
|
33
|
+
private
|
36
34
|
|
37
|
-
|
38
|
-
|
39
|
-
|
35
|
+
def self.matcher(url)
|
36
|
+
url.match(/^campfire(-ssl)?:\/\/([^:]+):([^@]+)@([^\.]+).campfirenow.com/)[1,4]
|
37
|
+
end
|
40
38
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
end
|
39
|
+
def self.success?(response)
|
40
|
+
case response.code
|
41
|
+
when 200, 201: true
|
42
|
+
else
|
43
|
+
false
|
47
44
|
end
|
48
|
-
|
49
45
|
end
|
50
46
|
|
51
47
|
end
|
data/lib/messenger/email.rb
CHANGED
@@ -1,38 +1,34 @@
|
|
1
1
|
require 'mail'
|
2
2
|
|
3
|
-
|
3
|
+
class Messenger::Email
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
!!url.match(/^(mailto:)?[^@]+@.*$/)
|
9
|
-
end
|
10
|
-
|
11
|
-
# URL format:
|
12
|
-
# mailto:email@example.com
|
13
|
-
#
|
14
|
-
# Options:
|
15
|
-
# :email_from => Who the email is from
|
16
|
-
# :email_subject => The subject of the email
|
17
|
-
def self.send(url, message, options={})
|
18
|
-
raise URLError, "The URL provided is invalid" unless valid_url?(url)
|
19
|
-
mail = Mail.new do
|
20
|
-
from options[:email_from]
|
21
|
-
to url.sub(/mailto:/, '')
|
22
|
-
subject options[:email_subject]
|
23
|
-
body message
|
24
|
-
end
|
25
|
-
mail.deliver!
|
26
|
-
Result.new(true, nil)
|
27
|
-
rescue Errno::ECONNREFUSED, Errno::EAFNOSUPPORT => e
|
28
|
-
Result.new(false, e)
|
29
|
-
end
|
5
|
+
def self.valid_url?(url)
|
6
|
+
!!url.match(/^(mailto:)?[^@]+@.*$/)
|
7
|
+
end
|
30
8
|
|
31
|
-
|
32
|
-
|
33
|
-
|
9
|
+
# URL format:
|
10
|
+
# mailto:email@example.com
|
11
|
+
#
|
12
|
+
# Options:
|
13
|
+
# :email_from => Who the email is from
|
14
|
+
# :email_subject => The subject of the email
|
15
|
+
def self.deliver(url, message, options={})
|
16
|
+
raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
|
17
|
+
mail = Mail.new do
|
18
|
+
from options[:email_from]
|
19
|
+
to url.sub(/mailto:/, '')
|
20
|
+
subject options[:email_subject]
|
21
|
+
body message
|
34
22
|
end
|
23
|
+
mail.deliver!
|
24
|
+
Messenger::Result.new(true, nil)
|
25
|
+
rescue Errno::ECONNREFUSED, Errno::EAFNOSUPPORT => e
|
26
|
+
Messenger::Result.new(false, e)
|
27
|
+
end
|
35
28
|
|
29
|
+
def self.obfuscate(url)
|
30
|
+
raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
|
31
|
+
url
|
36
32
|
end
|
37
33
|
|
38
34
|
end
|
data/lib/messenger/jabber.rb
CHANGED
@@ -1,49 +1,45 @@
|
|
1
1
|
require 'xmpp4r-simple'
|
2
2
|
|
3
|
-
module Messenger
|
3
|
+
module Messenger::Jabber
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
false
|
11
|
-
end
|
12
|
-
|
13
|
-
# URL format:
|
14
|
-
# jabber://email@example.com/server_hostname
|
15
|
-
#
|
16
|
-
# The server's hostname is optional, but needed for Google Apps jabber accounts.
|
17
|
-
#
|
18
|
-
# Options:
|
19
|
-
# :jabber_id => The jabber id of the sender
|
20
|
-
# :jabber_password => The password of the sender
|
21
|
-
def self.send(url, body, options={})
|
22
|
-
raise URLError, "The URL provided is invalid" unless valid_url?(url)
|
23
|
-
recipient, host = url.sub("jabber://", "").split("/")[0,2]
|
24
|
-
jabber = ::Jabber::Simple.new(options[:jabber_id], options[:jabber_password], host)
|
25
|
-
jabber.deliver(recipient, body)
|
26
|
-
pending_messages_count = 1
|
27
|
-
until pending_messages_count == 0
|
28
|
-
pending_messages_count = jabber.send(:queue, :pending_messages).size
|
29
|
-
sleep 1
|
30
|
-
end
|
31
|
-
status = jabber.subscribed_to?(recipient)
|
32
|
-
Result.new(status, status ? nil : "Not yet authorized")
|
33
|
-
end
|
5
|
+
def self.valid_url?(url)
|
6
|
+
!!matcher(url)
|
7
|
+
rescue NoMethodError
|
8
|
+
false
|
9
|
+
end
|
34
10
|
|
35
|
-
|
36
|
-
|
37
|
-
|
11
|
+
# URL format:
|
12
|
+
# jabber://email@example.com/server_hostname
|
13
|
+
#
|
14
|
+
# The server's hostname is optional, but needed for Google Apps jabber accounts.
|
15
|
+
#
|
16
|
+
# Options:
|
17
|
+
# :jabber_id => The jabber id of the sender
|
18
|
+
# :jabber_password => The password of the sender
|
19
|
+
def self.deliver(url, body, options={})
|
20
|
+
raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
|
21
|
+
recipient, host = url.sub("jabber://", "").split("/")[0,2]
|
22
|
+
jabber = ::Jabber::Simple.new(options[:jabber_id], options[:jabber_password], host)
|
23
|
+
jabber.deliver(recipient, body)
|
24
|
+
pending_messages_count = 1
|
25
|
+
until pending_messages_count == 0
|
26
|
+
pending_messages_count = jabber.send(:queue, :pending_messages).size
|
27
|
+
sleep 1
|
38
28
|
end
|
29
|
+
status = jabber.subscribed_to?(recipient)
|
30
|
+
Messenger::Result.new(status, status ? nil : "Not yet authorized")
|
31
|
+
end
|
39
32
|
|
33
|
+
def self.obfuscate(url)
|
34
|
+
raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
|
35
|
+
url
|
36
|
+
end
|
40
37
|
|
41
|
-
private
|
42
38
|
|
43
|
-
|
44
|
-
url.sub("jabber://", "").match("@")
|
45
|
-
end
|
39
|
+
private
|
46
40
|
|
41
|
+
def self.matcher(url)
|
42
|
+
url.sub("jabber://", "").match("@")
|
47
43
|
end
|
48
44
|
|
49
45
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'system_timer'
|
2
|
+
require 'base64'
|
3
|
+
|
4
|
+
module Messenger
|
5
|
+
|
6
|
+
MESSAGER_VERSION = [0,1] unless defined?(MESSAGER_VERSION)
|
7
|
+
APP_ROOT = File.expand_path(File.dirname(__FILE__) + '/..') unless defined?(APP_ROOT)
|
8
|
+
|
9
|
+
def self.version
|
10
|
+
MESSAGER_VERSION.join(".")
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.root
|
14
|
+
APP_ROOT
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
def self.valid_url?(url)
|
19
|
+
service_handler = handler(url)
|
20
|
+
service_handler.valid_url?(url)
|
21
|
+
rescue ProtocolError
|
22
|
+
false
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.deliver(url, message, options={})
|
26
|
+
service_handler = handler(url)
|
27
|
+
SystemTimer.timeout_after(options[:timeout] || 15) do
|
28
|
+
service_handler.deliver(url, message, options)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.obfuscate(url)
|
33
|
+
service_handler = handler(url)
|
34
|
+
service_handler.obfuscate(url)
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
def self.protocol(url)
|
39
|
+
# TODO: More services
|
40
|
+
# sms://1231231234
|
41
|
+
# twitter://username
|
42
|
+
# aim://username
|
43
|
+
case url
|
44
|
+
when /^http/: :http
|
45
|
+
when /^campfire/: :campfire
|
46
|
+
when /^jabber/: :jabber
|
47
|
+
when /^notifo/: :notifo
|
48
|
+
when /^mailto|@+/: :email
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.handler(url)
|
53
|
+
case protocol(url)
|
54
|
+
when :email: Messenger::Email
|
55
|
+
when :http: Messenger::Web
|
56
|
+
when :campfire: Messenger::Campfire
|
57
|
+
when :jabber: Messenger::Jabber
|
58
|
+
when :notifo: Messenger::Notifo
|
59
|
+
else
|
60
|
+
raise Messenger::ProtocolError, "Malformed service URL: #{url}. Either this syntax is wrong or this service type is not yet implemented."
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.basic_auth(user, password)
|
65
|
+
encoded_credentials = ["#{user}:#{password}"].pack("m*").gsub(/\n/, '')
|
66
|
+
{"HTTP_AUTHORIZATION" => "Basic #{encoded_credentials}"}
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
data/lib/messenger/notifo.rb
CHANGED
@@ -1,47 +1,42 @@
|
|
1
|
-
require '
|
1
|
+
require 'typhoeus'
|
2
2
|
require 'json'
|
3
3
|
|
4
|
-
|
4
|
+
class Messenger::Notifo
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
!!url.match(/^notifo:\/\/.+$/)
|
10
|
-
end
|
11
|
-
|
12
|
-
# URL format:
|
13
|
-
# notifo://username
|
14
|
-
#
|
15
|
-
# Options:
|
16
|
-
# :notifo_api_username => The service's API username
|
17
|
-
# :notifo_api_secret => The service's API secret
|
18
|
-
# :notifo_title => The notificaiton title
|
19
|
-
# :notifo_url => Open this URL
|
20
|
-
def self.send(url, message, options={})
|
21
|
-
raise URLError, "The URL provided is invalid" unless valid_url?(url)
|
22
|
-
username = matcher(url)
|
23
|
-
response = HTTParty.post("https://api.notifo.com/v1/send_notification",
|
24
|
-
:body => { :to => username, :msg => message, :title => options[:notifo_title], :uri => options[:notifo_url] },
|
25
|
-
:basic_auth => { :username => options[:notifo_api_username], :password => options[:notifo_api_secret] })
|
26
|
-
Result.new(success?(response), response)
|
27
|
-
end
|
6
|
+
def self.valid_url?(url)
|
7
|
+
!!url.match(/^notifo:\/\/.+$/)
|
8
|
+
end
|
28
9
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
10
|
+
# URL format:
|
11
|
+
# notifo://username
|
12
|
+
#
|
13
|
+
# Options:
|
14
|
+
# :notifo_api_username => The service's API username
|
15
|
+
# :notifo_api_secret => The service's API secret
|
16
|
+
# :notifo_title => The notificaiton title
|
17
|
+
# :notifo_url => Open this URL
|
18
|
+
def self.deliver(url, message, options={})
|
19
|
+
raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
|
20
|
+
username = matcher(url)
|
21
|
+
response = Typhoeus::Request.post("https://#{options[:notifo_api_username]}:#{options[:notifo_api_secret]}@api.notifo.com/v1/send_notification",
|
22
|
+
:body => { :to => username, :msg => message, :title => options[:notifo_title], :uri => options[:notifo_url] }.to_param)
|
23
|
+
Messenger::Result.new(success?(response), response)
|
24
|
+
end
|
33
25
|
|
26
|
+
def self.obfuscate(url)
|
27
|
+
raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
|
28
|
+
url
|
29
|
+
end
|
34
30
|
|
35
|
-
private
|
36
31
|
|
37
|
-
|
38
|
-
url.match(/^notifo:\/\/(.+)/)[1]
|
39
|
-
end
|
32
|
+
private
|
40
33
|
|
41
|
-
|
42
|
-
|
43
|
-
|
34
|
+
def self.matcher(url)
|
35
|
+
url.match(/^notifo:\/\/(.+)/)[1]
|
36
|
+
end
|
44
37
|
|
38
|
+
def self.success?(response)
|
39
|
+
response.code == 200
|
45
40
|
end
|
46
41
|
|
47
42
|
end
|
data/lib/messenger/result.rb
CHANGED
@@ -1,30 +1,26 @@
|
|
1
|
-
|
1
|
+
class Messenger::Result
|
2
2
|
|
3
|
-
|
3
|
+
attr_reader :response
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
@response = response
|
10
|
-
end
|
5
|
+
def initialize(success, response)
|
6
|
+
@success = success
|
7
|
+
@response = response
|
8
|
+
end
|
11
9
|
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
def success?
|
11
|
+
!!@success
|
12
|
+
end
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
def code
|
15
|
+
response.code rescue nil
|
16
|
+
end
|
19
17
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
18
|
+
def body
|
19
|
+
if response.respond_to?(:body)
|
20
|
+
response.body
|
21
|
+
else
|
22
|
+
response.to_s
|
26
23
|
end
|
27
|
-
|
28
24
|
end
|
29
25
|
|
30
26
|
end
|
data/lib/messenger/web.rb
CHANGED
@@ -1,47 +1,43 @@
|
|
1
|
-
require '
|
1
|
+
require 'typhoeus'
|
2
2
|
|
3
|
-
|
3
|
+
class Messenger::Web
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
false
|
11
|
-
end
|
5
|
+
def self.valid_url?(url)
|
6
|
+
!!URI.parse(url)
|
7
|
+
rescue URI::InvalidURIError
|
8
|
+
false
|
9
|
+
end
|
12
10
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
11
|
+
# URL format:
|
12
|
+
# http://example.com
|
13
|
+
# https://user:pass@example.com
|
14
|
+
#
|
15
|
+
# The body of the message is posted as the body of the request, not the query.
|
16
|
+
def self.deliver(url, body, options={})
|
17
|
+
raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
|
18
|
+
response = Typhoeus::Request.post(url, options.merge(:body => body))
|
19
|
+
Messenger::Result.new(success?(response), response)
|
20
|
+
end
|
23
21
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
22
|
+
def self.obfuscate(url)
|
23
|
+
raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
|
24
|
+
path = URI.parse(url)
|
25
|
+
if path.password
|
26
|
+
url.sub(/#{path.password}/, 'xxxx')
|
27
|
+
else
|
28
|
+
url
|
32
29
|
end
|
30
|
+
end
|
33
31
|
|
34
32
|
|
35
|
-
|
33
|
+
private
|
36
34
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
35
|
+
def self.success?(response)
|
36
|
+
case response.code
|
37
|
+
when 200, 201: true
|
38
|
+
else
|
39
|
+
false
|
43
40
|
end
|
44
|
-
|
45
41
|
end
|
46
42
|
|
47
43
|
end
|
metadata
CHANGED
@@ -1,150 +1,121 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: messenger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 23
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
|
-
-
|
7
|
+
- 3
|
9
8
|
- 0
|
10
|
-
version: 0.
|
9
|
+
version: 0.3.0
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Brandon Arbini
|
13
|
+
- Nathan Sutton
|
14
14
|
autorequire:
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-06-
|
19
|
-
default_executable:
|
18
|
+
date: 2010-06-22 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
name:
|
22
|
+
name: SystemTimer
|
23
23
|
prerelease: false
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
25
|
requirements:
|
27
26
|
- - ">="
|
28
27
|
- !ruby/object:Gem::Version
|
29
|
-
hash: 17
|
30
28
|
segments:
|
31
|
-
-
|
32
|
-
|
33
|
-
version: "1.15"
|
29
|
+
- 0
|
30
|
+
version: "0"
|
34
31
|
type: :runtime
|
35
32
|
version_requirements: *id001
|
36
33
|
- !ruby/object:Gem::Dependency
|
37
|
-
name:
|
34
|
+
name: xmpp4r-simple
|
38
35
|
prerelease: false
|
39
36
|
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
-
none: false
|
41
37
|
requirements:
|
42
38
|
- - ">="
|
43
39
|
- !ruby/object:Gem::Version
|
44
|
-
hash: 47
|
45
40
|
segments:
|
46
|
-
-
|
47
|
-
|
48
|
-
version: "1.16"
|
41
|
+
- 0
|
42
|
+
version: "0"
|
49
43
|
type: :runtime
|
50
44
|
version_requirements: *id002
|
51
45
|
- !ruby/object:Gem::Dependency
|
52
|
-
name:
|
46
|
+
name: mail
|
53
47
|
prerelease: false
|
54
48
|
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
-
none: false
|
56
49
|
requirements:
|
57
50
|
- - ">="
|
58
51
|
- !ruby/object:Gem::Version
|
59
|
-
hash: 15
|
60
52
|
segments:
|
61
53
|
- 0
|
62
|
-
|
63
|
-
- 2
|
64
|
-
version: 0.5.2
|
54
|
+
version: "0"
|
65
55
|
type: :runtime
|
66
56
|
version_requirements: *id003
|
67
57
|
- !ruby/object:Gem::Dependency
|
68
|
-
name:
|
58
|
+
name: typhoeus
|
69
59
|
prerelease: false
|
70
60
|
requirement: &id004 !ruby/object:Gem::Requirement
|
71
|
-
none: false
|
72
61
|
requirements:
|
73
62
|
- - ">="
|
74
63
|
- !ruby/object:Gem::Version
|
75
|
-
hash: 21
|
76
64
|
segments:
|
77
|
-
-
|
78
|
-
|
79
|
-
- 3
|
80
|
-
version: 1.1.3
|
65
|
+
- 0
|
66
|
+
version: "0"
|
81
67
|
type: :runtime
|
82
68
|
version_requirements: *id004
|
83
69
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
70
|
+
name: trollop
|
85
71
|
prerelease: false
|
86
72
|
requirement: &id005 !ruby/object:Gem::Requirement
|
87
|
-
none: false
|
88
73
|
requirements:
|
89
|
-
- - "
|
74
|
+
- - ">="
|
90
75
|
- !ruby/object:Gem::Version
|
91
|
-
hash: 1
|
92
76
|
segments:
|
93
77
|
- 0
|
94
|
-
|
95
|
-
version: "0.5"
|
78
|
+
version: "0"
|
96
79
|
type: :runtime
|
97
80
|
version_requirements: *id005
|
98
81
|
- !ruby/object:Gem::Dependency
|
99
|
-
name:
|
82
|
+
name: shoulda
|
100
83
|
prerelease: false
|
101
84
|
requirement: &id006 !ruby/object:Gem::Requirement
|
102
|
-
none: false
|
103
85
|
requirements:
|
104
|
-
- - "
|
86
|
+
- - ">="
|
105
87
|
- !ruby/object:Gem::Version
|
106
|
-
hash: 47
|
107
88
|
segments:
|
108
89
|
- 0
|
109
|
-
|
110
|
-
|
111
|
-
version: 0.8.8
|
112
|
-
type: :runtime
|
90
|
+
version: "0"
|
91
|
+
type: :development
|
113
92
|
version_requirements: *id006
|
114
93
|
- !ruby/object:Gem::Dependency
|
115
|
-
name:
|
94
|
+
name: mocha
|
116
95
|
prerelease: false
|
117
96
|
requirement: &id007 !ruby/object:Gem::Requirement
|
118
|
-
none: false
|
119
97
|
requirements:
|
120
98
|
- - ">="
|
121
99
|
- !ruby/object:Gem::Version
|
122
|
-
hash: 31
|
123
100
|
segments:
|
124
|
-
- 1
|
125
|
-
- 2
|
126
101
|
- 0
|
127
|
-
version:
|
128
|
-
type: :
|
102
|
+
version: "0"
|
103
|
+
type: :development
|
129
104
|
version_requirements: *id007
|
130
105
|
- !ruby/object:Gem::Dependency
|
131
|
-
name:
|
106
|
+
name: webmock
|
132
107
|
prerelease: false
|
133
108
|
requirement: &id008 !ruby/object:Gem::Requirement
|
134
|
-
none: false
|
135
109
|
requirements:
|
136
110
|
- - ">="
|
137
111
|
- !ruby/object:Gem::Version
|
138
|
-
hash: 15
|
139
112
|
segments:
|
140
|
-
-
|
141
|
-
|
142
|
-
|
143
|
-
version: 2.1.2
|
144
|
-
type: :runtime
|
113
|
+
- 0
|
114
|
+
version: "0"
|
115
|
+
type: :development
|
145
116
|
version_requirements: *id008
|
146
|
-
description: "Messenger: easy message sending"
|
147
|
-
email: brandon@zencoder.
|
117
|
+
description: "Messenger: easy message sending for various protocols."
|
118
|
+
email: brandon@zencoder.com
|
148
119
|
executables:
|
149
120
|
- messenger
|
150
121
|
extensions: []
|
@@ -153,66 +124,48 @@ extra_rdoc_files:
|
|
153
124
|
- LICENSE
|
154
125
|
- README.markdown
|
155
126
|
files:
|
156
|
-
- README.markdown
|
157
|
-
- VERSION.yml
|
158
|
-
- lib/messenger.rb
|
159
127
|
- lib/messenger/campfire.rb
|
160
128
|
- lib/messenger/email.rb
|
161
129
|
- lib/messenger/errors.rb
|
162
130
|
- lib/messenger/jabber.rb
|
131
|
+
- lib/messenger/messenger.rb
|
163
132
|
- lib/messenger/notifo.rb
|
164
133
|
- lib/messenger/result.rb
|
134
|
+
- lib/messenger/version.rb
|
165
135
|
- lib/messenger/web.rb
|
166
|
-
-
|
167
|
-
- test/test_email.rb
|
168
|
-
- test/test_helper.rb
|
169
|
-
- test/test_jabber.rb
|
170
|
-
- test/test_messenger.rb
|
171
|
-
- test/test_notifo.rb
|
172
|
-
- test/test_result.rb
|
173
|
-
- test/test_web.rb
|
136
|
+
- lib/messenger.rb
|
174
137
|
- LICENSE
|
175
|
-
-
|
138
|
+
- README.markdown
|
139
|
+
- Rakefile
|
176
140
|
has_rdoc: true
|
177
141
|
homepage: http://github.com/zencoder/messenger
|
178
142
|
licenses: []
|
179
143
|
|
180
144
|
post_install_message:
|
181
|
-
rdoc_options:
|
182
|
-
|
145
|
+
rdoc_options: []
|
146
|
+
|
183
147
|
require_paths:
|
184
148
|
- lib
|
185
149
|
required_ruby_version: !ruby/object:Gem::Requirement
|
186
|
-
none: false
|
187
150
|
requirements:
|
188
151
|
- - ">="
|
189
152
|
- !ruby/object:Gem::Version
|
190
|
-
hash: 3
|
191
153
|
segments:
|
192
154
|
- 0
|
193
155
|
version: "0"
|
194
156
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
195
|
-
none: false
|
196
157
|
requirements:
|
197
158
|
- - ">="
|
198
159
|
- !ruby/object:Gem::Version
|
199
|
-
hash: 3
|
200
160
|
segments:
|
201
161
|
- 0
|
202
162
|
version: "0"
|
203
163
|
requirements: []
|
204
164
|
|
205
|
-
rubyforge_project:
|
206
|
-
rubygems_version: 1.3.
|
165
|
+
rubyforge_project: messenger
|
166
|
+
rubygems_version: 1.3.6
|
207
167
|
signing_key:
|
208
168
|
specification_version: 3
|
209
169
|
summary: "Messenger: easy message sending"
|
210
|
-
test_files:
|
211
|
-
|
212
|
-
- test/test_email.rb
|
213
|
-
- test/test_helper.rb
|
214
|
-
- test/test_jabber.rb
|
215
|
-
- test/test_messenger.rb
|
216
|
-
- test/test_notifo.rb
|
217
|
-
- test/test_result.rb
|
218
|
-
- test/test_web.rb
|
170
|
+
test_files: []
|
171
|
+
|
data/VERSION.yml
DELETED
data/test/test_campfire.rb
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
require 'httparty'
|
3
|
-
|
4
|
-
module Messenger
|
5
|
-
|
6
|
-
class CampfireTest < Test::Unit::TestCase
|
7
|
-
|
8
|
-
context "Campfire notification" do
|
9
|
-
setup do
|
10
|
-
@success_response = stub(:code => 200)
|
11
|
-
@failure_response = stub(:code => 500)
|
12
|
-
end
|
13
|
-
|
14
|
-
should "post a successful message" do
|
15
|
-
HTTParty.expects(:post).with("http://subdomain.campfirenow.com/room/room/speak.json", :basic_auth => { :username => 'api', :password => 'x' }, :body => '{"message":{"body":"content"}}', :headers => { "Content-Type" => "application/json" }).returns(@success_response)
|
16
|
-
result = Campfire.send("campfire://api:room@subdomain.campfirenow.com", 'content')
|
17
|
-
assert result.success?
|
18
|
-
assert_equal @success_response, result.response
|
19
|
-
end
|
20
|
-
|
21
|
-
should "post to secure URL" do
|
22
|
-
HTTParty.expects(:post).with("https://subdomain.campfirenow.com/room/room/speak.json", :basic_auth => { :username => 'api', :password => 'x' }, :body => '{"message":{"body":"content"}}', :headers => { "Content-Type" => "application/json" }).returns(@success_response)
|
23
|
-
result = Campfire.send("campfire-ssl://api:room@subdomain.campfirenow.com", 'content')
|
24
|
-
assert result.success?
|
25
|
-
assert_equal @success_response, result.response
|
26
|
-
end
|
27
|
-
|
28
|
-
should "post a failed message" do
|
29
|
-
HTTParty.expects(:post).with("http://subdomain.campfirenow.com/room/room/speak.json", :basic_auth => { :username => 'api', :password => 'x' }, :body => '{"message":{"body":"content"}}', :headers => { "Content-Type" => "application/json" }).returns(@failure_response)
|
30
|
-
result = Campfire.send("campfire://api:room@subdomain.campfirenow.com", 'content')
|
31
|
-
assert_equal false, result.success?
|
32
|
-
assert_equal @failure_response, result.response
|
33
|
-
end
|
34
|
-
|
35
|
-
should "raise when sending to an invalid URL" do
|
36
|
-
assert_raises URLError do
|
37
|
-
Campfire.send("campfire://missing_room@subdomain.campfirenow.com", 'content')
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
should "obfuscate the URL" do
|
42
|
-
assert_equal "campfire://xxxx:1234@example.campfirenow.com", Campfire.obfuscate("campfire://asdf1234:1234@example.campfirenow.com")
|
43
|
-
end
|
44
|
-
|
45
|
-
should "obfuscate a secure URL" do
|
46
|
-
assert_equal "campfire-ssl://xxxx:1234@example.campfirenow.com", Campfire.obfuscate("campfire-ssl://asdf1234:1234@example.campfirenow.com")
|
47
|
-
end
|
48
|
-
|
49
|
-
should "raise when obfuscating an invalid URL" do
|
50
|
-
assert_raises URLError do
|
51
|
-
Campfire.obfuscate("campfire://missing_room@subdomain.campfirenow.com")
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
context "Campfire URL validation" do
|
57
|
-
should "return true for good URLs" do
|
58
|
-
assert true, Campfire.valid_url?("campfire://api_key:room@subdomain.campfirenow.com")
|
59
|
-
assert true, Campfire.valid_url?("campfire-ssl://api_key:room@subdomain.campfirenow.com")
|
60
|
-
end
|
61
|
-
|
62
|
-
should "return false for bad URLs" do
|
63
|
-
assert_equal false, Campfire.valid_url?("campfire://!")
|
64
|
-
assert_equal false, Campfire.valid_url?("campfire://api_key@subdomain.campfirenow.com")
|
65
|
-
assert_equal false, Campfire.valid_url?("campfire://:room@subdomain.campfirenow.com")
|
66
|
-
assert_equal false, Campfire.valid_url?("campfire://api_key:room@subdomain")
|
67
|
-
assert_equal false, Campfire.valid_url?("campfire://api_key:room@campfirenow.com")
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
data/test/test_email.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
require 'mail'
|
3
|
-
|
4
|
-
module Messenger
|
5
|
-
|
6
|
-
class EmailTest < Test::Unit::TestCase
|
7
|
-
|
8
|
-
context "Email notification" do
|
9
|
-
setup do
|
10
|
-
Mail.defaults do
|
11
|
-
delivery_method :test
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
should "send an email" do
|
16
|
-
Email.send("mailto:to_test@example.com", "Test message", :email_from => "from_test@example.com", :email_subject => "Test")
|
17
|
-
assert_equal 1, Mail::TestMailer.deliveries.length
|
18
|
-
assert_equal ["to_test@example.com"], Mail::TestMailer.deliveries.first.to
|
19
|
-
assert_equal ["from_test@example.com"], Mail::TestMailer.deliveries.first.from
|
20
|
-
assert_equal "Test", Mail::TestMailer.deliveries.first.subject
|
21
|
-
assert_equal "Test message", Mail::TestMailer.deliveries.first.body.to_s
|
22
|
-
end
|
23
|
-
|
24
|
-
should "raise if trying to send to an invalid URL" do
|
25
|
-
assert_raises URLError do
|
26
|
-
Email.send("mailto:test", :body => "whatever", :email_from => "from_test@example.com", :email_subject => "Test")
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
should "obfuscate the URL" do
|
31
|
-
assert_equal "mailto:test@example.com", Email.obfuscate("mailto:test@example.com")
|
32
|
-
end
|
33
|
-
|
34
|
-
should "raise if trying obfuscate an invalid URL" do
|
35
|
-
assert_raises URLError do
|
36
|
-
Email.obfuscate("mailto:test")
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
context "Email notificaiton URL validation" do
|
42
|
-
should "return true for good URLs" do
|
43
|
-
assert true, Email.valid_url?("mailto:test@example.com")
|
44
|
-
end
|
45
|
-
|
46
|
-
should "return false for bad URLs" do
|
47
|
-
assert_equal false, Email.valid_url?("mailto:")
|
48
|
-
assert_equal false, Email.valid_url?("mailto:test")
|
49
|
-
assert_equal false, Email.valid_url?("mailto:example.com")
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
data/test/test_helper.rb
DELETED
data/test/test_jabber.rb
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
require 'xmpp4r-simple'
|
3
|
-
|
4
|
-
module Messenger
|
5
|
-
|
6
|
-
class JabberTest < Test::Unit::TestCase
|
7
|
-
|
8
|
-
context "Jabber notification" do
|
9
|
-
setup do
|
10
|
-
@successful_jabber = stub("jabber", :deliver => nil, :queue => stub("queue", :size => 0), :subscribed_to? => true)
|
11
|
-
@failed_jabber = stub("jabber", :deliver => nil, :queue => stub("queue", :size => 0), :subscribed_to? => false)
|
12
|
-
end
|
13
|
-
|
14
|
-
should "send a successful jabber message" do
|
15
|
-
::Jabber::Simple.expects(:new).with("notifier@zencoder.com", "asdfasdf", nil).returns(@successful_jabber)
|
16
|
-
result = Jabber.send("jabber://brandon@zencoder.com", "Test message", :jabber_id => "notifier@zencoder.com", :jabber_password => "asdfasdf")
|
17
|
-
assert result.success?
|
18
|
-
assert_nil result.response
|
19
|
-
end
|
20
|
-
|
21
|
-
should "determine and set the jabber host" do
|
22
|
-
::Jabber::Simple.expects(:new).with("notifier@zencoder.com", "asdfasdf", "host.com").returns(@successful_jabber)
|
23
|
-
result = Jabber.send("jabber://brandon@zencoder.com/host.com", "Test message", :jabber_id => "notifier@zencoder.com", :jabber_password => "asdfasdf")
|
24
|
-
assert result.success?
|
25
|
-
assert_nil result.response
|
26
|
-
end
|
27
|
-
|
28
|
-
should "fail if the recipient is not subscribed" do
|
29
|
-
::Jabber::Simple.expects(:new).with("notifier@zencoder.com", "asdfasdf", nil).returns(@failed_jabber)
|
30
|
-
result = Jabber.send("jabber://brandon@zencoder.com", "Test message", :jabber_id => "notifier@zencoder.com", :jabber_password => "asdfasdf")
|
31
|
-
assert_equal false, result.success?
|
32
|
-
assert_equal "Not yet authorized", result.response
|
33
|
-
end
|
34
|
-
|
35
|
-
should "raise when sending to an invalid URL" do
|
36
|
-
assert_raises URLError do
|
37
|
-
Jabber.send("jabber://", :jabber_id => "asdf", :jabber_password => "asdf")
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
should "obfuscate the URL" do
|
42
|
-
assert_equal "jabber://test@example.com", Jabber.obfuscate("jabber://test@example.com")
|
43
|
-
end
|
44
|
-
|
45
|
-
should "raise when obfuscating an invalid URL" do
|
46
|
-
assert_raises URLError do
|
47
|
-
Jabber.obfuscate("jabber://")
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
context "Jabber URL validation" do
|
53
|
-
should "return true for good URLs" do
|
54
|
-
assert true, Jabber.valid_url?("jabber://test@example.com")
|
55
|
-
end
|
56
|
-
|
57
|
-
should "return false for bad URLs" do
|
58
|
-
assert_equal false, Jabber.valid_url?("jabber://!")
|
59
|
-
assert_equal false, Jabber.valid_url?("jabber://test")
|
60
|
-
assert_equal false, Jabber.valid_url?("jabber://example.com")
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
data/test/test_messenger.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
|
3
|
-
module Messenger
|
4
|
-
|
5
|
-
class MessengerTest < Test::Unit::TestCase
|
6
|
-
|
7
|
-
should "determine the proper protocol" do
|
8
|
-
assert_equal :email, Messenger.protocol("mailto:test@example.com")
|
9
|
-
assert_equal :email, Messenger.protocol("test@example.com")
|
10
|
-
assert_equal :http, Messenger.protocol("http://example.com")
|
11
|
-
assert_equal :http, Messenger.protocol("https://example.com")
|
12
|
-
assert_equal :jabber, Messenger.protocol("jabber://test@example.com")
|
13
|
-
assert_equal :campfire, Messenger.protocol("campfire://api_key:room_id@subdomain.campfirenow.com")
|
14
|
-
assert_nil Messenger.protocol("bogus")
|
15
|
-
end
|
16
|
-
|
17
|
-
should "determine the proper notification handler given a protocol" do
|
18
|
-
assert_equal Email, Messenger.handler("mailto:test@example.com")
|
19
|
-
assert_equal Email, Messenger.handler("test@example.com")
|
20
|
-
assert_equal Web, Messenger.handler("http://example.com")
|
21
|
-
assert_equal Web, Messenger.handler("https://example.com")
|
22
|
-
assert_equal Jabber, Messenger.handler("jabber://test@example.com")
|
23
|
-
assert_equal Campfire, Messenger.handler("campfire://api_key:room_id@subdomain.campfirenow.com")
|
24
|
-
assert_raises Messenger::ProtocolError do
|
25
|
-
Messenger.handler("example.com")
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
should "determine valid URLs" do
|
30
|
-
assert Messenger.valid_url?("mailto:test@example.com")
|
31
|
-
assert Messenger.valid_url?("test@example.com")
|
32
|
-
assert Messenger.valid_url?("http://example.com")
|
33
|
-
assert Messenger.valid_url?("https://example.com")
|
34
|
-
assert Messenger.valid_url?("jabber://test@example.com")
|
35
|
-
assert Messenger.valid_url?("campfire://api_key:room_id@subdomain.campfirenow.com")
|
36
|
-
assert !Messenger.valid_url?("bogus")
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
data/test/test_notifo.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
require 'httparty'
|
3
|
-
|
4
|
-
module Messenger
|
5
|
-
|
6
|
-
class NotifoTest < Test::Unit::TestCase
|
7
|
-
|
8
|
-
context "Notifo notification" do
|
9
|
-
should "post a successful message" do
|
10
|
-
stub_request(:post, "https://api.notifo.com/v1/send_notification").to_return(:status => 200)
|
11
|
-
result = Notifo.send("notifo://testuser", 'message')
|
12
|
-
assert result.success?
|
13
|
-
end
|
14
|
-
|
15
|
-
should "post a failed message" do
|
16
|
-
stub_request(:post, "https://api.notifo.com/v1/send_notification").to_return(:status => 400)
|
17
|
-
result = Notifo.send("notifo://testuser", 'message')
|
18
|
-
assert !result.success?
|
19
|
-
end
|
20
|
-
|
21
|
-
should "raise when sending to an invalid URL" do
|
22
|
-
assert_raises URLError do
|
23
|
-
Notifo.send("notifo://", 'message')
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
context "Notifo URL validation" do
|
29
|
-
should "return true for good URLs" do
|
30
|
-
assert true, Notifo.valid_url?("notifo://testuser")
|
31
|
-
end
|
32
|
-
|
33
|
-
should "return false for bad URLs" do
|
34
|
-
assert_equal false, Notifo.valid_url?("notifo://")
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
data/test/test_result.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
|
3
|
-
module Messenger
|
4
|
-
|
5
|
-
class ResultTest < Test::Unit::TestCase
|
6
|
-
|
7
|
-
context "A result" do
|
8
|
-
|
9
|
-
should "return success as a boolean" do
|
10
|
-
result = Result.new(true, "Response")
|
11
|
-
assert result.success?
|
12
|
-
result = Result.new(false, "Response")
|
13
|
-
assert_equal false, result.success?
|
14
|
-
end
|
15
|
-
|
16
|
-
should "give access to the raw response" do
|
17
|
-
result = Result.new(true, "Response")
|
18
|
-
assert_equal "Response", result.response
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
data/test/test_web.rb
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
require 'httparty'
|
3
|
-
|
4
|
-
module Messenger
|
5
|
-
|
6
|
-
class WebTest < Test::Unit::TestCase
|
7
|
-
|
8
|
-
context "Web notification" do
|
9
|
-
setup do
|
10
|
-
@success_response = stub("response", :code => 200)
|
11
|
-
@failure_response = stub("response", :code => 500)
|
12
|
-
end
|
13
|
-
|
14
|
-
should "post a successful message" do
|
15
|
-
HTTParty.expects(:post).with("http://example.com", :body => '{ "key": "value" }', :headers => { "Content-Type" => "application/json" }).returns(@success_response)
|
16
|
-
result = Web.send("http://example.com", '{ "key": "value" }', :headers => { "Content-Type" => "application/json" })
|
17
|
-
assert result.success?
|
18
|
-
assert_equal @success_response, result.response
|
19
|
-
end
|
20
|
-
|
21
|
-
should "post a failed message" do
|
22
|
-
HTTParty.expects(:post).with("http://example.com", :body => '{ "key": "value" }', :headers => { "Content-Type" => "application/json" }).returns(@failure_response)
|
23
|
-
result = Web.send("http://example.com", '{ "key": "value" }', :headers => { "Content-Type" => "application/json" })
|
24
|
-
assert_equal false, result.success?
|
25
|
-
assert_equal @failure_response, result.response
|
26
|
-
end
|
27
|
-
|
28
|
-
should "raise if trying to send to an invalid URL" do
|
29
|
-
assert_raises URLError do
|
30
|
-
Web.send("http://!", :body => "whatever")
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
should "obfuscate the URL" do
|
35
|
-
assert_equal "http://example.com", Web.obfuscate("http://example.com")
|
36
|
-
assert_equal "http://user:xxxx@example.com", Web.obfuscate("http://user:secure_pass@example.com")
|
37
|
-
assert_equal "https://user:xxxx@example.com", Web.obfuscate("https://user:secure_pass@example.com")
|
38
|
-
end
|
39
|
-
|
40
|
-
should "raise if trying to obfuscate an invalid URL" do
|
41
|
-
assert_raises URLError do
|
42
|
-
Web.obfuscate("http://!")
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
context "Web notificaiton URL validation" do
|
48
|
-
should "return true for good URLs" do
|
49
|
-
assert true, Web.valid_url?("http://example.com")
|
50
|
-
assert true, Web.valid_url?("https://example.com")
|
51
|
-
assert true, Web.valid_url?("https://user@example.com")
|
52
|
-
assert true, Web.valid_url?("http://user:pass@example.com")
|
53
|
-
assert true, Web.valid_url?("https://user:#{URI.escape('!#$%^&*¢ç⎋')}@example.com")
|
54
|
-
end
|
55
|
-
|
56
|
-
should "return false for bad URLs" do
|
57
|
-
assert_equal false, Web.valid_url?("http://!")
|
58
|
-
assert_equal false, Web.valid_url?("http://")
|
59
|
-
assert_equal false, Web.valid_url?("https://")
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|