mattpuchlerz-rjab 0.1.1 → 0.2.0

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/README.markdown CHANGED
@@ -16,35 +16,77 @@ Utilizing the Library
16
16
  require 'rubygems'
17
17
  require 'jabbify'
18
18
 
19
- For one-off message deliveries, you might find it easiest to use the class method:
19
+ If you only make one message delivery in your app, you might find it easiest to use the class method:
20
20
 
21
21
  Jabbify::Comet.deliver(
22
22
  :api_key => 'YourApiKeyGoesHere',
23
- :type => :message,
24
- :action => :create,
23
+ :type => :type_of_delivery,
24
+ :action => :action_of_delivery,
25
25
  :name => 'John Doe',
26
26
  :message => 'This is the message!',
27
27
  :to => 'Jane Doe',
28
28
  )
29
29
 
30
- Or you could always instantiate an instance, and deliver when you're ready:
30
+ More commonly, you will instantiate an instance of `Jabbify::Comet` with a few customized attributes. 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
31
 
32
- defaults = {
32
+ custom_attributes = {
33
33
  :api_key => 'YourApiKeyGoesHere',
34
- :type => :message,
35
- :action => :create,
36
34
  :name => 'The Server'
37
35
  }
38
36
 
39
- @comet = Jabbify::Comet.new defaults
40
- @comet.message = "The time is now #{ Time.now }"
37
+ @comet = Jabbify::Comet.new(custom_attributes)
41
38
 
42
- if @comet.deliver
39
+ if @comet.deliver(:message => "A special message at #{ Time.now.to_s }")
43
40
  # do something
44
41
  else
45
42
  # do something else
46
43
  end
47
44
 
45
+ Default Attributes on the `Jabbify::Comet` Class
46
+ ------------------------------------------------
47
+
48
+ <table>
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>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>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>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
+
48
90
 
49
91
 
50
92
  [jabbify]: http://jabbify.com
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
data/lib/jabbify/comet.rb CHANGED
@@ -4,54 +4,78 @@ require 'restclient' # http://github.com/adamwiggins/rest-client
4
4
  module Jabbify
5
5
  class Comet
6
6
 
7
- attr_accessor :action, :api_key, :message, :name, :to, :type
8
-
9
- def initialize(options = {})
10
- options.each_pair { |key, val| send("#{ key }=", val) }
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
+ @overridden_attributes = {}
11
19
  end
12
20
 
13
- def action=(action)
14
- @action = action.to_sym
21
+ def attributes
22
+ @customized_attributes
15
23
  end
16
24
 
17
- def deliver
18
- return false unless valid?
19
- begin
20
- RestClient.post jabbify_uri, uri_params
21
- true
22
- rescue
23
- false
25
+ def deliver(overridden_attributes = {})
26
+ customized_attributes = @customized_attributes.dup
27
+ @customized_attributes.merge! overridden_attributes
28
+ return_value = false
29
+
30
+ if valid?
31
+ begin
32
+ RestClient.post jabbify_uri, uri_params
33
+ return_value = true
34
+ rescue
35
+ end
24
36
  end
37
+
38
+ @customized_attributes = customized_attributes
39
+ return_value
25
40
  end
26
41
 
27
- def jabbify_uri
28
- 'https://jabbify.com:8443/message_push'
42
+ def self.deliver(options)
43
+ new(options).deliver
29
44
  end
30
45
 
31
- def type=(type)
32
- @type = type.to_sym
46
+ def jabbify_uri
47
+ 'https://jabbify.com:8443/message_push'
33
48
  end
34
49
 
35
50
  def uri_params
36
51
  {
37
- :action => action,
38
- :key => api_key,
39
- :message => message,
40
- :name => name,
41
- :to => to,
42
- :type => type
43
- }.reject { |key, val| val.nil? }
52
+ :action => @customized_attributes[:action],
53
+ :key => @customized_attributes[:api_key],
54
+ :message => @customized_attributes[:message],
55
+ :name => @customized_attributes[:name],
56
+ :to => @customized_attributes[:to],
57
+ :type => @customized_attributes[:type],
58
+ }.reject { |key, value| value.nil? }
44
59
  end
45
60
 
46
61
  def valid?
47
- %w[ api_key name message ].each do |attribute|
48
- return false if send(attribute).nil?
62
+ [ :api_key, :name, :message ].each do |attribute|
63
+ return false if @customized_attributes[attribute].nil? or @customized_attributes[attribute].strip.length == 0
49
64
  end
50
65
  true
51
66
  end
52
67
 
53
- def self.deliver(options)
54
- new(options).deliver
68
+ DEFAULT_ATTRIBUTES.keys.each do |attribute|
69
+
70
+ define_method attribute do
71
+ attributes[attribute]
72
+ end
73
+
74
+ define_method "#{attribute}=" do |value|
75
+ value = value.to_sym if [ :action, :type ].include? attribute
76
+ @customized_attributes[attribute] = value
77
+ end
78
+
55
79
  end
56
80
 
57
81
  end
data/rjab.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{rjab}
5
- s.version = "0.1.1"
5
+ s.version = "0.2.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Matt Puchlerz"]
9
- s.date = %q{2009-05-22}
9
+ s.date = %q{2009-05-23}
10
10
  s.description = %q{A Ruby library for interacting with Jabbify. Simplifies the process of delivering messages to Jabbify's Comet server.}
11
11
  s.email = %q{matt@puchlerz.com}
12
12
  s.extra_rdoc_files = [
@@ -1,41 +1,58 @@
1
1
  require File.join( File.dirname(__FILE__), *%w[ .. spec_helper ] )
2
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
+
3
14
  describe Jabbify::Comet do
4
15
 
5
- def defaults(options = {})
6
- {
7
- :action => :i_am_the_action,
8
- :api_key => 'qwer1234qwer1234',
9
- :message => 'This is the message!',
10
- :name => 'John Doe',
11
- :to => 'Jane Doe',
12
- :type => :i_am_the_type,
13
- }.merge(options)
14
- end
15
-
16
16
  context "reading and writing attributes" do
17
17
 
18
18
  before(:each) do
19
19
  @comet = Jabbify::Comet.new
20
20
  end
21
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
+
22
39
  it "should be able to read/write an 'api_key' attribute" do
23
40
  @comet.api_key = 'qwer1234qwer1234'
24
41
  @comet.api_key.should == 'qwer1234qwer1234'
25
42
  end
26
43
 
27
44
  it "should be able to read/write a 'type' attribute as a symbol" do
28
- @comet.type = :i_am_the_type
29
- @comet.type.should == :i_am_the_type
30
45
  @comet.type = 'i_am_the_type'
31
46
  @comet.type.should == :i_am_the_type
47
+ @comet.type = :i_am_the_type
48
+ @comet.type.should == :i_am_the_type
32
49
  end
33
50
 
34
51
  it "should be able to read/write an 'action' attribute as a symbol" do
35
- @comet.action = :i_am_the_action
36
- @comet.action.should == :i_am_the_action
37
52
  @comet.action = 'i_am_the_action'
38
53
  @comet.action.should == :i_am_the_action
54
+ @comet.action = :i_am_the_action
55
+ @comet.action.should == :i_am_the_action
39
56
  end
40
57
 
41
58
  it "should be able to read/write a 'name' attribute" do
@@ -53,16 +70,6 @@ describe Jabbify::Comet do
53
70
  @comet.to.should == 'Jane Doe'
54
71
  end
55
72
 
56
- it "should be able to write any attribute via a hash passed in during initialization" do
57
- @comet = Jabbify::Comet.new defaults
58
- @comet.api_key.should == 'qwer1234qwer1234'
59
- @comet.type.should == :i_am_the_type
60
- @comet.action.should == :i_am_the_action
61
- @comet.name.should == 'John Doe'
62
- @comet.message.should == 'This is the message!'
63
- @comet.to.should == 'Jane Doe'
64
- end
65
-
66
73
  end
67
74
 
68
75
  context "determining the validity of the attributes" do
@@ -73,22 +80,28 @@ describe Jabbify::Comet do
73
80
  end
74
81
 
75
82
  it "should not be valid if the 'api_key' attribute is blank" do
76
- @comet = Jabbify::Comet.new defaults(:api_key => nil)
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 => ' ')
77
86
  @comet.should_not be_valid
78
87
  end
79
88
 
80
89
  it "should not be valid if the 'name' attribute is blank" do
81
- @comet = Jabbify::Comet.new defaults(:name => nil)
90
+ @comet = Jabbify::Comet.new common_attributes(:name => nil)
91
+ @comet.should_not be_valid
92
+ @comet = Jabbify::Comet.new common_attributes(:name => ' ')
82
93
  @comet.should_not be_valid
83
94
  end
84
95
 
85
96
  it "should not be valid if the 'message' attribute is blank" do
86
- @comet = Jabbify::Comet.new defaults(:message => nil)
97
+ @comet = Jabbify::Comet.new common_attributes(:message => nil)
98
+ @comet.should_not be_valid
99
+ @comet = Jabbify::Comet.new common_attributes(:message => ' ')
87
100
  @comet.should_not be_valid
88
101
  end
89
102
 
90
103
  it "should be valid if all attributes are provided" do
91
- @comet = Jabbify::Comet.new defaults
104
+ @comet = Jabbify::Comet.new common_attributes
92
105
  @comet.should be_valid
93
106
  end
94
107
 
@@ -102,7 +115,7 @@ describe Jabbify::Comet do
102
115
  end
103
116
 
104
117
  it "should be able to get a hash of all the needed URI parameters" do
105
- @comet = Jabbify::Comet.new defaults
118
+ @comet = Jabbify::Comet.new common_attributes
106
119
  @comet.uri_params.should ==
107
120
  {
108
121
  :action => :i_am_the_action,
@@ -115,7 +128,7 @@ describe Jabbify::Comet do
115
128
  end
116
129
 
117
130
  it "should not include any URI parameters that are blank" do
118
- @comet = Jabbify::Comet.new defaults(:to => nil)
131
+ @comet = Jabbify::Comet.new common_attributes(:to => nil)
119
132
  @comet.uri_params.should ==
120
133
  {
121
134
  :action => :i_am_the_action,
@@ -135,28 +148,37 @@ describe Jabbify::Comet do
135
148
  @comet.should respond_to(:deliver)
136
149
  end
137
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
+
138
160
  it "should not deliver if any attributes are invalid" do
139
- @comet = Jabbify::Comet.new defaults(:api_key => nil)
161
+ @comet = Jabbify::Comet.new common_attributes(:api_key => nil)
140
162
  @comet.deliver.should == false
141
163
  end
142
164
 
143
165
  it "should not deliver if the request fails" do
144
166
  RestClient.should_receive(:post).and_raise(RuntimeError)
145
- @comet = Jabbify::Comet.new defaults
167
+ @comet = Jabbify::Comet.new common_attributes
146
168
  @comet.deliver.should == false
147
169
  end
148
170
 
149
171
  it "should deliver if the request succeeds" do
150
172
  RestClient.should_receive(:post).and_return('body of response')
151
- @comet = Jabbify::Comet.new defaults
173
+ @comet = Jabbify::Comet.new common_attributes
152
174
  @comet.deliver.should == true
153
175
  end
154
176
 
155
177
  it "should be able to handle one-off deliveries via a class method" do
156
178
  comet = mock('Jabbify::Comet')
157
179
  comet.should_receive(:deliver).and_return(true)
158
- Jabbify::Comet.should_receive(:new).with(defaults).and_return(comet)
159
- Jabbify::Comet.deliver(defaults).should == true
180
+ Jabbify::Comet.should_receive(:new).with(common_attributes).and_return(comet)
181
+ Jabbify::Comet.deliver(common_attributes).should == true
160
182
  end
161
183
 
162
184
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mattpuchlerz-rjab
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Puchlerz
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-22 00:00:00 -07:00
12
+ date: 2009-05-23 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency