appush_client 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'appush_client'
5
- s.version = '0.2'
5
+ s.version = '0.3'
6
6
 
7
7
  s.authors = ['Igor Guerrero']
8
8
  s.date = '2010-01-09'
@@ -13,5 +13,5 @@ Gem::Specification.new do |s|
13
13
  'spec/appush_client_spec.rb']
14
14
  s.summary = "Ruby client library for Appush PUSH service."
15
15
 
16
- s.add_dependency 'rest-client', '>= 1.0'
16
+ s.add_dependency 'rest-client', '>= 1.3.1'
17
17
  end
data/lib/appush_client.rb CHANGED
@@ -139,10 +139,6 @@ class Profile < AppushClient
139
139
  def register_device(device_token, tags=[])
140
140
  url = "#{@service_url}/device/#{device_token}"
141
141
 
142
- if tags.empty?
143
- return RestClient.put url, :content_type=>:json, :accept=>:json
144
- end
145
-
146
142
  data = {:tags=>tags}.to_json
147
143
 
148
144
  RestClient.put url, data, :content_type=>:json, :accept=>:json
@@ -3,91 +3,201 @@
3
3
  require 'appush_client'
4
4
  require 'webmock/rspec'
5
5
 
6
+ require 'json'
7
+
6
8
  include WebMock
7
9
 
8
10
  describe "Using Appush client API" do
9
11
  before(:each) do
10
- @app_key = "I6ieYiJtbH3YPriyR3AK3VZtgSP"
11
- @app_secret = "48907eb5efbffe9ee23e62e2b9273929"
12
- @push_secret = "72781d3c6d508699e8f3918de6dceb25"
13
- @service_url = "http://appush.com/api"
14
-
15
- @client = AppushClient.new @app_key,
16
- @app_secret,
17
- :push_secret=>@push_secret,
18
- :service_url=>@service_url
12
+ # I used test keys and tokens, but tested them to work
13
+ @root_key = "09a539a84e32ddbce7c6bcdcedba78f4"
14
+ @root_secret = "e77694945eff115f4b0cc6d0bcf800ec"
15
+ @app_key = "38KaiutatmjKWPsbksVco1YeQeE"
16
+ @app_secret = "5ZulDkmNY230rRyx3v6CaJfkAhP"
17
+ @profile_key = "GkYwZ5xTV8PXJJZksQDV3POKMCp"
18
+ @profile_secret = "TqRrqWfnW0Zp4jsxOEQQR6iCcLH"
19
+ @service_url = "https://appush.com/api"
20
+
21
+ # we initialize every object on every test
22
+ @root_user = RootUser.new @root_key, @root_secret, :service_url=>@service_url
23
+ @application = Application.new @app_key, @app_secret, :service_url=>@service_url
24
+ @profile = Profile.new @profile_key, @profile_secret, :service_url=>@service_url
19
25
  end
20
26
 
21
27
  it "should not be null" do
22
- @client.should_not be nil
23
- @client.to_s.should == "Appush Client = I6ieYiJtbH3YPriyR3AK3VZtgSP:48907eb5efbffe9ee23e62e2b9273929, Push Secret = 72781d3c6d508699e8f3918de6dceb25, Server = appush.com/api"
24
- @client.protocol.should == "http://"
28
+ @root_user.should_not be nil
29
+ @root_user.to_s.should == "Server = https://09a539a84e32ddbce7c6bcdcedba78f4:e77694945eff115f4b0cc6d0bcf800ec@appush.com/api"
30
+
31
+ @application.should_not be nil
32
+ @application.to_s.should == "Server = https://38KaiutatmjKWPsbksVco1YeQeE:5ZulDkmNY230rRyx3v6CaJfkAhP@appush.com/api"
33
+
34
+ @profile.should_not be nil
35
+ @profile.to_s.should == "Server = https://GkYwZ5xTV8PXJJZksQDV3POKMCp:TqRrqWfnW0Zp4jsxOEQQR6iCcLH@appush.com/api"
36
+ end
37
+
38
+ it "should create an application" do
39
+ result = "{\"id\":\"YZgzHWpxdt4emUAYTptovTcfYdW\"}"
40
+
41
+ stub_request(:post, "https://#{@root_key}:#{@root_secret}@appush.com/api/application").to_return(:body=>result)
42
+
43
+ @root_user.create_application("test").to_s.should == result
44
+
45
+ WebMock.should have_requested(:post, "https://#{@root_key}:#{@root_secret}@appush.com/api/application").once
46
+ end
47
+
48
+ it "should modify an application" do
49
+ result = ""
50
+ id = "test"
51
+
52
+ stub_request(:put, "https://#{@root_key}:#{@root_secret}@appush.com/api/application/#{id}").to_return(:body=>result)
53
+
54
+ @root_user.modify_application("test", :name=>"new name").to_s.should == result
55
+
56
+ WebMock.should have_requested(:put, "https://#{@root_key}:#{@root_secret}@appush.com/api/application/#{id}").once
25
57
  end
