Module: PushToDevices::API
- Defined in:
- lib/push_to_devices.rb
Class Attribute Summary collapse
-
.client_id ⇒ Object
Returns the value of attribute client_id.
-
.client_info ⇒ Object
Returns the value of attribute client_info.
-
.client_secret ⇒ Object
Returns the value of attribute client_secret.
-
.debug ⇒ Object
Returns the value of attribute debug.
-
.host ⇒ Object
Returns the value of attribute host.
-
.port ⇒ Object
Returns the value of attribute port.
-
.use_ssl ⇒ Object
Returns the value of attribute use_ssl.
-
.user_agent ⇒ Object
Returns the value of attribute user_agent.
Class Method Summary collapse
- .api_port ⇒ Object
- .configure {|_self| ... } ⇒ Object
- .generate_client_credentials ⇒ Object
- .generate_client_sig(timestamp_s) ⇒ Object
- .generate_http ⇒ Object
- .generate_uri_from_params(endpoint, params) ⇒ Object
- .get(endpoint, params = {}) ⇒ Object
-
.get_service_info(params = {}) ⇒ Object
GETS to service/me Returns the body.
- .handle_response(response) ⇒ Object
- .post(endpoint, params = {}) ⇒ Object
-
.post_notification_to_user(params = {}) ⇒ Object
POSTS to users/:unique_hash/notifications to create a notification for a user Expects the following { unique_hash: a unique hash of the user in your service, notification_data: a hash with the following { ios_specific_fields: a hash of what you want to send to your ios users, android_specific_fields: a hash of whaty ou want to send to your android users separated into {, options: {}} } }.
-
.post_notification_to_users(params = {}) ⇒ Object
POSTS to users/notifications to create a notification for a group of users Expects the following { unique_hashes: an array of unique hashes notification_data: a hash with the following { ios_specific_fields: a hash of what you want to send to your ios users, android_specific_fields: a hash of whaty ou want to send to your android users separated into {, options: {}} } }.
-
.register_user_for_push(params = {}) ⇒ Object
POSTS to users/ to register a user for push notifications Expects the following { unique_hash: a unique hash of the user in your service, apn_device_token: an apple ios device token, gcm_registration_id: gcm_registration_id }.
Class Attribute Details
.client_id ⇒ Object
Returns the value of attribute client_id.
24 25 26 |
# File 'lib/push_to_devices.rb', line 24 def client_id @client_id end |
.client_info ⇒ Object
Returns the value of attribute client_info.
24 25 26 |
# File 'lib/push_to_devices.rb', line 24 def client_info @client_info end |
.client_secret ⇒ Object
Returns the value of attribute client_secret.
24 25 26 |
# File 'lib/push_to_devices.rb', line 24 def client_secret @client_secret end |
.debug ⇒ Object
Returns the value of attribute debug.
24 25 26 |
# File 'lib/push_to_devices.rb', line 24 def debug @debug end |
.host ⇒ Object
Returns the value of attribute host.
24 25 26 |
# File 'lib/push_to_devices.rb', line 24 def host @host end |
.port ⇒ Object
Returns the value of attribute port.
24 25 26 |
# File 'lib/push_to_devices.rb', line 24 def port @port end |
.use_ssl ⇒ Object
Returns the value of attribute use_ssl.
24 25 26 |
# File 'lib/push_to_devices.rb', line 24 def use_ssl @use_ssl end |
.user_agent ⇒ Object
Returns the value of attribute user_agent.
24 25 26 |
# File 'lib/push_to_devices.rb', line 24 def user_agent @user_agent end |
Class Method Details
.api_port ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/push_to_devices.rb', line 99 def api_port if port port else use_ssl ? 443 : 80 end end |
.configure {|_self| ... } ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/push_to_devices.rb', line 26 def configure # defaults self.client_id = "" self.client_secret = "" self.user_agent = "PushToDevices RB #{PushToDevices::Config::VERSION}" self.use_ssl = true self.debug = true self.host = "" self.port = 80 self.client_info = {version: PushToDevices::Config::VERSION} yield self end |
.generate_client_credentials ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/push_to_devices.rb', line 118 def generate_client_credentials = Time.now.to_i.to_s { client_id: client_id, client_sig: self.generate_client_sig(), timestamp: } end |
.generate_client_sig(timestamp_s) ⇒ Object
127 128 129 |
# File 'lib/push_to_devices.rb', line 127 def generate_client_sig() OpenSSL::HMAC.hexdigest 'sha1', client_secret, end |
.generate_http ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/push_to_devices.rb', line 86 def generate_http http = Net::HTTP.new( host, api_port ) http.use_ssl = (use_ssl == true) if debug == true http.set_debug_output($stdout) http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http end |
.generate_uri_from_params(endpoint, params) ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/push_to_devices.rb', line 107 def generate_uri_from_params(endpoint, params) if params.empty? "/#{endpoint}" else query_string = params.map {|k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&") "/#{endpoint}?#{query_string}" end end |
.get(endpoint, params = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/push_to_devices.rb', line 40 def get(endpoint, params={}) # Set up the HTTP connection http = generate_http uri = generate_uri_from_params(endpoint, params) # Set up the request request = Net::HTTP::Get.new(uri) # Set credentials client_credentials = generate_client_credentials request["server-client-id"] = client_credentials[:client_id] request["client-sig"] = client_credentials[:client_sig] request["timestamp"] = client_credentials[:timestamp] # Fire the package ! response = http.start {|http| http.request request } handle_response(response) end |
.get_service_info(params = {}) ⇒ Object
GETS to service/me Returns the body
141 142 143 |
# File 'lib/push_to_devices.rb', line 141 def get_service_info(params = {}) get('services/me') end |
.handle_response(response) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/push_to_devices.rb', line 131 def handle_response(response) if response.code.to_i != 200 raise PushToDevices::Exception.new(response.code, response.body) else response.body end end |
.post(endpoint, params = {}) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/push_to_devices.rb', line 64 def post(endpoint, params = {}) # Set up the HTTP connection http = generate_http request = Net::HTTP::Post.new("/"+endpoint, initheader = {'Content-Type' =>'application/json'}) request.body = params.to_json # Set credentials client_credentials = generate_client_credentials request["server-client-id"] = client_credentials[:client_id] request["client-sig"] = client_credentials[:client_sig] request["timestamp"] = client_credentials[:timestamp] # Fire the package ! response = http.start {|http| http.request request } handle_response(response) end |
.post_notification_to_user(params = {}) ⇒ Object
POSTS to users/:unique_hash/notifications to create a notification for a user Expects the following
unique_hash: a unique hash of the user in your service,
notification_data: a hash with the following
{
ios_specific_fields: a hash of what you want to send to your ios users,
android_specific_fields: a hash of whaty ou want to send to your android users
separated into {data: {, options: {}}
}
}
157 158 159 |
# File 'lib/push_to_devices.rb', line 157 def post_notification_to_user(params = {}) post("users/#{params.delete(:unique_hash)}/notifications", params.delete(:notification_data)) end |
.post_notification_to_users(params = {}) ⇒ Object
POSTS to users/notifications to create a notification for a group of users Expects the following
unique_hashes: an array of unique hashes
notification_data: a hash with the following
{
ios_specific_fields: a hash of what you want to send to your ios users,
android_specific_fields: a hash of whaty ou want to send to your android users
separated into {data: {, options: {}}
}
}
173 174 175 |
# File 'lib/push_to_devices.rb', line 173 def post_notification_to_users(params = {}) post("users/notifications", params) end |
.register_user_for_push(params = {}) ⇒ Object
POSTS to users/ to register a user for push notifications Expects the following {
unique_hash: a unique hash of the user in your service,
apn_device_token: an apple ios device token,
gcm_registration_id: gcm_registration_id
}
184 185 186 |
# File 'lib/push_to_devices.rb', line 184 def register_user_for_push(params = {}) post("users/", params) end |