push_to_devices 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -42,7 +42,7 @@ The example below assumes a typical Rails-like setup (with a YAML file to hold y
42
42
  push_server_config = YAML.load_file(Rails.root + 'config/push_server.yml')[rails_env].symbolize_keys
43
43
 
44
44
  PushToDevices::API.configure do |config|
45
- config.api_host = push_server_config[:api_host]
45
+ config.host = push_server_config[:host]
46
46
  config.client_id = push_server_config[:client_id]
47
47
  config.client_secret = push_server_config[:client_secret]
48
48
  config.use_ssl = push_server_config[:use_ssl]
@@ -1,12 +1,11 @@
1
1
  require "net/http"
2
2
  require "net/https"
3
- require "active_support/core_ext/module/attribute_accessors"
4
3
  require "cgi"
5
4
 
6
5
  module PushToDevices
7
6
 
8
7
  module Config
9
- VERSION = '0.01'
8
+ VERSION = '0.1.0'
10
9
  end
11
10
 
12
11
  class Exception < ::StandardError
@@ -22,172 +21,173 @@ module PushToDevices
22
21
 
23
22
  module API
24
23
 
25
- mattr_accessor :client_id
26
- @@client_id = ""
27
-
28
- mattr_accessor :client_secret
29
- @@client_secret = ""
24
+ class << self
25
+ attr_accessor :client_id, :client_secret, :user_agent, :use_ssl, :debug, :host, :port, :client_info
26
+
27
+ def configure
28
+ # defaults
29
+ client_id = ""
30
+ client_secret = ""
31
+ user_agent = "PushToDevices RB #{PushToDevices::Config::VERSION}"
32
+ use_ssl = true
33
+ debug = true
34
+ host = ""
35
+ port = 80
36
+ client_info = {version: PushToDevices::Config::VERSION}
37
+
38
+ yield self
39
+ end
30
40
 
31
- mattr_accessor :user_agent
32
- @@user_agent = "PushToDevices RB #{PushToDevices::Config::VERSION}"
41
+ def get(endpoint, params={})
33
42
 
34
- mattr_accessor :use_ssl
35
- @@use_ssl = true
43
+ # Set up the HTTP connection
44
+ http = Net::HTTP.new(
45
+ api_host,
46
+ api_port
47
+ )
48
+ http.use_ssl = (use_ssl == true)
49
+ if debug == true
50
+ http.set_debug_output($stdout)
51
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
52
+ end
36
53
 
37
- mattr_accessor :debug
38
- @@debug = true
54
+ uri = self.generate_uri_from_params(endpoint, params)
39
55
 
40
- mattr_accessor :api_host
41
- @@api_host = ""
56
+ # Set up the request
57
+ request = Net::HTTP::Get.new(uri)
42
58
 
43
- mattr_accessor :client_info
44
- @@client_info = {version: PushToDevices::Config::VERSION}
59
+ # Set credentials
60
+ client_credentials = generate_client_credentials
61
+ request["server-client-id"] = client_credentials[:client_id]
62
+ request["client-sig"] = client_credentials[:client_sig]
63
+ request["timestamp"] = client_credentials[:timestamp]
45
64
 
46
- def self.configure
47
- yield self if block_given?
48
- end
65
+ # Fire the package !
66
+ response = http.start {|http|
67
+ http.request request
68
+ }
49
69
 
50
- def self.get(endpoint, params={})
51
-
52
- # Set up the HTTP connection
53
- http = Net::HTTP.new(
54
- @@api_host,
55
- @@use_ssl == true ? 443 : 80
56
- )
57
- http.use_ssl = (@@use_ssl == true)
58
- if @@debug == true
59
- http.set_debug_output($stdout)
60
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
70
+ self.handle_response(response)
61
71
  end
62
72
 
63
- uri = self.generate_uri_from_params(endpoint, params)
64
-
65
- # Set up the request
66
- request = Net::HTTP::Get.new(uri)
67
-
68
- # Set credentials
69
- client_credentials = generate_client_credentials
70
- request["server-client-id"] = client_credentials[:client_id]
71
- request["client-sig"] = client_credentials[:client_sig]
72
- request["timestamp"] = client_credentials[:timestamp]
73
-
74
- # Fire the package !
75
- response = http.start {|http|
76
- http.request request
77
- }
78
-
79
- self.handle_response(response)
80
- end
81
-
82
- def self.post(endpoint, params = {})
83
-
84
- # Set up the HTTP connection
85
- http = Net::HTTP.new(
86
- @@api_host,
87
- @@use_ssl == true ? 443 : 80
88
- )
89
- http.use_ssl = (@@use_ssl == true)
90
- if @@debug == true
91
- http.set_debug_output($stdout)
92
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
73
+ def post(endpoint, params = {})
74
+
75
+ # Set up the HTTP connection
76
+ http = Net::HTTP.new(
77
+ api_host,
78
+ api_port
79
+ )
80
+ http.use_ssl = (use_ssl == true)
81
+ if debug == true
82
+ http.set_debug_output($stdout)
83
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
84
+ end
85
+
86
+ request = Net::HTTP::Post.new("/"+endpoint, initheader = {'Content-Type' =>'application/json'})
87
+ request.body = params.to_json
88
+
89
+ # Set credentials
90
+ client_credentials = generate_client_credentials
91
+ request["server-client-id"] = client_credentials[:client_id]
92
+ request["client-sig"] = client_credentials[:client_sig]
93
+ request["timestamp"] = client_credentials[:timestamp]
94
+
95
+ # Fire the package !
96
+ response = http.start {|http|
97
+ http.request request
98
+ }
99
+
100
+ self.handle_response(response)
93
101
  end
94
102
 
95
- request = Net::HTTP::Post.new("/"+endpoint, initheader = {'Content-Type' =>'application/json'})
96
- request.body = params.to_json
97
-
98
- # Set credentials
99
- client_credentials = generate_client_credentials
100
- request["server-client-id"] = client_credentials[:client_id]
101
- request["client-sig"] = client_credentials[:client_sig]
102
- request["timestamp"] = client_credentials[:timestamp]
103
-
104
- # Fire the package !
105
- response = http.start {|http|
106
- http.request request
107
- }
108
-
109
- self.handle_response(response)
110
- end
103
+ def api_port
104
+ if port
105
+ port
106
+ else
107
+ use_ssl ? 443 : 80
108
+ end
109
+ end
111
110
 
112
- def self.generate_uri_from_params(endpoint, params)
113
- if params.empty?
114
- "/#{endpoint}"
115
- else
116
- query_string = params.map {|k, v|
117
- "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
118
- }.join("&")
119
- "/#{endpoint}?#{query_string}"
111
+ def generate_uri_from_params(endpoint, params)
112
+ if params.empty?
113
+ "/#{endpoint}"
114
+ else
115
+ query_string = params.map {|k, v|
116
+ "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
117
+ }.join("&")
118
+ "/#{endpoint}?#{query_string}"
119
+ end
120
120
  end
121
- end
122
121
 
123
- def self.generate_client_credentials
124
- timestamp_s = Time.now.to_i.to_s
125
- {
126
- client_id: @@client_id,
127
- client_sig: self.generate_client_sig(timestamp_s),
128
- timestamp: timestamp_s
129
- }
130
- end
122
+ def generate_client_credentials
123
+ timestamp_s = Time.now.to_i.to_s
124
+ {
125
+ client_id: client_id,
126
+ client_sig: self.generate_client_sig(timestamp_s),
127
+ timestamp: timestamp_s
128
+ }
129
+ end
131
130
 
132
- def self.generate_client_sig(timestamp_s)
133
- OpenSSL::HMAC.hexdigest 'sha1', @@client_secret, timestamp_s
134
- end
131
+ def generate_client_sig(timestamp_s)
132
+ OpenSSL::HMAC.hexdigest 'sha1', client_secret, timestamp_s
133
+ end
135
134
 
136
- def self.handle_response(response)
137
- if response.code.to_i != 200
138
- raise PushToDevices::Exception.new(response.code, response.body)
139
- else
140
- response.body
135
+ def handle_response(response)
136
+ if response.code.to_i != 200
137
+ raise PushToDevices::Exception.new(response.code, response.body)
138
+ else
139
+ response.body
140
+ end
141
141
  end
142
- end
143
142
 
144
- # GETS to service/me
145
- # Returns the body
146
- def self.get_service_info(params = {})
147
- self.get('services/me')
148
- end
143
+ # GETS to service/me
144
+ # Returns the body
145
+ def get_service_info(params = {})
146
+ self.get('services/me')
147
+ end
149
148
 
150
- # POSTS to users/:unique_hash/notifications
151
- # to create a notification for a user
152
- # Expects the following
153
- # {
154
- # unique_hash: a unique hash of the user in your service,
155
- # notification_data: a hash with the following
156
- # {
157
- # ios_specific_fields: a hash of what you want to send to your ios users,
158
- # android_specific_fields: a hash of whaty ou want to send to your android users
159
- # separated into {data: {}, options: {}}
160
- # }
161
- # }
162
- def self.post_notification_to_user(params = {})
163
- self.post("users/#{params.delete(:unique_hash)}/notifications", params.delete(:notification_data))
164
- end
149
+ # POSTS to users/:unique_hash/notifications
150
+ # to create a notification for a user
151
+ # Expects the following
152
+ # {
153
+ # unique_hash: a unique hash of the user in your service,
154
+ # notification_data: a hash with the following
155
+ # {
156
+ # ios_specific_fields: a hash of what you want to send to your ios users,
157
+ # android_specific_fields: a hash of whaty ou want to send to your android users
158
+ # separated into {data: {}, options: {}}
159
+ # }
160
+ # }
161
+ def post_notification_to_user(params = {})
162
+ self.post("users/#{params.delete(:unique_hash)}/notifications", params.delete(:notification_data))
163
+ end
165
164
 
166
- # POSTS to users/notifications
167
- # to create a notification for a group of users
168
- # Expects the following
169
- # {
170
- # unique_hashes: an array of unique hashes
171
- # notification_data: a hash with the following
172
- # {
173
- # ios_specific_fields: a hash of what you want to send to your ios users,
174
- # android_specific_fields: a hash of whaty ou want to send to your android users
175
- # separated into {data: {}, options: {}}
176
- # }
177
- # }
178
- def self.post_notification_to_users(params = {})
179
- self.post("users/notifications", params)
180
- end
165
+ # POSTS to users/notifications
166
+ # to create a notification for a group of users
167
+ # Expects the following
168
+ # {
169
+ # unique_hashes: an array of unique hashes
170
+ # notification_data: a hash with the following
171
+ # {
172
+ # ios_specific_fields: a hash of what you want to send to your ios users,
173
+ # android_specific_fields: a hash of whaty ou want to send to your android users
174
+ # separated into {data: {}, options: {}}
175
+ # }
176
+ # }
177
+ def post_notification_to_users(params = {})
178
+ self.post("users/notifications", params)
179
+ end
181
180
 
182
- # POSTS to users/ to register a user for push notifications
183
- # Expects the following
184
- # {
185
- # unique_hash: a unique hash of the user in your service,
186
- # apn_device_token: an apple ios device token,
187
- # gcm_registration_id: gcm_registration_id
188
- # }
189
- def self.register_user_for_push(params = {})
190
- self.post("users/", params)
181
+ # POSTS to users/ to register a user for push notifications
182
+ # Expects the following
183
+ # {
184
+ # unique_hash: a unique hash of the user in your service,
185
+ # apn_device_token: an apple ios device token,
186
+ # gcm_registration_id: gcm_registration_id
187
+ # }
188
+ def register_user_for_push(params = {})
189
+ self.post("users/", params)
190
+ end
191
191
  end
192
192
 
193
193
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = %q{push_to_devices}
3
- gem.version = "0.0.9"
3
+ gem.version = "0.1.0"
4
4
  gem.date = %q{2013-02-15}
5
5
  gem.authors = ["Lloyd Meta"]
6
6
  gem.email = ["lloydmeta@gmail.com"]
@@ -18,7 +18,4 @@ Gem::Specification.new do |gem|
18
18
  gem.add_development_dependency 'rake'
19
19
  gem.add_development_dependency 'rspec'
20
20
  gem.add_development_dependency 'webmock'
21
- gem.add_development_dependency 'active_support'
22
- gem.add_development_dependency 'i18n'
23
-
24
21
  end
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,7 @@ require 'bundler/setup'
2
2
  require 'rspec'
3
3
  require 'push_to_devices'
4
4
  require 'webmock/rspec'
5
- require 'active_support/core_ext'
5
+ require 'json'
6
6
 
7
7
  Dir[File.expand_path('../support/**/*', __FILE__)].each { |f| require f }
8
8
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: push_to_devices
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -75,38 +75,6 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
- - !ruby/object:Gem::Dependency
79
- name: active_support
80
- requirement: !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
83
- - - ! '>='
84
- - !ruby/object:Gem::Version
85
- version: '0'
86
- type: :development
87
- prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ! '>='
92
- - !ruby/object:Gem::Version
93
- version: '0'
94
- - !ruby/object:Gem::Dependency
95
- name: i18n
96
- requirement: !ruby/object:Gem::Requirement
97
- none: false
98
- requirements:
99
- - - ! '>='
100
- - !ruby/object:Gem::Version
101
- version: '0'
102
- type: :development
103
- prerelease: false
104
- version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
- requirements:
107
- - - ! '>='
108
- - !ruby/object:Gem::Version
109
- version: '0'
110
78
  description: A Ruby library for interfacing with push_to_devices
111
79
  email:
112
80
  - lloydmeta@gmail.com