messenger 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  ---
2
- :patch: 1
3
- :major: 0
2
+ :minor: 2
4
3
  :build:
5
- :minor: 1
4
+ :patch: 0
5
+ :major: 0
@@ -60,6 +60,12 @@ EOS
60
60
  opt :jabber_id, "The jabber ID to connect as (user@example.com)", :type => String
61
61
  opt :jabber_password, "The password for your jabber id", :type => String
62
62
 
63
+ # Notifo options
64
+ opt :notifo_api_username, "The service's API username", :type => String
65
+ opt :notifo_api_secret, "The service's API secret", :type => String
66
+ opt :notifo_title, "The notificaiton title", :type => String
67
+ opt :notifo_url, "Open this URL", :type => String
68
+
63
69
  # CLI options
64
70
  # opt :silent, "Don't print anything to stdout", :default => false
65
71
  # opt :verbose, "Print verbose output on success", :default => false
@@ -1,7 +1,6 @@
1
1
  $:.unshift File.dirname(__FILE__)
2
2
 
3
3
  require 'rubygems'
4
- require 'ruby-debug'
5
4
  require 'messenger/errors'
6
5
  require 'system_timer'
7
6
 
@@ -23,6 +22,8 @@ module Messenger
23
22
  def self.valid_url?(url)
24
23
  service_handler = handler(url)
25
24
  service_handler.valid_url?(url)
25
+ rescue ProtocolError
26
+ false
26
27
  end
27
28
 
28
29
  def self.send(url, message, options={})
@@ -44,10 +45,11 @@ module Messenger
44
45
  # twitter://username
45
46
  # aim://username
46
47
  case url
47
- when /^mailto/: :email
48
- when /^http/: :http
49
- when /^campfire/: :campfire
50
- when /^jabber/: :jabber
48
+ when /^http/: :http
49
+ when /^campfire/: :campfire
50
+ when /^jabber/: :jabber
51
+ when /^notifo/: :notifo
52
+ when /^mailto|@+/: :email
51
53
  end
52
54
  end
53
55
 
@@ -57,6 +59,7 @@ module Messenger
57
59
  when :http: Web
58
60
  when :campfire: Campfire
59
61
  when :jabber: Jabber
62
+ when :notifo: Notifo
60
63
  else
61
64
  raise ProtocolError, "Malformed service URL: #{url}. Either this syntax is wrong or this service type is not yet implemented."
62
65
  end
@@ -68,5 +71,6 @@ module Messenger
68
71
  autoload :Web, "messenger/web"
69
72
  autoload :Campfire, "messenger/campfire"
70
73
  autoload :Jabber, "messenger/jabber"
74
+ autoload :Notifo, "messenger/notifo"
71
75
 
72
76
  end
@@ -15,9 +15,9 @@ module Messenger
15
15
  # campfire://api-key:room-id@subdomain.campfirenow.com
16
16
  def self.send(url, body, options={})
17
17
  raise URLError, "The URL provided is invalid" unless valid_url?(url)
18
- api_key, room, subdomain = matcher(url)
18
+ ssl, api_key, room, subdomain = matcher(url)
19
19
  response = HTTParty.post(
20
- "http://#{subdomain}.campfirenow.com/room/#{room}/speak.json",
20
+ "http#{ssl ? "s" : ""}://#{subdomain}.campfirenow.com/room/#{room}/speak.json",
21
21
  :basic_auth => { :username => api_key, :password => "x" },
22
22
  :headers => { "Content-Type" => "application/json" },
23
23
  :body => { "message" => { "body" => body } }.to_json
@@ -27,15 +27,15 @@ module Messenger
27
27
 
28
28
  def self.obfuscate(url)
29
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"
30
+ ssl, api_key, room, subdomain = matcher(url)
31
+ "campfire#{ssl ? "-ssl" : ""}://xxxx:#{room}@#{subdomain}.campfirenow.com"
32
32
  end
33
33
 
34
34
 
35
35
  private
36
36
 
37
37
  def self.matcher(url)
38
- url.match(/^campfire:\/\/([^:]+):([^@]+)@([^\.]+).campfirenow.com/)[1,3]
38
+ url.match(/^campfire(-ssl)?:\/\/([^:]+):([^@]+)@([^\.]+).campfirenow.com/)[1,4]
39
39
  end
40
40
 
41
41
  def self.success?(response)
@@ -5,7 +5,7 @@ module Messenger
5
5
  class Email
6
6
 
7
7
  def self.valid_url?(url)
8
- !!url.match(/mailto:[^@]+@.*/)
8
+ !!url.match(/^(mailto:)?[^@]+@.*$/)
9
9
  end
10
10
 
11
11
  # URL format:
@@ -24,6 +24,8 @@ module Messenger
24
24
  end
25
25
  mail.deliver!
26
26
  Result.new(true, nil)
27
+ rescue Errno::ECONNREFUSED, Errno::EAFNOSUPPORT => e
28
+ Result.new(false, e)
27
29
  end
28
30
 
29
31
  def self.obfuscate(url)
@@ -0,0 +1,47 @@
1
+ require 'httparty'
2
+ require 'json'
3
+
4
+ module Messenger
5
+
6
+ class Notifo
7
+
8
+ def self.valid_url?(url)
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
28
+
29
+ def self.obfuscate(url)
30
+ raise URLError, "The URL provided is invalid" unless valid_url?(url)
31
+ url
32
+ end
33
+
34
+
35
+ private
36
+
37
+ def self.matcher(url)
38
+ url.match(/^notifo:\/\/(.+)/)[1]
39
+ end
40
+
41
+ def self.success?(response)
42
+ response.code == 200
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -13,6 +13,18 @@ module Messenger
13
13
  !!@success
14
14
  end
15
15
 
16
+ def code
17
+ response.code rescue nil
18
+ end
19
+
20
+ def body
21
+ if response.respond_to?(:body)
22
+ response.body
23
+ else
24
+ response.to_s
25
+ end
26
+ end
27
+
16
28
  end
17
29
 
18
30
  end
@@ -1,4 +1,4 @@
1
- require "#{File.dirname(__FILE__)}/test_helper"
1
+ require "test_helper"
2
2
  require 'httparty'
3
3
 
4
4
  module Messenger
@@ -7,8 +7,8 @@ module Messenger
7
7
 
8
8
  context "Campfire notification" do
9
9
  setup do
10
- @success_response = stub("response", :code => 200)
11
- @failure_response = stub("response", :code => 500)
10
+ @success_response = stub(:code => 200)
11
+ @failure_response = stub(:code => 500)
12
12
  end
13
13
 
14
14
  should "post a successful message" do
@@ -18,6 +18,13 @@ module Messenger
18
18
  assert_equal @success_response, result.response
19
19
  end
20
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
+
21
28
  should "post a failed message" do
22
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)
23
30
  result = Campfire.send("campfire://api:room@subdomain.campfirenow.com", 'content')
@@ -35,6 +42,10 @@ module Messenger
35
42
  assert_equal "campfire://xxxx:1234@example.campfirenow.com", Campfire.obfuscate("campfire://asdf1234:1234@example.campfirenow.com")
36
43
  end
37
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
+
38
49
  should "raise when obfuscating an invalid URL" do
39
50
  assert_raises URLError do
40
51
  Campfire.obfuscate("campfire://missing_room@subdomain.campfirenow.com")
@@ -45,6 +56,7 @@ module Messenger
45
56
  context "Campfire URL validation" do
46
57
  should "return true for good URLs" do
47
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")
48
60
  end
49
61
 
50
62
  should "return false for bad URLs" do
@@ -1,4 +1,4 @@
1
- require "#{File.dirname(__FILE__)}/test_helper"
1
+ require "test_helper"
2
2
  require 'mail'
3
3
 
4
4
  module Messenger
@@ -46,7 +46,6 @@ module Messenger
46
46
  should "return false for bad URLs" do
47
47
  assert_equal false, Email.valid_url?("mailto:")
48
48
  assert_equal false, Email.valid_url?("mailto:test")
49
- assert_equal false, Email.valid_url?("mailto:@example.com")
50
49
  assert_equal false, Email.valid_url?("mailto:example.com")
51
50
  end
52
51
  end
@@ -1,6 +1,11 @@
1
- require "#{File.dirname(__FILE__)}/../lib/messenger"
1
+ require "messenger"
2
2
  require 'test/unit'
3
3
  require 'shoulda'
4
4
  require 'mocha'
5
+ require 'webmock/test_unit'
6
+
7
+ class Test::Unit::TestCase
8
+ include WebMock
9
+ end
5
10
 
6
11
  Dir["#{File.dirname(__FILE__)}/shoulda_macros/*.rb"].each {|file| require file }
@@ -1,4 +1,4 @@
1
- require "#{File.dirname(__FILE__)}/test_helper"
1
+ require "test_helper"
2
2
  require 'xmpp4r-simple'
3
3
 
4
4
  module Messenger
@@ -1,4 +1,4 @@
1
- require "#{File.dirname(__FILE__)}/test_helper"
1
+ require "test_helper"
2
2
 
3
3
  module Messenger
4
4
 
@@ -6,18 +6,34 @@ module Messenger
6
6
 
7
7
  should "determine the proper protocol" do
8
8
  assert_equal :email, Messenger.protocol("mailto:test@example.com")
9
+ assert_equal :email, Messenger.protocol("test@example.com")
9
10
  assert_equal :http, Messenger.protocol("http://example.com")
10
11
  assert_equal :http, Messenger.protocol("https://example.com")
11
12
  assert_equal :jabber, Messenger.protocol("jabber://test@example.com")
12
13
  assert_equal :campfire, Messenger.protocol("campfire://api_key:room_id@subdomain.campfirenow.com")
14
+ assert_nil Messenger.protocol("bogus")
13
15
  end
14
16
 
15
17
  should "determine the proper notification handler given a protocol" do
16
18
  assert_equal Email, Messenger.handler("mailto:test@example.com")
19
+ assert_equal Email, Messenger.handler("test@example.com")
17
20
  assert_equal Web, Messenger.handler("http://example.com")
18
21
  assert_equal Web, Messenger.handler("https://example.com")
19
22
  assert_equal Jabber, Messenger.handler("jabber://test@example.com")
20
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")
21
37
  end
22
38
 
23
39
  end
@@ -0,0 +1,40 @@
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
@@ -1,4 +1,4 @@
1
- require "#{File.dirname(__FILE__)}/test_helper"
1
+ require "test_helper"
2
2
 
3
3
  module Messenger
4
4
 
@@ -1,4 +1,4 @@
1
- require "#{File.dirname(__FILE__)}/test_helper"
1
+ require "test_helper"
2
2
  require 'httparty'
3
3
 
4
4
  module Messenger
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: messenger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - Brandon Arbini
@@ -9,89 +15,134 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2010-02-08 00:00:00 -08:00
18
+ date: 2010-06-17 00:00:00 -05:00
13
19
  default_executable: messenger
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
22
  name: trollop
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
20
26
  requirements:
21
27
  - - ">="
22
28
  - !ruby/object:Gem::Version
29
+ hash: 17
30
+ segments:
31
+ - 1
32
+ - 15
23
33
  version: "1.15"
24
- version:
34
+ type: :runtime
35
+ version_requirements: *id001
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: mime-types
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
30
41
  requirements:
31
42
  - - ">="
32
43
  - !ruby/object:Gem::Version
44
+ hash: 47
45
+ segments:
46
+ - 1
47
+ - 16
33
48
  version: "1.16"
34
- version:
49
+ type: :runtime
50
+ version_requirements: *id002
35
51
  - !ruby/object:Gem::Dependency
36
52
  name: httparty
37
- type: :runtime
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
40
56
  requirements:
41
57
  - - ">="
42
58
  - !ruby/object:Gem::Version
59
+ hash: 15
60
+ segments:
61
+ - 0
62
+ - 5
63
+ - 2
43
64
  version: 0.5.2
44
- version:
65
+ type: :runtime
66
+ version_requirements: *id003
45
67
  - !ruby/object:Gem::Dependency
46
68
  name: SystemTimer
47
- type: :runtime
48
- version_requirement:
49
- version_requirements: !ruby/object:Gem::Requirement
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
50
72
  requirements:
51
73
  - - ">="
52
74
  - !ruby/object:Gem::Version
75
+ hash: 21
76
+ segments:
77
+ - 1
78
+ - 1
79
+ - 3
53
80
  version: 1.1.3
54
- version:
81
+ type: :runtime
82
+ version_requirements: *id004
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: xmpp4r
57
- type: :runtime
58
- version_requirement:
59
- version_requirements: !ruby/object:Gem::Requirement
85
+ prerelease: false
86
+ requirement: &id005 !ruby/object:Gem::Requirement
87
+ none: false
60
88
  requirements:
61
89
  - - "="
62
90
  - !ruby/object:Gem::Version
