class TWINPUSH

Constants

API_URL
BASE_URI
DEFAULT_TIMEOUT
FORMAT

Attributes

api_token[RW]
api_token_creator[RW]
app_id[RW]
uri[RW]

Public Class Methods

new(authentication_keys, client_options = {}, timeout = nil) click to toggle source

@param [Hash] authentication_keys @param [Hash] client_options @param [Integer] timeout

# File lib/twinpush.rb, line 16
def initialize(authentication_keys, client_options = {}, timeout = nil)
  validate_keys(authentication_keys)
  authentication_keys.each_pair do |key, value|
    instance_variable_set("@#{key}", value)
    self.class.instance_eval { attr_accessor key.to_sym }
  end
  @uri = BASE_URI.sub 'subdomain', @subdomain
  @client_options = client_options
  @timeout = timeout | DEFAULT_TIMEOUT
end

Public Instance Methods

clear_properties(device_id) click to toggle source

Clears all the custom properties for a given device

# File lib/twinpush.rb, line 124
def clear_properties(device_id)
  path = "#{API_URL}/#{app_id}/devices/#{device_id}/clear_custom_properties"
  for_uri(uri) do |connection|
    response = connection.delete(path)
    build_response(response)
  end
end
create(notification_params)
Alias for: create_notification
create_notification(notification_params) click to toggle source

creates a new notification to be delivered from the platform

# File lib/twinpush.rb, line 44
def create_notification(notification_params)
  path = "#{API_URL}/#{app_id}/notifications"
  for_uri(uri) do |connection|
    response = connection.post(path, notification_params.to_json)
    build_response(response)
  end
end
Also aliased as: create
delete_inbox(device_id, notification_id) click to toggle source

Removes the selected notification from the inbox of the user (or alias) associated to the device

# File lib/twinpush.rb, line 97
def delete_inbox(device_id, notification_id)
  path = "#{API_URL}/#{app_id}/devices/#{device_id}/notifications/#{notification_id}"
  for_uri(uri) do |connection|
    response = connection.delete(path)
    build_response(response)
  end
end
deliveries(notification_id)
deliveries_notification(notification_id) click to toggle source

Obtains paginated list of all the deliveries for a given notification. This is useful to obtain exactly who has been the recipient of the notification and also who has opened it

# File lib/twinpush.rb, line 67
def deliveries_notification(notification_id)
  path = "#{API_URL}/#{app_id}/notifications/#{notification_id}/deliveries"
  for_uri(uri) do |connection|
    response = connection.get(path)
    build_response(response)
  end
end
Also aliased as: deliveries
inbox(device_id) click to toggle source

Makes a paginated search of the notifications sent to an user through the device alias. It allows filtering by notification tags

# File lib/twinpush.rb, line 78
def inbox(device_id)
  path = "#{API_URL}/#{app_id}/devices/#{device_id}/inbox"
  for_uri(uri) do |connection|
    response = connection.get(path)
    build_response(response)
  end
end
inbox_summary(device_id) click to toggle source

Obtains a fast summary of the notification inbox associated to the current device alias It offers the total notification count and the unopened notification count

# File lib/twinpush.rb, line 88
def inbox_summary(device_id)
  path = "#{API_URL}/#{app_id}/devices/#{device_id}/inbox_summary"
  for_uri(uri) do |connection|
    response = connection.get(path)
    build_response(response)
  end
end
report(notification_id)
Alias for: report_notification
report_notification(notification_id) click to toggle source

obtains delivery statistics for a given notification

# File lib/twinpush.rb, line 55
def report_notification(notification_id)
  path = "#{API_URL}/#{app_id}/notifications/#{notification_id}/report"
  for_uri(uri) do |connection|
    response = connection.get(path)
    build_response(response)
  end
end
Also aliased as: report
search_device_notifications(device_id, params) click to toggle source

Makes a paginated search of the notifications received by a device. It allows filtering by notification tags

# File lib/twinpush.rb, line 115
def search_device_notifications(device_id, params)
  path = "#{API_URL}/#{app_id}/devices/#{device_id}/search_notifications"
  for_uri(uri) do |connection|
    response = connection.post(path, params.to_json)
    build_response(response)
  end
end
set_custom_property(device_id, properties) click to toggle source

assigns value for a device custom property

# File lib/twinpush.rb, line 106
def set_custom_property(device_id, properties)
  path = "#{API_URL}/#{app_id}/devices/#{device_id}/set_custom_property"
  for_uri(uri) do |connection|
    response = connection.post(path, properties.to_json)
    build_response(response)
  end
end
show(notification_id, device_id = nil)
Alias for: show_notification
show_notification(notification_id, device_id = nil) click to toggle source

obtains details from a previously created notification

# File lib/twinpush.rb, line 28
def show_notification(notification_id, device_id = nil)
  path = "#{API_URL}/#{app_id}#{"/devices/#{device_id}" if device_id }/notifications/#{notification_id}"
  for_uri(uri) do |connection|
    response = connection.get(path)
    build_response(response)
  end
end
Also aliased as: show
show_notification_object(notification_id, device_id = nil) click to toggle source

obtains notification details in OpenStruct

# File lib/twinpush.rb, line 39
def show_notification_object(notification_id, device_id = nil)
  OpenStruct.new JSON.parse(show_notification(notification_id, device_id)[:body])
end

Private Instance Methods

build_response(response) click to toggle source
# File lib/twinpush.rb, line 154
def build_response(response)
  body = response.body || {}
  response_hash = { body: body, headers: response.headers, status_code: response.status }
  case response.status
  when 200
    response_hash[:response] = 'success'
  when 403
    response_hash[:response] = 'API Key Token or Creator API Key Token are not valid or does not match with App ID'
  when 404
    response_hash[:response] = 'Application not found for given application_id, device not found for given device_id or delivery not found for given notification and device'
  when 422
    response_hash[:response] = 'Some of the parameters given is not valid when trying to create a notification, create a device property or register or update a device'
  when 500..599
    response_hash[:response] = 'There was an internal error in the Twinpush server while trying to process the request.'
  end
  response_hash
end
for_uri(uri, extra_headers = {}) { |connection| ... } click to toggle source
# File lib/twinpush.rb, line 141
def for_uri(uri, extra_headers = {})
  connection = ::Faraday.new(:url => uri) do |faraday|
    faraday.adapter  Faraday.default_adapter
    faraday.headers["Content-Type"] = "application/json"
    faraday.headers["X-TwinPush-REST-API-Key-Creator"] = api_token_creator
    faraday.headers["X-TwinPush-REST-API-Token"] = api_token
    extra_headers.each do |key, value|
      faraday.headers[key] = value
    end
  end
  yield connection
end
validate_keys(authentication_keys) click to toggle source
# File lib/twinpush.rb, line 134
def validate_keys(authentication_keys)
  required_params = [:app_id, :subdomain, :api_token, :api_token_creator]
  (required_params - authentication_keys.keys).each do |params|
    raise ArgumentError, "Required authentication_key #{params} missing"
  end
end