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.
- data/appush_client.gemspec +1 -1
- data/lib/appush_client.rb +119 -117
- data/spec/appush_client_spec.rb +1 -0
- metadata +1 -1
data/appush_client.gemspec
CHANGED
data/lib/appush_client.rb
CHANGED
@@ -3,160 +3,162 @@
|
|
3
3
|
require 'json'
|
4
4
|
require 'rest_client'
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
module AppushClient
|
7
|
+
class AppushClient
|
8
|
+
attr_accessor :user, :password, :protocol, :service_url
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
15
|
-
|
15
|
+
# cleaning params[:service_url], removing tailing "/"
|
16
|
+
params[:service_url] = clean_service_url(params[:service_url])
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
22
|
-
|
22
|
+
@service_url = "#{@protocol}#{@user}:#{@password}@#{@url}"
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
def to_s
|
26
|
+
"Server = #{@service_url}"
|
27
|
+
end
|
27
28
|
|
28
|
-
|
29
|
+
private
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
def clean_service_url(service_url)
|
32
|
+
while !service_url.match(/\/$/).nil?
|
33
|
+
service_url = service_url.sub(/\/$/, '')
|
34
|
+
end
|
34
35
|
|
35
|
-
|
36
|
+
service_url
|
37
|
+
end
|
36
38
|
end
|
37
|
-
end
|
38
39
|
|
39
|
-
class RootUser < AppushClient
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
-
|
46
|
-
|
46
|
+
RestClient.post url, data, :content_type=>:json, :accept=>:json
|
47
|
+
end
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
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
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
-
|
60
|
-
|
61
|
-
|
60
|
+
data = data.to_json
|
61
|
+
RestClient.put url, data, :content_type=>:json, :accept=>:json
|
62
|
+
end
|
62
63
|
|
63
|
-
|
64
|
-
|
65
|
-
|
64
|
+
# GET
|
65
|
+
def list_applications()
|
66
|
+
url = "#{@service_url}/application"
|
66
67
|
|
67
|
-
|
68
|
-
|
68
|
+
RestClient.get url, :content_type=>:json, :accept=>:json
|
69
|
+
end
|
69
70
|
|
70
|
-
|
71
|
-
|
72
|
-
|
71
|
+
# GET <id>
|
72
|
+
def get_application(id)
|
73
|
+
url = "#{@service_url}/application/#{id}"
|
73
74
|
|
74
|
-
|
75
|
-
|
75
|
+
RestClient.get url, :content_type=>:json, :accept=>:json
|
76
|
+
end
|
76
77
|
|
77
|
-
|
78
|
-
|
79
|
-
|
78
|
+
# GET application/<id>/icon
|
79
|
+
def get_application_icon(id)
|
80
|
+
url = "#{@service_url}/application/#{id}/icon"
|
80
81
|
|
81
|
-
|
82
|
-
|
82
|
+
RestClient.get url, :content_type=>"image/png", :accept=>"image/png"
|
83
|
+
end
|
83
84
|
|
84
|
-
|
85
|
-
|
86
|
-
|
85
|
+
# PUT application/<id>/icon
|
86
|
+
def save_application_icon(app_id, icon)
|
87
|
+
url = "#{@service_url}/application/#{app_id}/icon"
|
87
88
|
|
88
|
-
|
89
|
-
|
89
|
+
RestClient.put url, icon, :content_type=>"image/png"
|
90
|
+
end
|
90
91
|
|
91
|
-
|
92
|
-
|
93
|
-
|
92
|
+
# DELETE <id>
|
93
|
+
def delete_application(id)
|
94
|
+
url = "#{@service_url}/application/#{id}"
|
94
95
|
|
95
|
-
|
96
|
-
|
96
|
+
RestClient.delete url, :content_type=>:json, :accept=>:json
|
97
|
+
end
|
97
98
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
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
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
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
|
-
|
123
|
-
|
124
|
-
|
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
|
-
|
127
|
+
RestClient.get url, :content_type=>:json, :accept=>:json
|
128
|
+
end
|
127
129
|
end
|
128
|
-
end
|
129
130
|
|
130
|
-
class Profile < AppushClient
|
131
|
-
|
132
|
-
|
133
|
-
|
131
|
+
class Profile < AppushClient
|
132
|
+
# GET device info
|
133
|
+
def get_device(device_token)
|
134
|
+
url = "#{@service_url}/device/#{device_token}"
|
134
135
|
|
135
|
-
|
136
|
-
|
136
|
+
RestClient.get url, :content_type=>:json, :accept=>:json
|
137
|
+
end
|
137
138
|
|
138
|
-
|
139
|
-
|
140
|
-
|
139
|
+
# PUT register a device with tags
|
140
|
+
def register_device(device_token, tags=[])
|
141
|
+
url = "#{@service_url}/device/#{device_token}"
|
141
142
|
|
142
|
-
|
143
|
+
data = {:tags=>tags}.to_json
|
143
144
|
|
144
|
-
|
145
|
-
|
145
|
+
RestClient.put url, data, :content_type=>:json, :accept=>:json
|
146
|
+
end
|
146
147
|
|
147
|
-
|
148
|
-
|
149
|
-
|
148
|
+
# DELETE unregister device
|
149
|
+
def unregister_device(device_token)
|
150
|
+
url = "#{@service_url}/device/#{device_token}"
|
150
151
|
|
151
|
-
|
152
|
+
RestClient.delete url, :content_type=>:json, :accept=>:json
|
153
|
+
end
|
152
154
|
end
|
153
|
-
end
|
154
155
|
|
155
|
-
class Application < AppushClient
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
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
|
data/spec/appush_client_spec.rb
CHANGED