urbanairship 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -35,7 +35,7 @@ Sending a push notification
35
35
  ---------------------------
36
36
  ```ruby
37
37
  notification = {
38
- :schedule_for => 1.hour.from_now,
38
+ :schedule_for => [1.hour.from_now],
39
39
  :device_tokens => ['DEVICE-TOKEN-ONE', 'DEVICE-TOKEN-TWO'],
40
40
  :aps => {:alert => 'You have a new message!', :badge => 1}
41
41
  }
@@ -48,12 +48,12 @@ Batching push notification sends
48
48
  ```ruby
49
49
  notifications = [
50
50
  {
51
- :schedule_for => 1.hour.from_now,
51
+ :schedule_for => [{ :alias => 'deadbeef', :scheduled_time => 1.hour.from_now }], # assigning an alias to a scheduled push
52
52
  :device_tokens => ['DEVICE-TOKEN-ONE', 'DEVICE-TOKEN-TWO'],
53
53
  :aps => {:alert => 'You have a new message!', :badge => 1}
54
54
  },
55
55
  {
56
- :schedule_for => 3.hours.from_now,
56
+ :schedule_for => [3.hours.from_now],
57
57
  :device_tokens => ['DEVICE-TOKEN-THREE'],
58
58
  :aps => {:alert => 'You have a new message!', :badge => 1}
59
59
  }
@@ -62,13 +62,14 @@ notifications = [
62
62
  Urbanairship.batch_push notifications # => true
63
63
  ```
64
64
 
65
- Sending broadcoast notifications
66
- --------------------------------
65
+
66
+ Sending broadcast notifications
67
+ -------------------------------
67
68
  Urbanairship allows you to send a broadcast notification to all active registered device tokens for your app.
68
69
 
69
70
  ```ruby
70
71
  notification = {
71
- :schedule_for => 1.hour.from_now,
72
+ :schedule_for => [1.hour.from_now],
72
73
  :aps => {:alert => 'Important announcement!', :badge => 1}
73
74
  }
74
75
 
@@ -95,3 +96,14 @@ Urbanairship.feedback 24.hours.ago # =>
95
96
  # }
96
97
  # ]
97
98
  ```
99
+
100
+ Deleting scheduled notifications
101
+ --------------------------------
102
+
103
+ If you know the alias or id of a scheduled push notification then you can delete it from Urbanairship's queue and it will not be delivered.
104
+
105
+ ```ruby
106
+ Urbanairship.delete_scheduled_push("123456789") # => true
107
+ Urbanairship.delete_scheduled_push(123456789) # => true
108
+ Urbanairship.delete_scheduled_push(:alias => "deadbeef") # => true
109
+ ```
data/lib/urbanairship.rb CHANGED
@@ -12,12 +12,17 @@ module Urbanairship
12
12
  end
13
13
 
14
14
  VALID_PUSH_PARAMS = %w(device_tokens aliases tags schedule_for exclude_tokens aps)
15
+ VALID_REGISTER_PARAMS = %w(alias)
15
16
 
16
17
  class << self
17
18
  attr_accessor :application_key, :application_secret, :master_secret, :logger, :request_timeout
18
19
 
19
- def register_device(device_token)
20
- response = do_request(:put, "/api/device_tokens/#{device_token}", :authenticate_with => :application_secret)
20
+ def register_device(device_token, options = {})
21
+ response = do_request(:put, "/api/device_tokens/#{device_token}", :authenticate_with => :application_secret) do |request|
22
+ request.body = parse_register_options(options).to_json
23
+ request.add_field "Content-Type", "application/json" unless options.empty?
24
+ end
25
+
21
26
  response && %w(200 201).include?(response.code)
22
27
  end
23
28
 
@@ -26,6 +31,12 @@ module Urbanairship
26
31
  response && response.code == "204"
27
32
  end
28
33
 
34
+ def delete_scheduled_push(param)
35
+ path = (param.is_a? Hash) ? "/api/push/scheduled/alias/#{param[:alias].to_s}" : "/api/push/scheduled/#{param.to_s}"
36
+ response = do_request(:delete, path, :authenticate_with => :master_secret)
37
+ response && response.code == "204"
38
+ end
39
+
29
40
  def push(options = {})
30
41
  response = do_request(:post, "/api/push/", :authenticate_with => :master_secret) do |request|
31
42
  request.body = parse_push_options(options).to_json
@@ -90,8 +101,13 @@ module Urbanairship
90
101
  Net::HTTP.new("go.urbanairship.com", 443).tap{|http| http.use_ssl = true}
91
102
  end
92
103
 
104
+ def parse_register_options(hash = {})
105
+ hash[:alias] = hash[:alias].to_s unless hash[:alias].nil?
106
+ hash.delete_if{|key, value| !VALID_REGISTER_PARAMS.include?(key.to_s)}
107
+ end
108
+
93
109
  def parse_push_options(hash = {})
94
- hash[:schedule_for] = hash[:schedule_for].map{|time| format_time(time)} unless hash[:schedule_for].nil?
110
+ hash[:schedule_for] = hash[:schedule_for].map{|elem| process_scheduled_elem(elem)} unless hash[:schedule_for].nil?
95
111
  hash.delete_if{|key, value| !VALID_PUSH_PARAMS.include?(key.to_s)}
96
112
  end
97
113
 
@@ -109,6 +125,14 @@ module Urbanairship
109
125
  time.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
110
126
  end
111
127
 
128
+ def process_scheduled_elem(elem)
129
+ if elem.class == Hash
130
+ elem.merge!(:scheduled_time => format_time(elem[:scheduled_time]))
131
+ else
132
+ format_time(elem)
133
+ end
134
+ end
135
+
112
136
  def request_timeout
113
137
  @request_timeout || 5.0
114
138
  end
@@ -6,6 +6,7 @@ describe Urbanairship do
6
6
  # register_device
7
7
  FakeWeb.register_uri(:put, "https://my_app_key:my_app_secret@go.urbanairship.com/api/device_tokens/new_device_token", :status => ["201", "Created"])
8
8
  FakeWeb.register_uri(:put, "https://my_app_key:my_app_secret@go.urbanairship.com/api/device_tokens/existing_device_token", :status => ["200", "OK"])
9
+ FakeWeb.register_uri(:put, "https://my_app_key:my_app_secret@go.urbanairship.com/api/device_tokens/device_token_one", :status => ["201", "Created"])
9
10
  FakeWeb.register_uri(:put, /bad_key\:my_app_secret\@go\.urbanairship\.com/, :status => ["401", "Unauthorized"])
10
11
 
11
12
  # unregister_device
@@ -25,6 +26,11 @@ describe Urbanairship do
25
26
  FakeWeb.register_uri(:post, "https://my_app_key:my_master_secret@go.urbanairship.com/api/push/broadcast/", :status => ["200", "OK"])
26
27
  FakeWeb.register_uri(:post, "https://my_app_key2:my_master_secret2@go.urbanairship.com/api/push/broadcast/", :status => ["400", "Bad Request"])
27
28
 
29
+ # delete_scheduled_push
30
+ FakeWeb.register_uri(:delete, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/push\/scheduled\/[0-9]+/, :status => ["204", "No Content"])
31
+ FakeWeb.register_uri(:delete, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/push\/scheduled\/alias\/.+/, :status => ["204", "No Content"])
32
+ FakeWeb.register_uri(:delete, /bad_key\:my_master_secret\@go\.urbanairship.com\/api\/push\/scheduled\/[0-9]+/, :status => ["401", "Unauthorized"])
33
+
28
34
  # feedback
29
35
  FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/device_tokens\/feedback/, :status => ["200", "OK"], :body => "[{\"device_token\":\"token\",\"marked_inactive_on\":\"2010-10-14T19:15:13Z\",\"alias\":\"my_alias\"}]")
30
36
  FakeWeb.register_uri(:get, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/device_tokens\/feedback/, :status => ["500", "Internal Server Error"])
@@ -65,6 +71,7 @@ describe Urbanairship do
65
71
  describe "registering a device" do
66
72
 
67
73
  before(:each) do
74
+ @valid_params = {:alias => 'one'}
68
75
  Urbanairship.application_key = "my_app_key"
69
76
  Urbanairship.application_secret = "my_app_secret"
70
77
  end
@@ -101,8 +108,35 @@ describe Urbanairship do
101
108
  Urbanairship.register_device("new_device_token").should == false
102
109
  end
103
110
 
104
- # TODO:
105
- # it "accepts additional parameters (EXPAND THIS)"
111
+ it "doesn't set the content-type header to application/json if options are empty" do
112
+ Urbanairship.register_device("device_token_one")
113
+ FakeWeb.last_request['content-type'].should_not == 'application/json'
114
+ end
115
+
116
+ it "accepts an alias" do
117
+ Urbanairship.register_device("device_token_one", @valid_params).should == true
118
+ end
119
+
120
+ it "sets the content-type header to application/json when options are added" do
121
+ Urbanairship.register_device("device_token_one", @valid_params)
122
+ FakeWeb.last_request['content-type'].should == 'application/json'
123
+ end
124
+
125
+ it "adds alias to the JSON payload" do
126
+ Urbanairship.register_device("device_token_one", @valid_params)
127
+ request_json['alias'].should == "one"
128
+ end
129
+
130
+ it "converts alias param to string" do
131
+ Urbanairship.register_device("device_token_one", :alias => 11)
132
+ request_json['alias'].should be_a_kind_of String
133
+ end
134
+
135
+ it "excludes invalid parameters from the JSON payload" do
136
+ @valid_params.merge!(:foo => 'bar')
137
+ Urbanairship.register_device("device_token_one", @valid_params)
138
+ request_json['foo'].should be_nil
139
+ end
106
140
 
107
141
  end
108
142
 
@@ -143,6 +177,53 @@ describe Urbanairship do
143
177
 
144
178
  end
145
179
 
180
+ describe "deleting a scheduled push notification" do
181
+ before(:each) do
182
+ Urbanairship.application_key = "my_app_key"
183
+ Urbanairship.master_secret = "my_master_secret"
184
+ end
185
+
186
+ it "raises an error if call is made without an app key and master secret configured" do
187
+ Urbanairship.application_key = nil
188
+ Urbanairship.master_secret = nil
189
+
190
+ lambda {
191
+ Urbanairship.delete_scheduled_push("123456789")
192
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
193
+ end
194
+
195
+ it "uses app key and secret to sign the request" do
196
+ Urbanairship.delete_scheduled_push("123456789")
197
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
198
+ end
199
+
200
+ it "sends the key that needs to be deleted" do
201
+ Urbanairship.delete_scheduled_push("123456789")
202
+ FakeWeb.last_request.path.should == "/api/push/scheduled/123456789"
203
+ end
204
+
205
+ it "sends the key that needs to be deleted" do
206
+ Urbanairship.delete_scheduled_push(123456789)
207
+ FakeWeb.last_request.path.should == "/api/push/scheduled/123456789"
208
+ end
209
+
210
+ it "sends the alias that needs to be deleted" do
211
+ Urbanairship.delete_scheduled_push(:alias => "alias_to_delete")
212
+ FakeWeb.last_request.path.should == "/api/push/scheduled/alias/alias_to_delete"
213
+ end
214
+
215
+ it "returns true when the push notification is successfully deleted" do
216
+ Urbanairship.delete_scheduled_push("123456789").should == true
217
+ FakeWeb.last_request.body.should be_nil
218
+ end
219
+
220
+ it "returns false when the authorization is invalid" do
221
+ Urbanairship.application_key = "bad_key"
222
+ Urbanairship.delete_scheduled_push("123456789").should == false
223
+ end
224
+
225
+ end
226
+
146
227
  describe "sending multiple push notifications" do
147
228
 
148
229
  before(:each) do
@@ -205,6 +286,13 @@ describe Urbanairship do
205
286
  request_json['schedule_for'].should == ['2010-10-10T09:09:09Z']
206
287
  end
207
288
 
289
+ it "adds an aliased schedule_for to the JSON payload" do
290
+ time = Time.parse("Oct 17th, 2010, 8:00 PM UTC")
291
+ alias_str = 'cafebabe'
292
+ Urbanairship.push(@valid_params.merge(:schedule_for => [{ :alias => alias_str, :scheduled_time => time }]))
293
+ request_json['schedule_for'].should == [{ 'alias' => alias_str, 'scheduled_time' => '2010-10-17T20:00:00Z' }]
294
+ end
295
+
208
296
  it "adds exclude_tokens to the JSON payload" do
209
297
  Urbanairship.push(@valid_params.merge(:exclude_tokens => ["one", "two"]))
210
298
  request_json['exclude_tokens'].should == ["one", "two"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: urbanairship
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 2
10
- version: 1.0.2
9
+ - 3
10
+ version: 1.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Groupon, Inc.
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-14 00:00:00 -05:00
18
+ date: 2011-11-14 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency