Module: PushToDevices::API

Defined in:
lib/push_to_devices.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.client_idObject

Returns the value of attribute client_id.



25
26
27
# File 'lib/push_to_devices.rb', line 25

def client_id
  @client_id
end

.client_infoObject

Returns the value of attribute client_info.



25
26
27
# File 'lib/push_to_devices.rb', line 25

def client_info
  @client_info
end

.client_secretObject

Returns the value of attribute client_secret.



25
26
27
# File 'lib/push_to_devices.rb', line 25

def client_secret
  @client_secret
end

.debugObject

Returns the value of attribute debug.



25
26
27
# File 'lib/push_to_devices.rb', line 25

def debug
  @debug
end

.hostObject

Returns the value of attribute host.



25
26
27
# File 'lib/push_to_devices.rb', line 25

def host
  @host
end

.portObject

Returns the value of attribute port.



25
26
27
# File 'lib/push_to_devices.rb', line 25

def port
  @port
end

.use_sslObject

Returns the value of attribute use_ssl.



25
26
27
# File 'lib/push_to_devices.rb', line 25

def use_ssl
  @use_ssl
end

.user_agentObject

Returns the value of attribute user_agent.



25
26
27
# File 'lib/push_to_devices.rb', line 25

def user_agent
  @user_agent
end

Class Method Details

.api_portObject



100
101
102
103
104
105
106
# File 'lib/push_to_devices.rb', line 100

def api_port
  if port
    port
  else
    use_ssl ? 443 : 80
  end
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/push_to_devices.rb', line 27

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_credentialsObject



119
120
121
122
123
124
125
126
# File 'lib/push_to_devices.rb', line 119

def generate_client_credentials
  timestamp_s = Time.now.to_i.to_s
  {
    client_id: client_id,
    client_sig: self.generate_client_sig(timestamp_s),
    timestamp: timestamp_s
  }
end

.generate_client_sig(timestamp_s) ⇒ Object



128
129
130
# File 'lib/push_to_devices.rb', line 128

def generate_client_sig(timestamp_s)
  OpenSSL::HMAC.hexdigest 'sha1', client_secret, timestamp_s
end

.generate_httpObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/push_to_devices.rb', line 87

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



108
109
110
111
112
113
114
115
116
117
# File 'lib/push_to_devices.rb', line 108

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/push_to_devices.rb', line 41

def get(endpoint, params={})

  # Set up the HTTP connection
  http = generate_http

  uri = self.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
  }

  self.handle_response(response)
end

.get_service_info(params = {}) ⇒ Object

GETS to service/me Returns the body



142
143
144
# File 'lib/push_to_devices.rb', line 142

def get_service_info(params = {})
  self.get('services/me')
end

.handle_response(response) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/push_to_devices.rb', line 132

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/push_to_devices.rb', line 65

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
  }

  self.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: {}}
  }

}



158
159
160
# File 'lib/push_to_devices.rb', line 158

def post_notification_to_user(params = {})
  self.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: {}}
  }

}



174
175
176
# File 'lib/push_to_devices.rb', line 174

def post_notification_to_users(params = {})
  self.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
}


185
186
187
# File 'lib/push_to_devices.rb', line 185

def register_user_for_push(params = {})
  self.post("users/", params)
end