Class: TWINPUSH

Inherits:
Object
  • Object
show all
Includes:
Memory
Defined in:
lib/twinpush.rb

Defined Under Namespace

Classes: PayloadSizeError

Constant Summary collapse

DEFAULT_TIMEOUT =
30
FORMAT =
:json
BASE_URI =
'https://subdomain.twinpush.com'
API_URL =
'/api/v2/apps'

Constants included from Memory

Memory::FIXNUM_SIZE, Memory::OBJ_OVERHEAD, Memory::REF_SIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Memory

analyze

Constructor Details

#initialize(authentication_keys, client_options = {}, timeout = nil) ⇒ TWINPUSH

Returns a new instance of TWINPUSH.

Parameters:

  • authentication_keys (Hash)
  • client_options (Hash) (defaults to: {})
  • timeout (Integer) (defaults to: nil)


21
22
23
24
25
26
27
28
29
30
# File 'lib/twinpush.rb', line 21

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

Instance Attribute Details

#api_tokenObject

Returns the value of attribute api_token.



16
17
18
# File 'lib/twinpush.rb', line 16

def api_token
  @api_token
end

#api_token_creatorObject

Returns the value of attribute api_token_creator.



16
17
18
# File 'lib/twinpush.rb', line 16

def api_token_creator
  @api_token_creator
end

#app_idObject

Returns the value of attribute app_id.



16
17
18
# File 'lib/twinpush.rb', line 16

def app_id
  @app_id
end

#uriObject

Returns the value of attribute uri.



16
17
18
# File 'lib/twinpush.rb', line 16

def uri
  @uri
end

Instance Method Details

#clear_properties(device_id) ⇒ Object

Clears all the custom properties for a given device



134
135
136
137
138
139
140
# File 'lib/twinpush.rb', line 134

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(notification_params) ⇒ Object Also known as: create

creates a new notification to be delivered from the platform



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/twinpush.rb', line 49

def create_notification(notification_params)
  memory_size = Memory.analyze(notification_params)
  if memory_size.bytes < 2048 # 2KB limit
    path = "#{API_URL}/#{app_id}/notifications"
    for_uri(uri) do |connection|
      response = connection.post(path, notification_params.to_json)
      build_response(response)
    end
  else
    raise  PayloadSizeError.new "The notification payload exceeds the 2KB limit"
  end
end

#delete_inbox(device_id, notification_id) ⇒ Object

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



107
108
109
110
111
112
113
# File 'lib/twinpush.rb', line 107

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(notification_id) ⇒ Object Also known as: deliveries

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



77
78
79
80
81
82
83
# File 'lib/twinpush.rb', line 77

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

#inbox(device_id) ⇒ Object

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



88
89
90
91
92
93
94
# File 'lib/twinpush.rb', line 88

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) ⇒ Object

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



98
99
100
101
102
103
104
# File 'lib/twinpush.rb', line 98

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(notification_id) ⇒ Object Also known as: report

obtains delivery statistics for a given notification



65
66
67
68
69
70
71
# File 'lib/twinpush.rb', line 65

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

#search_device_notifications(device_id, params) ⇒ Object

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



125
126
127
128
129
130
131
# File 'lib/twinpush.rb', line 125

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) ⇒ Object

assigns value for a device custom property



116
117
118
119
120
121
122
# File 'lib/twinpush.rb', line 116

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(notification_id, device_id = nil) ⇒ Object Also known as: show

obtains details from a previously created notification



33
34
35
36
37
38
39
# File 'lib/twinpush.rb', line 33

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

#show_notification_object(notification_id, device_id = nil) ⇒ Object

obtains notification details in OpenStruct



44
45
46
# File 'lib/twinpush.rb', line 44

def show_notification_object(notification_id, device_id = nil)
  OpenStruct.new JSON.parse(show_notification(notification_id, device_id)[:body])
end