26
-
27
- it "should get device information" do
28
- result = {"tags"=>["foo", "waa"], "status"=> 200}
29
58
 
30
- stub_request(:get, "#{@client.protocol}#{@client.app_key}:#{@client.app_secret}@#{@client.service_url}/device/test_device").to_return(:body=>result.to_json)
59
+ it "should list all applications" do
60
+ result = "[\"app1\", \"app2\"]"
61
+
62
+ stub_request(:get, "https://#{@root_key}:#{@root_secret}@appush.com/api/application").to_return(:body=>result)
63
+
64
+ @root_user.list_applications.to_s.should == result
65
+
66
+ WebMock.should have_requested(:get, "https://#{@root_key}:#{@root_secret}@appush.com/api/application").once
67
+ end
68
+
69
+ it "should get an application" do
70
+ result = "{\"name\":\"My New Application\", \"application_token\": \"EVtmyITWMKzDRtawXxubVmfIYWU\", \"application_secret\": \"e52c6cbd232e2111671953d320ff80a2\", \"status\":\"dev\",\"dev_pem\":\"Bag Attributes\", \"active_devices\":0}\""
71
+ id = "test"
72
+
73
+ stub_request(:get, "https://#{@root_key}:#{@root_secret}@appush.com/api/application/#{id}").to_return(:body=>result)
74
+
75
+ @root_user.get_application(id).to_s.should == result
76
+
77
+ WebMock.should have_requested(:get, "https://#{@root_key}:#{@root_secret}@appush.com/api/application/#{id}").once
78
+ end
79
+
80
+ it "should get an application icon" do
81
+ result = File.read("#{File.dirname(__FILE__)}/no-icon.png")
82
+ id = "test"
83
+
84
+ stub_request(:get, "https://#{@root_key}:#{@root_secret}@appush.com/api/application/#{id}/icon").to_return(:body=>result)
85
+
86
+ @root_user.get_application_icon(id).to_s.should == result
87
+
88
+ WebMock.should have_requested(:get, "https://#{@root_key}:#{@root_secret}@appush.com/api/application/#{id}/icon").once
89
+ end
90
+
91
+ it "should save an application icon" do
92
+ icon = File.read("#{File.dirname(__FILE__)}/no-icon.png")
93
+ id = "test"
94
+
95
+ stub_request(:put, "https://#{@root_key}:#{@root_secret}@appush.com/api/application/#{id}/icon").with(:body=>icon, :headers=>{"Content-Type"=>"image/png"}).to_return(:body=>"")
96
+
97
+ @root_user.save_application_icon(id, icon).to_s.should == ""
98
+
99
+ WebMock.should have_requested(:put, "https://#{@root_key}:#{@root_secret}@appush.com/api/application/#{id}/icon").once
100
+ end
101
+
102
+ it "should delete an application" do
103
+ id = "test"
104
+
105
+ stub_request(:delete, "https://#{@root_key}:#{@root_secret}@appush.com/api/application/#{id}").to_return(:body=>"")
106
+
107
+ @root_user.delete_application(id).to_s.should == ""
108
+
109
+ WebMock.should have_requested(:delete, "https://#{@root_key}:#{@root_secret}@appush.com/api/application/#{id}").once
110
+ end
111
+
112
+ it "should send a notification" do
113
+ id = "test"
114
+
115
+ params={:tags=>["one", "two", "three"],
116
+ :devices=>["lol", "waa"],
117
+ :exclude=>["do", "not", "exclude", "me"],
118
+ :alert=>"w00t!",
119
+ :sound=>"cat.ogg",
120
+ :badge=>1,
121
+ :kv=>{:go=>"crazy"}}
122
+
123
+ stub_request(:post, "https://#{@root_key}:#{@root_secret}@appush.com/api/application/#{id}/notification").with(:body=>"{\"devices\":[\"lol\",\"waa\"],\"payload\":{\"go\":\"crazy\",\"aps\":{\"badge\":1,\"sound\":\"cat.ogg\",\"alert\":\"w00t!\"}},\"exclude\":[\"do\",\"not\",\"exclude\",\"me\"],\"tags\":[\"one\",\"two\",\"three\"]}", :headers=>{"Content-Type"=>"application/json"}).to_return(:body=>"")
124
+
125
+ @root_user.send_notification(id, params).to_s.should == ""
126
+
127
+ WebMock.should have_requested(:post, "https://#{@root_key}:#{@root_secret}@appush.com/api/application/#{id}/notification").once
128
+ end
129
+
130
+ it "should get an notification status" do
131
+ app_id = "test_app"
132
+ notification_id = "test_notification"
133
+
134
+ stub_request(:get, "https://#{@root_key}:#{@root_secret}@appush.com/api/application/#{app_id}/notification/#{notification_id}").to_return(:body=>"")
135
+
136
+ @root_user.get_notification_status(app_id, notification_id).to_s.should == ""
137
+
138
+ WebMock.should have_requested(:get, "https://#{@root_key}:#{@root_secret}@appush.com/api/application/#{app_id}/notification/#{notification_id}").once
139
+ end
140
+
141
+ it "should get all devices by tag" do
142
+ app_id = "test"
143
+ tag = "waa"
144
+
145
+ stub_request(:get, "https://#{@root_key}:#{@root_secret}@appush.com/api/application/#{app_id}/tag/#{tag}").to_return(:body=>"")
31
146
 
