appush_client 0.5 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/appush_client.gemspec +5 -7
  2. data/lib/appush_client.rb +62 -57
  3. metadata +5 -14
@@ -2,16 +2,14 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'appush_client'
5
- s.version = '0.5'
5
+ s.version = '0.6'
6
6
 
7
7
  s.authors = ['Igor Guerrero']
8
- s.date = '2010-01-09'
9
- s.description = "Ruby client library for Appush PUSH service."
8
+ s.date = '2010-02-21'
9
+ s.description = 'Ruby client library for Appush PUSH service.'
10
10
  s.email = ['igor@appush.com']
11
- s.homepage = 'http://appush.com'
11
+ s.homepage = 'http://github.com/APPush/Appush-Ruby-Client'
12
12
  s.files = ['appush_client.gemspec', 'CONTRIBUTORS', 'LICENSE', 'README.md', 'lib/appush_client.rb',
13
13
  'spec/appush_client_spec.rb']
14
- s.summary = "Ruby client library for Appush PUSH service."
15
-
16
- s.add_dependency 'rest-client', '>= 1.3.1'
14
+ s.summary = 'Ruby client library for Appush PUSH service.'
17
15
  end
@@ -1,7 +1,4 @@
1
- #appush_client.rb
2
-
3
1
  require 'json'
4
- require 'rest_client'
5
2
 
6
3
  module AppushClient
7
4
  class AppushClient
@@ -12,93 +9,113 @@ module AppushClient
12
9
  @user = user
13
10
  @password = password
14
11
 
15
- # cleaning params[:service_url], removing tailing "/"
16
- params[:service_url] = clean_service_url(params[:service_url])
12
+ u = URI.parse(params[:service_url])
13
+ @http = Net::HTTP.new(u.host, u.port)
14
+ @http.use_ssl = true if u.scheme == 'https'
15
+ @path = u.path
16
+ @service_url = "#{u.scheme}://#@user:#@password@#{u.host}#{u.path}"
17
+ end
17
18
 
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
19
+ def post(path, body=nil, options={})
20
+ res = request(:post, path, body, options)
21
+ return res.body
22
+ end
21
23
 
22
- @service_url = "#{@protocol}#{@user}:#{@password}@#{@url}"
24
+ def put(path, body=nil, options={})
25
+ res = request(:put, path, body, options)
26
+ return res.body
23
27
  end
24
28
 
25
- def to_s
26
- "Server = #{@service_url}"
29
+ def delete(path, options={})
30
+ res = request(:delete, path, nil, options)
31
+ return res.body
27
32
  end
28
33
 
29
- private
34
+ def get(path, options={})
35
+ res = request(:get, path, nil, options)
36
+ return res.body
37
+ end
30
38
 
31
- def clean_service_url(service_url)
32
- while !service_url.match(/\/$/).nil?
33
- service_url = service_url.sub(/\/$/, '')
39
+ def request(method, path, body, options)
40
+ options = {:content_type=>'application/json'}.merge(options)
41
+ req = case method
42
+ when :get
43
+ Net::HTTP::Get.new("#@path#{path}")
44
+ when :post
45
+ Net::HTTP::Post.new("#@path#{path}")
46
+ when :put
47
+ Net::HTTP::Put.new("#@path#{path}")
48
+ when :delete
49
+ Net::HTTP::Delete.new("#@path#{path}")
34
50
  end
51
+ req.basic_auth @user, @password
52
+ req.content_type = options[:content_type]
53
+ req.body = body
54
+ res = @http.request(req)
55
+ check_for_error(res)
56
+ end
57
+
58
+ def check_for_error(res)
59
+ case res.code
60
+ when /^(4|5)/
61
+ raise AppushClient.const_set(res.class.to_s.split('::').last, Class.new(StandardError))
62
+ else
63
+ return res
64
+ end
65
+ end
35
66
 
36
- service_url
67
+ def to_s
68
+ "Server = #{@service_url}"
37
69
  end
38
70
  end
39
71
 
40
72
  class RootUser < AppushClient
41
73
  # POST
42
74
  def create_application(name, env="dev", dev_pem="", prod_pem="")
43
- url = "#{@service_url}/application"
44
75
  data = {:name=>name, :env=>env, :dev_pem=>dev_pem, :prod_pem=>prod_pem}.to_json
45
-
46
- RestClient.post url, data, :content_type=>:json, :accept=>:json
76
+ post "/application", data
47
77
  end
48
78
 
49
79
  # PUT
50
80
  # parameters are optional: :name, :env, :dev_pem, :prod_pem
51
81
  def modify_application(id, params={})
52
- url = "#{@service_url}/application/#{id}"
53
82
  data = Hash.new
54
-
55
83
  data[:name] = params[:name] if params[:name]
56
84
  data[:env] = params[:env] if params[:env]
57
85
  data[:dev_pem] = params[:dev_pem] if params[:dev_pem]
58
86
  data[:prod_pem] = params[:prod_pem] if params[:prod_pem]
59
87
 
60
88
  data = data.to_json
61
- RestClient.put url, data, :content_type=>:json, :accept=>:json
89
+ put "/application/#{id}", data
62
90
  end
63
91
 
64
92
  # GET
65
93
  def list_applications()
66
- url = "#{@service_url}/application"
67
-
68
- RestClient.get url, :content_type=>:json, :accept=>:json
94
+ get "/application"
69
95
  end
70
96
 
71
97
  # GET <id>
72
98
  def get_application(id)
73
- url = "#{@service_url}/application/#{id}"
74
-
75
- RestClient.get url, :content_type=>:json, :accept=>:json
99
+ get "/application/#{id}"
76
100
  end
77
101
 
78
102
  # GET application/<id>/icon
79
103
  def get_application_icon(id)
80
- url = "#{@service_url}/application/#{id}/icon"
81
-
82
- RestClient.get url, :content_type=>"image/png", :accept=>"image/png"
104
+ get "/application/#{id}/icon", :content_type=>"image/png"
83
105
  end
84
106
 
85
107
  # PUT application/<id>/icon
86
108
  def save_application_icon(app_id, icon)
87
- url = "#{@service_url}/application/#{app_id}/icon"
88
-
89
- RestClient.put url, icon, :content_type=>"image/png"
109
+ put "/application/#{app_id}/icon", icon, :content_type=>"image/png"
90
110
  end
91
111
 
92
112
  # DELETE <id>
93
113
  def delete_application(id)
94
- url = "#{@service_url}/application/#{id}"
95
-
96
- RestClient.delete url, :content_type=>:json, :accept=>:json
114
+ delete "/application/#{id}"
97
115
  end
98
116
 
99
117
  # POST <id> send notification
100
118
  def send_notification(app_id, params={})
101
- url = "#{@service_url}/application/#{app_id}/notification"
102
119
  params = {:tags=>[], :devices=>[], :exclude=>[], :alert=>"", :sound=>"", :badge=>0, :kv=>[]}.merge params
103
120
 
104
121
  payload = {"aps"=>{"alert"=>params[:alert],
@@ -110,55 +127,43 @@ module AppushClient
110
127
  "exclude"=>params[:exclude]}.merge payload
111
128
  data = data.to_json
112
129
 
113
- RestClient.post url, data, :content_type=>:json, :accept=>:json
130
+ post "/application/#{app_id}/notification", data
114
131
  end
115
132
 
116
133
  # GET get notification status
117
134
  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
135
+ get "/application/#{app_id}/notification/#{notification_id}"
121
136
  end
122
137
 
123
138
  # GET all the devices with a tag
124
139
  def get_devices_by_tag(app_id, tag)
125
- url = "#{@service_url}/application/#{app_id}/tag/#{tag}"
126
-
127
- RestClient.get url, :content_type=>:json, :accept=>:json
140
+ get "/application/#{app_id}/tag/#{tag}"
128
141
  end
129
142
  end
130
143
 
131
144
  class Profile < AppushClient
132
145
  # GET device info
133
146
  def get_device(device_token)
134
- url = "#{@service_url}/device/#{device_token}"
135
-
136
- RestClient.get url, :content_type=>:json, :accept=>:json
147
+ get "/device/#{device_token}"
137
148
  end
138
149
 
139
150
  # PUT register a device with tags
140
151
  def register_device(device_token, tags=[])
141
- url = "#{@service_url}/device/#{device_token}"
142
-
143
152
  data = {:tags=>tags}.to_json
144
153
 
145
- RestClient.put url, data, :content_type=>:json, :accept=>:json
154
+ put "/device/#{device_token}", data
146
155
  end
147
156
 
148
157
  # DELETE unregister device
149
158
  def unregister_device(device_token)
150
- url = "#{@service_url}/device/#{device_token}"
151
-
152
- RestClient.delete url, :content_type=>:json, :accept=>:json
159
+ delete "/device/#{device_token}"
153
160
  end
154
161
  end
155
162
 
156
163
  class Application < AppushClient
157
164
  # POST
158
165
  def create_profile()
159
- url = "#{@service_url}/profile"
160
-
161
- RestClient.post url, "".to_json, :content_type=>:json, :accept=>:json
166
+ post "/profile", "{}"
162
167
  end
163
168
  end
164
169
  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.5"
4
+ version: "0.6"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Guerrero
@@ -9,19 +9,10 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-09 00:00:00 -05:00
12
+ date: 2010-02-21 00:00:00 -05:00
13
13
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: rest-client
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 1.3.1
24
- version:
14
+ dependencies: []
15
+
25
16
  description: Ruby client library for Appush PUSH service.
26
17
  email:
27
18
  - igor@appush.com
@@ -39,7 +30,7 @@ files:
39
30
  - lib/appush_client.rb
40
31
  - spec/appush_client_spec.rb
41
32
  has_rdoc: true
42
- homepage: http://appush.com
33
+ homepage: http://github.com/APPush/Appush-Ruby-Client
43
34
  licenses: []
44
35
 
45
36
  post_install_message: