messenger 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +2 -2
- data/bin/messenger +2 -2
- data/lib/messenger.rb +10 -0
- data/lib/messenger/campfire.rb +18 -5
- data/lib/messenger/email.rb +10 -0
- data/lib/messenger/jabber.rb +19 -0
- data/lib/messenger/web.rb +19 -0
- data/test/test_campfire.rb +29 -5
- data/test/test_email.rb +29 -0
- data/test/test_jabber.rb +28 -0
- data/test/test_messenger.rb +13 -11
- data/test/test_web.rb +34 -0
- metadata +1 -1
data/VERSION.yml
CHANGED
data/bin/messenger
CHANGED
@@ -61,8 +61,8 @@ EOS
|
|
61
61
|
opt :jabber_password, "The password for your jabber id", :type => String
|
62
62
|
|
63
63
|
# CLI options
|
64
|
-
opt :silent, "Don't print anything to stdout", :default => false
|
65
|
-
opt :verbose, "Print verbose output on success", :default => false
|
64
|
+
# opt :silent, "Don't print anything to stdout", :default => false
|
65
|
+
# opt :verbose, "Print verbose output on success", :default => false
|
66
66
|
end
|
67
67
|
|
68
68
|
options.merge!(trollop_options.reject{|k,v| v.nil?})
|
data/lib/messenger.rb
CHANGED
@@ -20,6 +20,11 @@ module Messenger
|
|
20
20
|
end
|
21
21
|
|
22
22
|
|
23
|
+
def self.valid_url?(url)
|
24
|
+
service_handler = handler(url)
|
25
|
+
service_handler.valid_url?(url)
|
26
|
+
end
|
27
|
+
|
23
28
|
def self.send(url, message, options={})
|
24
29
|
service_handler = handler(url)
|
25
30
|
SystemTimer.timeout_after(options[:timeout] || 15) do
|
@@ -27,6 +32,11 @@ module Messenger
|
|
27
32
|
end
|
28
33
|
end
|
29
34
|
|
35
|
+
def self.obfuscate(url)
|
36
|
+
service_handler = handler(url)
|
37
|
+
service_handler.obfuscate(url)
|
38
|
+
end
|
39
|
+
|
30
40
|
|
31
41
|
def self.protocol(url)
|
32
42
|
# TODO: More services
|
data/lib/messenger/campfire.rb
CHANGED
@@ -5,14 +5,17 @@ module Messenger
|
|
5
5
|
|
6
6
|
class Campfire
|
7
7
|
|
8
|
+
def self.valid_url?(url)
|
9
|
+
!!matcher(url)
|
10
|
+
rescue NoMethodError
|
11
|
+
false
|
12
|
+
end
|
13
|
+
|
8
14
|
# URL format:
|
9
15
|
# campfire://api-key:room-id@subdomain.campfirenow.com
|
10
16
|
def self.send(url, body, options={})
|
11
|
-
|
12
|
-
|
13
|
-
rescue
|
14
|
-
raise URLError, "The URL provided is invalid"
|
15
|
-
end
|
17
|
+
raise URLError, "The URL provided is invalid" unless valid_url?(url)
|
18
|
+
api_key, room, subdomain = matcher(url)
|
16
19
|
response = HTTParty.post(
|
17
20
|
"http://#{subdomain}.campfirenow.com/room/#{room}/speak.json",
|
18
21
|
:basic_auth => { :username => api_key, :password => "x" },
|
@@ -22,9 +25,19 @@ module Messenger
|
|
22
25
|
[success?(response), response]
|
23
26
|
end
|
24
27
|
|
28
|
+
def self.obfuscate(url)
|
29
|
+
raise URLError, "The URL provided is invalid" unless valid_url?(url)
|
30
|
+
api_key, room, subdomain = matcher(url)
|
31
|
+
"campfire://xxxx:#{room}@#{subdomain}.campfirenow.com"
|
32
|
+
end
|
33
|
+
|
25
34
|
|
26
35
|
private
|
27
36
|
|
37
|
+
def self.matcher(url)
|
38
|
+
url.match(/^campfire:\/\/([^:]+):([^@]+)@([^\.]+).campfirenow.com/)[1,3]
|
39
|
+
end
|
40
|
+
|
28
41
|
def self.success?(response)
|
29
42
|
case response.code
|
30
43
|
when 200, 201: true
|
data/lib/messenger/email.rb
CHANGED
@@ -4,6 +4,10 @@ module Messenger
|
|
4
4
|
|
5
5
|
class Email
|
6
6
|
|
7
|
+
def self.valid_url?(url)
|
8
|
+
!!url.match(/mailto:[^@]+@.*/)
|
9
|
+
end
|
10
|
+
|
7
11
|
# URL format:
|
8
12
|
# mailto:email@example.com
|
9
13
|
#
|
@@ -11,6 +15,7 @@ module Messenger
|
|
11
15
|
# :email_from => Who the email is from
|
12
16
|
# :email_subject => The subject of the email
|
13
17
|
def self.send(url, message, options={})
|
18
|
+
raise URLError, "The URL provided is invalid" unless valid_url?(url)
|
14
19
|
mail = Mail.new do
|
15
20
|
from options[:email_from]
|
16
21
|
to url.sub(/mailto:/, '')
|
@@ -21,6 +26,11 @@ module Messenger
|
|
21
26
|
[true, nil]
|
22
27
|
end
|
23
28
|
|
29
|
+
def self.obfuscate(url)
|
30
|
+
raise URLError, "The URL provided is invalid" unless valid_url?(url)
|
31
|
+
url
|
32
|
+
end
|
33
|
+
|
24
34
|
end
|
25
35
|
|
26
36
|
end
|
data/lib/messenger/jabber.rb
CHANGED
@@ -4,6 +4,12 @@ module Messenger
|
|
4
4
|
|
5
5
|
class Jabber
|
6
6
|
|
7
|
+
def self.valid_url?(url)
|
8
|
+
!!matcher(url)
|
9
|
+
rescue NoMethodError
|
10
|
+
false
|
11
|
+
end
|
12
|
+
|
7
13
|
# URL format:
|
8
14
|
# jabber://email@example.com/server_hostname
|
9
15
|
#
|
@@ -13,6 +19,7 @@ module Messenger
|
|
13
19
|
# :jabber_id => The jabber id of the sender
|
14
20
|
# :jabber_password => The password of the sender
|
15
21
|
def self.send(url, body, options={})
|
22
|
+
raise URLError, "The URL provided is invalid" unless valid_url?(url)
|
16
23
|
recipient, host = url.sub("jabber://", "").split("/")[0,2]
|
17
24
|
jabber = ::Jabber::Simple.new(options[:jabber_id], options[:jabber_password], host)
|
18
25
|
jabber.deliver(recipient, body)
|
@@ -25,6 +32,18 @@ module Messenger
|
|
25
32
|
[status, status ? nil : "Not yet authorized"]
|
26
33
|
end
|
27
34
|
|
35
|
+
def self.obfuscate(url)
|
36
|
+
raise URLError, "The URL provided is invalid" unless valid_url?(url)
|
37
|
+
url
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def self.matcher(url)
|
44
|
+
url.sub("jabber://", "").match("@")
|
45
|
+
end
|
46
|
+
|
28
47
|
end
|
29
48
|
|
30
49
|
end
|
data/lib/messenger/web.rb
CHANGED
@@ -4,15 +4,34 @@ module Messenger
|
|
4
4
|
|
5
5
|
class Web
|
6
6
|
|
7
|
+
def self.valid_url?(url)
|
8
|
+
!!URI.parse(url)
|
9
|
+
rescue URI::InvalidURIError
|
10
|
+
false
|
11
|
+
end
|
12
|
+
|
7
13
|
# URL format:
|
8
14
|
# http://example.com
|
15
|
+
# https://user:pass@example.com
|
9
16
|
#
|
10
17
|
# The body of the message is posted as the body of the request, not the query.
|
11
18
|
def self.send(url, body, options={})
|
19
|
+
raise URLError, "The URL provided is invalid" unless self.valid_url?(url)
|
12
20
|
response = HTTParty.post(url, options.merge(:body => body))
|
13
21
|
[success?(response), response]
|
14
22
|
end
|
15
23
|
|
24
|
+
def self.obfuscate(url)
|
25
|
+
raise URLError, "The URL provided is invalid" unless self.valid_url?(url)
|
26
|
+
path = URI.parse(url)
|
27
|
+
if path.password
|
28
|
+
url.sub(/#{path.password}/, 'xxxx')
|
29
|
+
else
|
30
|
+
url
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
16
35
|
private
|
17
36
|
|
18
37
|
def self.success?(response)
|
data/test/test_campfire.rb
CHANGED
@@ -13,21 +13,45 @@ module Messenger
|
|
13
13
|
|
14
14
|
should "post a successful message" do
|
15
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.
|
16
|
+
result = Campfire.send("campfire://api:room@subdomain.campfirenow.com", 'content')
|
17
17
|
assert_equal [true, @success_response], result
|
18
18
|
end
|
19
19
|
|
20
20
|
should "post a failed message" do
|
21
21
|
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)
|
22
|
-
result = Campfire.send("campfire://api:room@subdomain.
|
22
|
+
result = Campfire.send("campfire://api:room@subdomain.campfirenow.com", 'content')
|
23
23
|
assert_equal [false, @failure_response], result
|
24
24
|
end
|
25
25
|
|
26
|
-
should "raise
|
27
|
-
assert_raises
|
28
|
-
Campfire.send("campfire://missing_room@subdomain.
|
26
|
+
should "raise when sending to an invalid URL" do
|
27
|
+
assert_raises URLError do
|
28
|
+
Campfire.send("campfire://missing_room@subdomain.campfirenow.com", 'content')
|
29
29
|
end
|
30
30
|
end
|
31
|
+
|
32
|
+
should "obfuscate the URL" do
|
33
|
+
assert_equal "campfire://xxxx:1234@example.campfirenow.com", Campfire.obfuscate("campfire://asdf1234:1234@example.campfirenow.com")
|
34
|
+
end
|
35
|
+
|
36
|
+
should "raise when obfuscating an invalid URL" do
|
37
|
+
assert_raises URLError do
|
38
|
+
Campfire.obfuscate("campfire://missing_room@subdomain.campfirenow.com")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "Campfire URL validation" do
|
44
|
+
should "return true for good URLs" do
|
45
|
+
assert true, Campfire.valid_url?("campfire://api_key:room@subdomain.campfirenow.com")
|
46
|
+
end
|
47
|
+
|
48
|
+
should "return false for bad URLs" do
|
49
|
+
assert_equal false, Campfire.valid_url?("campfire://!")
|
50
|
+
assert_equal false, Campfire.valid_url?("campfire://api_key@subdomain.campfirenow.com")
|
51
|
+
assert_equal false, Campfire.valid_url?("campfire://:room@subdomain.campfirenow.com")
|
52
|
+
assert_equal false, Campfire.valid_url?("campfire://api_key:room@subdomain")
|
53
|
+
assert_equal false, Campfire.valid_url?("campfire://api_key:room@campfirenow.com")
|
54
|
+
end
|
31
55
|
end
|
32
56
|
|
33
57
|
end
|
data/test/test_email.rb
CHANGED
@@ -20,6 +20,35 @@ module Messenger
|
|
20
20
|
assert_equal "Test", Mail::TestMailer.deliveries.first.subject
|
21
21
|
assert_equal "Test message", Mail::TestMailer.deliveries.first.body.to_s
|
22
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
|
+
assert_equal false, Email.valid_url?("mailto:example.com")
|
51
|
+
end
|
23
52
|
end
|
24
53
|
|
25
54
|
end
|
data/test/test_jabber.rb
CHANGED
@@ -28,6 +28,34 @@ module Messenger
|
|
28
28
|
result = Jabber.send("jabber://brandon@zencoder.com", "Test message", :jabber_id => "notifier@zencoder.com", :jabber_password => "asdfasdf")
|
29
29
|
assert_equal [false, "Not yet authorized"], result
|
30
30
|
end
|
31
|
+
|
32
|
+
should "raise when sending to an invalid URL" do
|
33
|
+
assert_raises URLError do
|
34
|
+
Jabber.send("jabber://", :jabber_id => "asdf", :jabber_password => "asdf")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
should "obfuscate the URL" do
|
39
|
+
assert_equal "jabber://test@example.com", Jabber.obfuscate("jabber://test@example.com")
|
40
|
+
end
|
41
|
+
|
42
|
+
should "raise when obfuscating an invalid URL" do
|
43
|
+
assert_raises URLError do
|
44
|
+
Jabber.obfuscate("jabber://")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "Jabber URL validation" do
|
50
|
+
should "return true for good URLs" do
|
51
|
+
assert true, Jabber.valid_url?("jabber://test@example.com")
|
52
|
+
end
|
53
|
+
|
54
|
+
should "return false for bad URLs" do
|
55
|
+
assert_equal false, Jabber.valid_url?("jabber://!")
|
56
|
+
assert_equal false, Jabber.valid_url?("jabber://test")
|
57
|
+
assert_equal false, Jabber.valid_url?("jabber://example.com")
|
58
|
+
end
|
31
59
|
end
|
32
60
|
|
33
61
|
end
|
data/test/test_messenger.rb
CHANGED
@@ -4,18 +4,20 @@ module Messenger
|
|
4
4
|
|
5
5
|
class MessengerTest < Test::Unit::TestCase
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
7
|
+
should "determine the proper protocol" do
|
8
|
+
assert_equal :email, Messenger.protocol("mailto:test@example.com")
|
9
|
+
assert_equal :http, Messenger.protocol("http://example.com")
|
10
|
+
assert_equal :http, Messenger.protocol("https://example.com")
|
11
|
+
assert_equal :jabber, Messenger.protocol("jabber://test@example.com")
|
12
|
+
assert_equal :campfire, Messenger.protocol("campfire://api_key:room_id@subdomain.campfirenow.com")
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
should "determine the proper notification handler given a protocol" do
|
16
|
+
assert_equal Email, Messenger.handler("mailto:test@example.com")
|
17
|
+
assert_equal Web, Messenger.handler("http://example.com")
|
18
|
+
assert_equal Web, Messenger.handler("https://example.com")
|
19
|
+
assert_equal Jabber, Messenger.handler("jabber://test@example.com")
|
20
|
+
assert_equal Campfire, Messenger.handler("campfire://api_key:room_id@subdomain.campfirenow.com")
|
19
21
|
end
|
20
22
|
|
21
23
|
end
|
data/test/test_web.rb
CHANGED
@@ -22,6 +22,40 @@ module Messenger
|
|
22
22
|
result = Web.send("http://example.com", '{ "key": "value" }', :headers => { "Content-Type" => "application/json" })
|
23
23
|
assert_equal [false, @failure_response], result
|
24
24
|
end
|
25
|
+
|
26
|
+
should "raise if trying to send to an invalid URL" do
|
27
|
+
assert_raises URLError do
|
28
|
+
Web.send("http://!", :body => "whatever")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
should "obfuscate the URL" do
|
33
|
+
assert_equal "http://example.com", Web.obfuscate("http://example.com")
|
34
|
+
assert_equal "http://user:xxxx@example.com", Web.obfuscate("http://user:secure_pass@example.com")
|
35
|
+
assert_equal "https://user:xxxx@example.com", Web.obfuscate("https://user:secure_pass@example.com")
|
36
|
+
end
|
37
|
+
|
38
|
+
should "raise if trying to obfuscate an invalid URL" do
|
39
|
+
assert_raises URLError do
|
40
|
+
Web.obfuscate("http://!")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "Web notificaiton URL validation" do
|
46
|
+
should "return true for good URLs" do
|
47
|
+
assert true, Web.valid_url?("http://example.com")
|
48
|
+
assert true, Web.valid_url?("https://example.com")
|
49
|
+
assert true, Web.valid_url?("https://user@example.com")
|
50
|
+
assert true, Web.valid_url?("http://user:pass@example.com")
|
51
|
+
assert true, Web.valid_url?("https://user:#{URI.escape('!#$%^&*¢ç⎋')}@example.com")
|
52
|
+
end
|
53
|
+
|
54
|
+
should "return false for bad URLs" do
|
55
|
+
assert_equal false, Web.valid_url?("http://!")
|
56
|
+
assert_equal false, Web.valid_url?("http://")
|
57
|
+
assert_equal false, Web.valid_url?("https://")
|
58
|
+
end
|
25
59
|
end
|
26
60
|
|
27
61
|
end
|