32
- @client.get_device("test_device").should == result
147
+ @root_user.get_devices_by_tag(app_id, tag).to_s.should == ""
33
148
 
34
- WebMock.should have_requested(:get, "#{@client.protocol}#{@client.app_key}:#{@client.app_secret}@#{@client.service_url}/device/test_device").once
149
+ WebMock.should have_requested(:get, "https://#{@root_key}:#{@root_secret}@appush.com/api/application/#{app_id}/tag/#{tag}").once
35
150
  end
36
151
 
37
- it "should register devices" do
38
- result = {"status"=>204}
152
+ it "should get a device information" do
153
+ dev_token = "test_dev"
39
154
 
40
- stub_request(:put, "#{@client.protocol}#{@client.app_key}:#{@client.app_secret}@#{@client.service_url}/device/test_device2").to_return(:body=>result.to_json, :status=>204)
155
+ stub_request(:get, "https://#{@profile_key}:#{@profile_secret}@appush.com/api/device/#{dev_token}").to_return(:body=>"")
41
156
 
42
- @client.register_device("test_device2").should == result
157
+ @profile.get_device(dev_token).to_s.should == ""
43
158
 
44
- WebMock.should have_requested(:put, "#{@client.protocol}#{@client.app_key}:#{@client.app_secret}@#{@client.service_url}/device/test_device2").once
159
+ WebMock.should have_requested(:get, "https://#{@profile_key}:#{@profile_secret}@appush.com/api/device/#{dev_token}").once
45
160
  end
46
161
 
47
- it "should unregister devices" do
48
- result = {"status"=>204}
162
+ it "should register a device with tags" do
163
+ dev_token = "test_dev"
164
+ tags = ["foo", "bar"]
49
165
 
50
- stub_request(:delete, "#{@client.protocol}#{@client.app_key}:#{@client.app_secret}@#{@client.service_url}/device/test_device").to_return(:body=>result.to_json, :status=>204)
166
+ stub_request(:put, "https://#{@profile_key}:#{@profile_secret}@appush.com/api/device/#{dev_token}").with(:body=>{:tags=>tags}.to_json, :headers=>{"Content-Type"=>"application/json"}).to_return(:body=>"")
51
167
 
52
- @client.unregister_device("test_device").should == result
168
+ @profile.register_device(dev_token, tags).to_s.should == ""
53
169
 
