appush_client 0.3 → 0.4

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.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'appush_client'
5
- s.version = '0.3'
5
+ s.version = '0.4'
6
6
 
7
7
  s.authors = ['Igor Guerrero']
8
8
  s.date = '2010-01-09'
data/lib/appush_client.rb CHANGED
@@ -3,160 +3,162 @@
3
3
  require 'json'
4
4
  require 'rest_client'
5
5
 
6
- class AppushClient
7
- attr_accessor :user, :password, :protocol, :service_url
6
+ module AppushClient
7
+ class AppushClient
8
+ attr_accessor :user, :password, :protocol, :service_url
8
9
 
9
- def initialize(user, password, params={})
10
- params = {:service_url=>"https://appush.com/api"}.merge params
11
- @user = user
12
- @password = password
10
+ def initialize(user, password, params={})
11
+ params = {:service_url=>"https://appush.com/api"}.merge params
12
+ @user = user
13
+ @password = password
13
14
 
14
- # cleaning params[:service_url], removing tailing "/"
15
- params[:service_url] = clean_service_url(params[:service_url])
15
+ # cleaning params[:service_url], removing tailing "/"
16
+ params[:service_url] = clean_service_url(params[:service_url])
16
17
 
17
- # http://xkcd.com/208/
18
- @protocol = params[:service_url].match(/^https?:\/\//)[0] # match the protocol, http:// or https://
19
- @url = params[:service_url].sub(/^https?:\/\//, '') # removes the protocol form the service
18
+ # http://xkcd.com/208/
19
+ @protocol = params[:service_url].match(/^https?:\/\//)[0] # match the protocol, http:// or https://
20
+ @url = params[:service_url].sub(/^https?:\/\//, '') # removes the protocol form the service
20
21
 
21
- @service_url = "#{@protocol}#{@user}:#{@password}@#{@url}"
22
- end
22
+ @service_url = "#{@protocol}#{@user}:#{@password}@#{@url}"
23
+ end
23
24
 
24
- def to_s
25
- "Server = #{@service_url}"
26
- end
25
+ def to_s
26
+ "Server = #{@service_url}"
27
+ end
27
28
 
28
- private
29
+ private
29
30
 
30
- def clean_service_url(service_url)
31
- while !service_url.match(/\/$/).nil?
32
- service_url = service_url.sub(/\/$/, '')
33
- end
31
+ def clean_service_url(service_url)
32
+ while !service_url.match(/\/$/).nil?
33
+ service_url = service_url.sub(/\/$/, '')
34
+ end
34
35
 
35
- service_url
36
+ service_url
37
+ end
36
38
  end
37
- end
38
39
 
39
- class RootUser < AppushClient
40
- # POST
41
- def create_application(name, env="dev", dev_pem="", prod_pem="")
42
- url = "#{@service_url}/application"
43
- data = {:name=>name, :env=>env, :dev_pem=>dev_pem, :prod_pem=>prod_pem}.to_json
40
+ class RootUser < AppushClient
41
+ # POST
42
+ def create_application(name, env="dev", dev_pem="", prod_pem="")
43
+ url = "#{@service_url}/application"
44
+ data = {:name=>name, :env=>env, :dev_pem=>dev_pem, :prod_pem=>prod_pem}.to_json
44
45
 
45
- RestClient.post url, data, :content_type=>:json, :accept=>:json
46
- end
46
+ RestClient.post url, data, :content_type=>:json, :accept=>:json
47
+ end
47
48
 
48
- # PUT
49
- # parameters are optional: :name, :env, :dev_pem, :prod_pem
50
- def modify_application(id, params={})
51
- url = "#{@service_url}/application/#{id}"
52
- data = Hash.new
49
+ # PUT
50
+ # parameters are optional: :name, :env, :dev_pem, :prod_pem
51
+ def modify_application(id, params={})
52
+ url = "#{@service_url}/application/#{id}"
53
+ data = Hash.new
53
54
 
54
- data[:name] = params[:name] if params[:name]
55
- data[:env] = params[:env] if params[:env]
56
- data[:dev_pem] = params[:dev_pem] if params[:dev_pem]
57
- data[:prod_pem] = params[:prod_pem] if params[:prod_pem]
55
+ data[:name] = params[:name] if params[:name]
56
+ data[:env] = params[:env] if params[:env]
57
+ data[:dev_pem] = params[:dev_pem] if params[:dev_pem]
58
+ data[:prod_pem] = params[:prod_pem] if params[:prod_pem]
58
59
 
59
- data = data.to_json
60
- RestClient.put url, data, :content_type=>:json, :accept=>:json
61
- end
60
+ data = data.to_json
61
+ RestClient.put url, data, :content_type=>:json, :accept=>:json
62
+ end
62
63
 
63
- # GET
64
- def list_applications()
65
- url = "#{@service_url}/application"
64
+ # GET
65
+ def list_applications()
66
+ url = "#{@service_url}/application"
66
67
 
67
- RestClient.get url, :content_type=>:json, :accept=>:json
68
- end
68
+ RestClient.get url, :content_type=>:json, :accept=>:json
69
+ end
69
70
 
70
- # GET <id>
71
- def get_application(id)
72
- url = "#{@service_url}/application/#{id}"
71
+ # GET <id>
72
+ def get_application(id)
73
+ url = "#{@service_url}/application/#{id}"
73
74
 
74
- RestClient.get url, :content_type=>:json, :accept=>:json
75
- end
75
+ RestClient.get url, :content_type=>:json, :accept=>:json
76
+ end
76
77
 
77
- # GET application/<id>/icon
78
- def get_application_icon(id)
79
- url = "#{@service_url}/application/#{id}/icon"
78
+ # GET application/<id>/icon
79
+ def get_application_icon(id)
80
+ url = "#{@service_url}/application/#{id}/icon"
80
81
 
81
- RestClient.get url, :content_type=>"image/png", :accept=>"image/png"
82
- end
82
+ RestClient.get url, :content_type=>"image/png", :accept=>"image/png"
83
+ end
83
84
 
84
- # PUT application/<id>/icon
85
- def save_application_icon(app_id, icon)
86
- url = "#{@service_url}/application/#{app_id}/icon"
85
+ # PUT application/<id>/icon
86
+ def save_application_icon(app_id, icon)
87
+ url = "#{@service_url}/application/#{app_id}/icon"
87
88
 
88
- RestClient.put url, icon, :content_type=>"image/png"
89
- end
89
+ RestClient.put url, icon, :content_type=>"image/png"
90
+ end
90
91
 
91
- # DELETE <id>
92
- def delete_application(id)
93
- url = "#{@service_url}/application/#{id}"
92
+ # DELETE <id>
93
+ def delete_application(id)
94
+ url = "#{@service_url}/application/#{id}"
94
95
 
95
- RestClient.delete url, :content_type=>:json, :accept=>:json
96
- end
96
+ RestClient.delete url, :content_type=>:json, :accept=>:json
97
+ end
97
98
 
98
- # POST <id> send notification
99
- def send_notification(app_id, params={})
100
- url = "#{@service_url}/application/#{app_id}/notification"
101
- params = {:tags=>[], :devices=>[], :exclude=>[], :alert=>"", :sound=>"", :badge=>0, :kv=>[]}.merge params
102
-
103
- payload = {"aps"=>{"alert"=>params[:alert],
104
- "sound"=>params[:sound],
105
- "badge"=>params[:badge]}}.merge params[:kv]
106
- payload = {"payload"=>payload}
107
- data = {"tags"=>params[:tags],
108
- "devices"=>params[:devices],
109
- "exclude"=>params[:exclude]}.merge payload
110
- data = data.to_json
111
-
112
- RestClient.post url, data, :content_type=>:json, :accept=>:json
113
- end
99
+ # POST <id> send notification
100
+ def send_notification(app_id, params={})
101
+ url = "#{@service_url}/application/#{app_id}/notification"
102
+ params = {:tags=>[], :devices=>[], :exclude=>[], :alert=>"", :sound=>"", :badge=>0, :kv=>[]}.merge params
103
+
104
+ payload = {"aps"=>{"alert"=>params[:alert],
105
+ "sound"=>params[:sound],
106
+ "badge"=>params[:badge]}}.merge params[:kv]
107
+ payload = {"payload"=>payload}
108
+ data = {"tags"=>params[:tags],
109
+ "devices"=>params[:devices],
110
+ "exclude"=>params[:exclude]}.merge payload
111
+ data = data.to_json
112
+
113
+ RestClient.post url, data, :content_type=>:json, :accept=>:json
114
+ end
114
115
 
115
- # GET get notification status
116
- def get_notification_status(app_id, notification_id)
117
- url = "#{@service_url}/application/#{app_id}/notification/#{notification_id}"
118
-
119
- RestClient.get url, :content_type=>:json, :accept=>:json
120
- end
116
+ # GET get notification status
117
+ def get_notification_status(app_id, notification_id)
118
+ url = "#{@service_url}/application/#{app_id}/notification/#{notification_id}"
119
+
120
+ RestClient.get url, :content_type=>:json, :accept=>:json
121
+ end
121
122
 
122
- # GET all the devices with a tag
123
- def get_devices_by_tag(app_id, tag)
124
- url = "#{@service_url}/application/#{app_id}/tag/#{tag}"
123
+ # GET all the devices with a tag
124
+ def get_devices_by_tag(app_id, tag)
125
+ url = "#{@service_url}/application/#{app_id}/tag/#{tag}"
125
126
 
126
- RestClient.get url, :content_type=>:json, :accept=>:json
127
+ RestClient.get url, :content_type=>:json, :accept=>:json
128
+ end
127
129
  end
128
- end
129
130
 
130
- class Profile < AppushClient
131
- # GET device info
132
- def get_device(device_token)
133
- url = "#{@service_url}/device/#{device_token}"
131
+ class Profile < AppushClient
132
+ # GET device info
133
+ def get_device(device_token)
134
+ url = "#{@service_url}/device/#{device_token}"
134
135
 
135
- RestClient.get url, :content_type=>:json, :accept=>:json
136
- end
136
+ RestClient.get url, :content_type=>:json, :accept=>:json
137
+ end
137
138
 
138
- # PUT register a device with tags
139
- def register_device(device_token, tags=[])
140
- url = "#{@service_url}/device/#{device_token}"
139
+ # PUT register a device with tags
140
+ def register_device(device_token, tags=[])
141
+ url = "#{@service_url}/device/#{device_token}"
141
142
 
142
- data = {:tags=>tags}.to_json
143
+ data = {:tags=>tags}.to_json
143
144
 
144
- RestClient.put url, data, :content_type=>:json, :accept=>:json
145
- end
145
+ RestClient.put url, data, :content_type=>:json, :accept=>:json
146
+ end
146
147
 
147
- # DELETE unregister device
148
- def unregister_device(device_token)
149
- url = "#{@service_url}/device/#{device_token}"
148
+ # DELETE unregister device
149
+ def unregister_device(device_token)
150
+ url = "#{@service_url}/device/#{device_token}"
150
151
 
151
- RestClient.delete url, :content_type=>:json, :accept=>:json
152
+ RestClient.delete url, :content_type=>:json, :accept=>:json
153
+ end
152
154
  end
153
- end
154
155
 
155
- class Application < AppushClient
156
- # POST
157
- def create_profile()
158
- url = "#{@service_url}/profile"
159
-
160
- RestClient.post url, "".to_json, :content_type=>:json, :accept=>:json
156
+ class Application < AppushClient
157
+ # POST
158
+ def create_profile()
159
+ url = "#{@service_url}/profile"
160
+
161
+ RestClient.post url, "".to_json, :content_type=>:json, :accept=>:json
162
+ end
161
163
  end
162
164
  end
@@ -6,6 +6,7 @@ require 'webmock/rspec'
6
6
  require 'json'
7
7
 
8
8
  include WebMock
9
+ include AppushClient
9
10
 
10
11
  describe "Using Appush client API" do
11
12
  before(:each) do
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.3"
4
+ version: "0.4"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Guerrero