91
+ hash: 1
92
+ segments:
93
+ - 0
94
+ - 5
63
95
  version: "0.5"
64
- version:
96
+ type: :runtime
97
+ version_requirements: *id005
65
98
  - !ruby/object:Gem::Dependency
66
99
  name: xmpp4r-simple
67
- type: :runtime
68
- version_requirement:
69
- version_requirements: !ruby/object:Gem::Requirement
100
+ prerelease: false
101
+ requirement: &id006 !ruby/object:Gem::Requirement
102
+ none: false
70
103
  requirements:
71
104
  - - "="
72
105
  - !ruby/object:Gem::Version
106
+ hash: 47
107
+ segments:
108
+ - 0
109
+ - 8
110
+ - 8
73
111
  version: 0.8.8
74
- version:
112
+ type: :runtime
113
+ version_requirements: *id006
75
114
  - !ruby/object:Gem::Dependency
76
115
  name: json_pure
77
- type: :runtime
78
- version_requirement:
79
- version_requirements: !ruby/object:Gem::Requirement
116
+ prerelease: false
117
+ requirement: &id007 !ruby/object:Gem::Requirement
118
+ none: false
80
119
  requirements:
81
120
  - - ">="
82
121
  - !ruby/object:Gem::Version
122
+ hash: 31
123
+ segments:
124
+ - 1
125
+ - 2
126
+ - 0
83
127
  version: 1.2.0
84
- version:
128
+ type: :runtime
129
+ version_requirements: *id007
85
130
  - !ruby/object:Gem::Dependency
86
131
  name: mail
87
- type: :runtime
88
- version_requirement:
89
- version_requirements: !ruby/object:Gem::Requirement
132
+ prerelease: false
133
+ requirement: &id008 !ruby/object:Gem::Requirement
134
+ none: false
90
135
  requirements:
91
136
  - - ">="
92
137
  - !ruby/object:Gem::Version
138
+ hash: 15
139
+ segments:
140
+ - 2
141
+ - 1
142
+ - 2
93
143
  version: 2.1.2
94
- version:
144
+ type: :runtime
145
+ version_requirements: *id008
95
146
  description: "Messenger: easy message sending"
96
147
  email: brandon@zencoder.tv
97
148
  executables:
@@ -109,6 +160,7 @@ files:
109
160
  - lib/messenger/email.rb
110
161
  - lib/messenger/errors.rb
111
162
  - lib/messenger/jabber.rb
163
+ - lib/messenger/notifo.rb
112
164
  - lib/messenger/result.rb
113
165
  - lib/messenger/web.rb
114
166
  - test/test_campfire.rb
@@ -116,9 +168,11 @@ files:
116
168
  - test/test_helper.rb
117
169
  - test/test_jabber.rb
118
170
  - test/test_messenger.rb
171
+ - test/test_notifo.rb
119
172
  - test/test_result.rb
120
173
  - test/test_web.rb
121
174
  - LICENSE
175
+ - bin/messenger
122
176
  has_rdoc: true
123
177
  homepage: http://github.com/zencoder/messenger
124
178
  licenses: []
@@ -129,21 +183,27 @@ rdoc_options:
129
183
  require_paths:
130
184
  - lib
131
185
  required_ruby_version: !ruby/object:Gem::Requirement
186
+ none: false
132
187
  requirements:
133
188
  - - ">="
134
189
  - !ruby/object:Gem::Version
190
+ hash: 3
191
+ segments:
192
+ - 0
135
193
  version: "0"
136
- version:
137
194
  required_rubygems_version: !ruby/object:Gem::Requirement
195
+ none: false
138
196
  requirements:
139
197
  - - ">="
140
198
  - !ruby/object:Gem::Version
199
+ hash: 3
200
+ segments:
201
+ - 0
141
202
  version: "0"
142
- version:
143
203
  requirements: []
144
204
 
145
205
  rubyforge_project:
146
- rubygems_version: 1.3.5
206
+ rubygems_version: 1.3.7
147
207
  signing_key:
148
208
  specification_version: 3
149
209
  summary: "Messenger: easy message sending"
@@ -153,5 +213,6 @@ test_files:
153
213
  - test/test_helper.rb
154
214
  - test/test_jabber.rb
155
215
  - test/test_messenger.rb
216
+ - test/test_notifo.rb
156
217
  - test/test_result.rb
157
218
  - test/test_web.rb