54
- WebMock.should have_requested(:delete, "#{@client.protocol}#{@client.app_key}:#{@client.app_secret}@#{@client.service_url}/device/test_device").once
170
+ WebMock.should have_requested(:put, "https://#{@profile_key}:#{@profile_secret}@appush.com/api/device/#{dev_token}").once
55
171
  end
56
172
 
57
- it "should get devices using a tag" do
58
- result = {"devices"=>["test_device"],"status"=>200}
173
+ it "should register a device without tags" do
174
+ dev_token = "test_dev"
175
+ tags = []
59
176
 
60
- stub_request(:get, "#{@client.protocol}#{@client.app_key}:#{@client.push_secret}@#{@client.service_url}/tag/waa").to_return(:body=>result.to_json, :status=>200)
177
+ stub_request(:put, "https://#{@profile_key}:#{@profile_secret}@appush.com/api/device/#{dev_token}").with(:body=>{:tags=>tags}.to_json, :headers=>{"Content-Type"=>"application/json"}).to_return(:body=>"")
61
178
 
62
- @client.get_devices_by_tag("waa").should == result
179
+ @profile.register_device(dev_token, tags).to_s.should == ""
63
180
 
64
- WebMock.should have_requested(:get, "#{@client.protocol}#{@client.app_key}:#{@client.push_secret}@#{@client.service_url}/tag/waa").once
181
+ WebMock.should have_requested(:put, "https://#{@profile_key}:#{@profile_secret}@appush.com/api/device/#{dev_token}").once
65
182
  end
66
183
 
67
- it "should send notifications" do
68
- result = {"status"=>201}
69
- tags = ["foo", "waa"]
70
- devices = ["test_device"]
71
- exclude = ["invalid_device"]
72
- alert = "hello"
73
- sound = "meow.aiff"
74
- badge = 1
75
- kv = {"spam"=>"foo"}
184
+ it "should unregister a device" do
185
+ dev_token = "test_dev"
76
186
 
77
- stub_request(:post, "#{@client.protocol}#{@client.app_key}:#{@client.push_secret}@#{@client.service_url}/notification").to_return(:status=>201, :body=>result.to_json)
187
+ stub_request(:delete, "https://#{@profile_key}:#{@profile_secret}@appush.com/api/device/#{dev_token}").to_return(:body=>"")
78
188
 
79
- @client.send_notification(:tags=>tags, :devices=>devices, :exclude=>exclude, :alert=>alert, :sound=>sound, :badge=>badge, :kv=>kv).should == {"status"=>201}
189
+ @profile.unregister_device(dev_token).to_s.should == ""
80
190
 
81
- WebMock.should have_requested(:post, "#{@client.protocol}#{@client.app_key}:#{@client.push_secret}@#{@client.service_url}/notification").once
191
+ WebMock.should have_requested(:delete, "https://#{@profile_key}:#{@profile_secret}@appush.com/api/device/#{dev_token}").once
82
192
  end
83
193
 
84
- it "should get notifications statuses" do
85
- result = {"devices"=>["test_device"], "completed"=>0, "payload"=>{"aps"=>{"badge"=>1, "sound"=>"meow.aiff", "alert"=>"Hello World"}, "spam"=>"eggs"}, "delivered"=>[], "tags"=>["foo", "bar", "baz"], "status"=>200}
194
+ it "should create a profile" do
195
+ result = "[\"x\", \"y\"]"
86
196
 
87
- stub_request(:get, "#{@client.protocol}#{@client.app_key}:#{@client.push_secret}@#{@client.service_url}/notification/id_notify").to_return(:body=>result.to_json)
197
+ stub_request(:post, "https://#{@app_key}:#{@app_secret}@appush.com/api/profile").with(:headers=>{"Content-Type"=>"application/json"}).to_return(:body=>result)
88
198
 
89
- @client.get_notification_status("id_notify").should == result
199
+ @application.create_profile.to_s.should == result
90
200
 
91
- WebMock.should have_requested(:get, "#{@client.protocol}#{@client.app_key}:#{@client.push_secret}@#{@client.service_url}/notification/id_notify").once
201
+ WebMock.should have_requested(:post, "https://#{@app_key}:#{@app_secret}@appush.com/api/profile").once
92
202
  end
93
203
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: appush_client
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ version: "0.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Guerrero
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: "1.0"
23
+ version: 1.3.1
24
24
  version:
25
25
  description: Ruby client library for Appush PUSH service.
26
26
  email: