rjab 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -0
- data/README.markdown +93 -0
- data/Rakefile +13 -0
- data/VERSION +1 -0
- data/lib/jabbify.rb +3 -0
- data/lib/jabbify/comet.rb +81 -0
- data/rjab.gemspec +53 -0
- data/spec/jabbify/comet_spec.rb +186 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +4 -0
- metadata +74 -0
data/.gitignore
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
RJab: a Ruby library for Jabbify
|
2
|
+
================================
|
3
|
+
|
4
|
+
[Jabbify][jabbify] is a simple messaging service that one can plug into their website with a few lines of Javascript. At the heart of Jabbify is a centralized, ready-to-use Comet server.
|
5
|
+
|
6
|
+
You can [utilize their Comet service][jabbify_comet] via the Javascript API, or by creating a simple GET or POST request along with the necessary parameters. This library attempts to simplify creation of that request.
|
7
|
+
|
8
|
+
Installing the Gem
|
9
|
+
------------------
|
10
|
+
|
11
|
+
$ sudo gem install rjab
|
12
|
+
|
13
|
+
Utilizing the Library
|
14
|
+
---------------------
|
15
|
+
|
16
|
+
require 'rubygems'
|
17
|
+
require 'jabbify'
|
18
|
+
|
19
|
+
If you only make one message delivery in your app, you might find it easiest to use the class method:
|
20
|
+
|
21
|
+
Jabbify::Comet.deliver(
|
22
|
+
:api_key => 'YourApiKeyGoesHere',
|
23
|
+
:type => :type_of_delivery,
|
24
|
+
:action => :action_of_delivery,
|
25
|
+
:name => 'John Doe',
|
26
|
+
:message => 'This is the message!',
|
27
|
+
:to => 'Jane Doe',
|
28
|
+
)
|
29
|
+
|
30
|
+
More commonly, you will instantiate an instance of `Jabbify::Comet` with a few customized attributes. Keep in mind that there a few **default attributes** set for you; they are denoted below. Then you can just call `#deliver` whenever you're ready, passing in a couple more attributes which will *only be used during the delivery*:
|
31
|
+
|
32
|
+
custom_attributes = {
|
33
|
+
:api_key => 'YourApiKeyGoesHere',
|
34
|
+
:name => 'The Server'
|
35
|
+
}
|
36
|
+
|
37
|
+
@comet = Jabbify::Comet.new(custom_attributes)
|
38
|
+
|
39
|
+
if @comet.deliver(:message => "A special message at #{ Time.now.to_s }")
|
40
|
+
# do something
|
41
|
+
else
|
42
|
+
# do something else
|
43
|
+
end
|
44
|
+
|
45
|
+
Default Attributes on the `Jabbify::Comet` Class
|
46
|
+
------------------------------------------------
|
47
|
+
|
48
|
+
<table id="rjab_default_attributes">
|
49
|
+
<thead>
|
50
|
+
<tr>
|
51
|
+
<th>Attribute</th>
|
52
|
+
<th>Default Value</th>
|
53
|
+
<th>Description</th>
|
54
|
+
</tr>
|
55
|
+
</thead>
|
56
|
+
<tbody>
|
57
|
+
<tr>
|
58
|
+
<td><code>:action</code></td>
|
59
|
+
<td><code>:create</code></td>
|
60
|
+
<td>The action you are performing against the type, as a <strong>symbol</strong>. Commonly a RESTful action.</td>
|
61
|
+
</tr>
|
62
|
+
<tr>
|
63
|
+
<td><code>:api_key</code></td>
|
64
|
+
<td><code>nil</code></td>
|
65
|
+
<td><em>Required.</em> The API key provided to you when you sign up with Jabbify.</td>
|
66
|
+
</tr>
|
67
|
+
<tr>
|
68
|
+
<td><code>:message</code></td>
|
69
|
+
<td><code>nil</code></td>
|
70
|
+
<td><em>Required.</em> The message to deliver to the Comet server.</td>
|
71
|
+
</tr>
|
72
|
+
<tr>
|
73
|
+
<td><code>:name</code></td>
|
74
|
+
<td><code>'Server'</code></td>
|
75
|
+
<td><em>Required.</em> The sender of the message.</td>
|
76
|
+
</tr>
|
77
|
+
<tr>
|
78
|
+
<td><code>:to</code></td>
|
79
|
+
<td><code>nil</code></td>
|
80
|
+
<td>The recipient of the message. Not usually specified, as most messages are sent to all users.</td>
|
81
|
+
</tr>
|
82
|
+
<tr>
|
83
|
+
<td><code>:type</code></td>
|
84
|
+
<td><code>:message</code></td>
|
85
|
+
<td>The type of resource you are working with, as a <strong>symbol</strong>.</td>
|
86
|
+
</tr>
|
87
|
+
</tbody>
|
88
|
+
</table>
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
[jabbify]: http://jabbify.com
|
93
|
+
[jabbify_comet]: https://jabbify.com/home/comet_service
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'jeweler'
|
2
|
+
|
3
|
+
Jeweler::Tasks.new do |gemspec|
|
4
|
+
gemspec.name = 'rjab'
|
5
|
+
gemspec.summary = %Q{A Ruby library for interacting with Jabbify.}
|
6
|
+
gemspec.description = %Q{A Ruby library for interacting with Jabbify. Simplifies the process of delivering messages to Jabbify's Comet server.}
|
7
|
+
gemspec.email = 'matt+rjab@puchlerz.com'
|
8
|
+
gemspec.homepage = 'http://github.com/mattpuchlerz/rjab'
|
9
|
+
gemspec.authors = [ 'Matt Puchlerz' ]
|
10
|
+
gemspec.add_dependency 'rest-client', '>= 0.9.2'
|
11
|
+
end
|
12
|
+
|
13
|
+
Jeweler::GemcutterTasks.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.2
|
data/lib/jabbify.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'restclient' # http://github.com/adamwiggins/rest-client
|
3
|
+
|
4
|
+
module Jabbify
|
5
|
+
class Comet
|
6
|
+
|
7
|
+
DEFAULT_ATTRIBUTES = {
|
8
|
+
:action => :create,
|
9
|
+
:api_key => nil,
|
10
|
+
:message => nil,
|
11
|
+
:name => 'Server',
|
12
|
+
:to => nil,
|
13
|
+
:type => :message,
|
14
|
+
}
|
15
|
+
|
16
|
+
def initialize(custom_attributes = {})
|
17
|
+
@customized_attributes = DEFAULT_ATTRIBUTES.merge custom_attributes
|
18
|
+
end
|
19
|
+
|
20
|
+
def attributes
|
21
|
+
@customized_attributes
|
22
|
+
end
|
23
|
+
|
24
|
+
def deliver(overridden_attributes = {})
|
25
|
+
customized_attributes = @customized_attributes.dup
|
26
|
+
@customized_attributes.merge! overridden_attributes
|
27
|
+
return_value = false
|
28
|
+
|
29
|
+
if valid?
|
30
|
+
begin
|
31
|
+
RestClient.post jabbify_uri, uri_params
|
32
|
+
return_value = true
|
33
|
+
rescue
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
@customized_attributes = customized_attributes
|
38
|
+
return_value
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.deliver(options)
|
42
|
+
new(options).deliver
|
43
|
+
end
|
44
|
+
|
45
|
+
def jabbify_uri
|
46
|
+
'https://jabbify.com:8443/message_push'
|
47
|
+
end
|
48
|
+
|
49
|
+
def uri_params
|
50
|
+
{
|
51
|
+
:action => @customized_attributes[:action],
|
52
|
+
:key => @customized_attributes[:api_key],
|
53
|
+
:message => @customized_attributes[:message],
|
54
|
+
:name => @customized_attributes[:name],
|
55
|
+
:to => @customized_attributes[:to],
|
56
|
+
:type => @customized_attributes[:type],
|
57
|
+
}.reject { |key, value| value.nil? }
|
58
|
+
end
|
59
|
+
|
60
|
+
def valid?
|
61
|
+
[ :api_key, :name, :message ].each do |attribute|
|
62
|
+
return false if @customized_attributes[attribute].nil? or @customized_attributes[attribute].strip.length == 0
|
63
|
+
end
|
64
|
+
true
|
65
|
+
end
|
66
|
+
|
67
|
+
DEFAULT_ATTRIBUTES.keys.each do |attribute|
|
68
|
+
|
69
|
+
define_method attribute do
|
70
|
+
attributes[attribute]
|
71
|
+
end
|
72
|
+
|
73
|
+
define_method "#{attribute}=" do |value|
|
74
|
+
value = value.to_sym if [ :action, :type ].include? attribute
|
75
|
+
@customized_attributes[attribute] = value
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
data/rjab.gemspec
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rjab}
|
8
|
+
s.version = "0.2.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Matt Puchlerz"]
|
12
|
+
s.date = %q{2009-11-02}
|
13
|
+
s.description = %q{A Ruby library for interacting with Jabbify. Simplifies the process of delivering messages to Jabbify's Comet server.}
|
14
|
+
s.email = %q{matt+rjab@puchlerz.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.markdown"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"README.markdown",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION",
|
23
|
+
"lib/jabbify.rb",
|
24
|
+
"lib/jabbify/comet.rb",
|
25
|
+
"rjab.gemspec",
|
26
|
+
"spec/jabbify/comet_spec.rb",
|
27
|
+
"spec/spec.opts",
|
28
|
+
"spec/spec_helper.rb"
|
29
|
+
]
|
30
|
+
s.homepage = %q{http://github.com/mattpuchlerz/rjab}
|
31
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
s.rubygems_version = %q{1.3.5}
|
34
|
+
s.summary = %q{A Ruby library for interacting with Jabbify.}
|
35
|
+
s.test_files = [
|
36
|
+
"spec/jabbify/comet_spec.rb",
|
37
|
+
"spec/spec_helper.rb"
|
38
|
+
]
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
42
|
+
s.specification_version = 3
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
45
|
+
s.add_runtime_dependency(%q<rest-client>, [">= 0.9.2"])
|
46
|
+
else
|
47
|
+
s.add_dependency(%q<rest-client>, [">= 0.9.2"])
|
48
|
+
end
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<rest-client>, [">= 0.9.2"])
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
@@ -0,0 +1,186 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), *%w[ .. spec_helper ] )
|
2
|
+
|
3
|
+
def common_attributes(attribute_overrides = {})
|
4
|
+
{
|
5
|
+
:action => :i_am_the_action,
|
6
|
+
:api_key => 'qwer1234qwer1234',
|
7
|
+
:message => 'This is the message!',
|
8
|
+
:name => 'John Doe',
|
9
|
+
:to => 'Jane Doe',
|
10
|
+
:type => :i_am_the_type,
|
11
|
+
}.merge(attribute_overrides)
|
12
|
+
end
|
13
|
+
|
14
|
+
describe Jabbify::Comet do
|
15
|
+
|
16
|
+
context "reading and writing attributes" do
|
17
|
+
|
18
|
+
before(:each) do
|
19
|
+
@comet = Jabbify::Comet.new
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should be able to get a hash of all default attributes" do
|
23
|
+
Jabbify::Comet::DEFAULT_ATTRIBUTES.should ==
|
24
|
+
{
|
25
|
+
:action => :create,
|
26
|
+
:api_key => nil,
|
27
|
+
:message => nil,
|
28
|
+
:name => 'Server',
|
29
|
+
:to => nil,
|
30
|
+
:type => :message,
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should be able to get a hash of all the current attributes (customs merged into defaults)" do
|
35
|
+
@comet = Jabbify::Comet.new common_attributes
|
36
|
+
@comet.attributes.should == common_attributes
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should be able to read/write an 'api_key' attribute" do
|
40
|
+
@comet.api_key = 'qwer1234qwer1234'
|
41
|
+
@comet.api_key.should == 'qwer1234qwer1234'
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should be able to read/write a 'type' attribute as a symbol" do
|
45
|
+
@comet.type = 'i_am_the_type'
|
46
|
+
@comet.type.should == :i_am_the_type
|
47
|
+
@comet.type = :i_am_the_type
|
48
|
+
@comet.type.should == :i_am_the_type
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should be able to read/write an 'action' attribute as a symbol" do
|
52
|
+
@comet.action = 'i_am_the_action'
|
53
|
+
@comet.action.should == :i_am_the_action
|
54
|
+
@comet.action = :i_am_the_action
|
55
|
+
@comet.action.should == :i_am_the_action
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should be able to read/write a 'name' attribute" do
|
59
|
+
@comet.name = 'John Doe'
|
60
|
+
@comet.name.should == 'John Doe'
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should be able to read/write a 'message' attribute" do
|
64
|
+
@comet.message = 'This is the message!'
|
65
|
+
@comet.message.should == 'This is the message!'
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should be able to read/write a 'to' attribute" do
|
69
|
+
@comet.to = 'Jane Doe'
|
70
|
+
@comet.to.should == 'Jane Doe'
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
context "determining the validity of the attributes" do
|
76
|
+
|
77
|
+
it "should be able to check the validity of the attributes" do
|
78
|
+
@comet = Jabbify::Comet.new
|
79
|
+
@comet.should respond_to(:valid?)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should not be valid if the 'api_key' attribute is blank" do
|
83
|
+
@comet = Jabbify::Comet.new common_attributes(:api_key => nil)
|
84
|
+
@comet.should_not be_valid
|
85
|
+
@comet = Jabbify::Comet.new common_attributes(:api_key => ' ')
|
86
|
+
@comet.should_not be_valid
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should not be valid if the 'name' attribute is blank" do
|
90
|
+
@comet = Jabbify::Comet.new common_attributes(:name => nil)
|
91
|
+
@comet.should_not be_valid
|
92
|
+
@comet = Jabbify::Comet.new common_attributes(:name => ' ')
|
93
|
+
@comet.should_not be_valid
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should not be valid if the 'message' attribute is blank" do
|
97
|
+
@comet = Jabbify::Comet.new common_attributes(:message => nil)
|
98
|
+
@comet.should_not be_valid
|
99
|
+
@comet = Jabbify::Comet.new common_attributes(:message => ' ')
|
100
|
+
@comet.should_not be_valid
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should be valid if all attributes are provided" do
|
104
|
+
@comet = Jabbify::Comet.new common_attributes
|
105
|
+
@comet.should be_valid
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
context "constructing the Jabbify URI" do
|
111
|
+
|
112
|
+
it "should be able to get the Jabbify URI" do
|
113
|
+
@comet = Jabbify::Comet.new
|
114
|
+
URI.parse(@comet.jabbify_uri).should_not raise_error(URI::InvalidURIError)
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should be able to get a hash of all the needed URI parameters" do
|
118
|
+
@comet = Jabbify::Comet.new common_attributes
|
119
|
+
@comet.uri_params.should ==
|
120
|
+
{
|
121
|
+
:action => :i_am_the_action,
|
122
|
+
:key => 'qwer1234qwer1234',
|
123
|
+
:message => 'This is the message!',
|
124
|
+
:name => 'John Doe',
|
125
|
+
:to => 'Jane Doe',
|
126
|
+
:type => :i_am_the_type,
|
127
|
+
}
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should not include any URI parameters that are blank" do
|
131
|
+
@comet = Jabbify::Comet.new common_attributes(:to => nil)
|
132
|
+
@comet.uri_params.should ==
|
133
|
+
{
|
134
|
+
:action => :i_am_the_action,
|
135
|
+
:key => 'qwer1234qwer1234',
|
136
|
+
:message => 'This is the message!',
|
137
|
+
:name => 'John Doe',
|
138
|
+
:type => :i_am_the_type,
|
139
|
+
}
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
context "delivering messages" do
|
145
|
+
|
146
|
+
it "should be able to deliver messages" do
|
147
|
+
@comet = Jabbify::Comet.new
|
148
|
+
@comet.should respond_to(:deliver)
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should be able to override any attribute via a hash passed in during delivery" do
|
152
|
+
RestClient.should_receive(:post).and_return('body of response')
|
153
|
+
overridden_attributes = { :message => 'Different message', :name => 'Mr. Doe' }
|
154
|
+
|
155
|
+
@comet = Jabbify::Comet.new common_attributes
|
156
|
+
@comet.deliver(overridden_attributes).should == true
|
157
|
+
@comet.attributes.should == common_attributes
|
158
|
+
end
|
159
|
+
|
160
|
+
it "should not deliver if any attributes are invalid" do
|
161
|
+
@comet = Jabbify::Comet.new common_attributes(:api_key => nil)
|
162
|
+
@comet.deliver.should == false
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should not deliver if the request fails" do
|
166
|
+
RestClient.should_receive(:post).and_raise(RuntimeError)
|
167
|
+
@comet = Jabbify::Comet.new common_attributes
|
168
|
+
@comet.deliver.should == false
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should deliver if the request succeeds" do
|
172
|
+
RestClient.should_receive(:post).and_return('body of response')
|
173
|
+
@comet = Jabbify::Comet.new common_attributes
|
174
|
+
@comet.deliver.should == true
|
175
|
+
end
|
176
|
+
|
177
|
+
it "should be able to handle one-off deliveries via a class method" do
|
178
|
+
comet = mock('Jabbify::Comet')
|
179
|
+
comet.should_receive(:deliver).and_return(true)
|
180
|
+
Jabbify::Comet.should_receive(:new).with(common_attributes).and_return(comet)
|
181
|
+
Jabbify::Comet.deliver(common_attributes).should == true
|
182
|
+
end
|
183
|
+
|
184
|
+
end
|
185
|
+
|
186
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rjab
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matt Puchlerz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-02 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rest-client
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.9.2
|
24
|
+
version:
|
25
|
+
description: A Ruby library for interacting with Jabbify. Simplifies the process of delivering messages to Jabbify's Comet server.
|
26
|
+
email: matt+rjab@puchlerz.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.markdown
|
33
|
+
files:
|
34
|
+
- .gitignore
|
35
|
+
- README.markdown
|
36
|
+
- Rakefile
|
37
|
+
- VERSION
|
38
|
+
- lib/jabbify.rb
|
39
|
+
- lib/jabbify/comet.rb
|
40
|
+
- rjab.gemspec
|
41
|
+
- spec/jabbify/comet_spec.rb
|
42
|
+
- spec/spec.opts
|
43
|
+
- spec/spec_helper.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/mattpuchlerz/rjab
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --charset=UTF-8
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.5
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: A Ruby library for interacting with Jabbify.
|
72
|
+
test_files:
|
73
|
+
- spec/jabbify/comet_spec.rb
|
74
|
+
- spec/spec_helper.rb
|