Class: Hsquare::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/hsquare/device.rb

Overview

Public: APNS/GCM devices with push tokens.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Device

Public: Initializes new device object.

attributes - Attributes of the device.

:id, :device_id     - ID of the device.
:type, :push_type   - Type of the device.
:user_id, :uuid     - ID of the owner.
:token, :push_token - Push token of the device.

Returns newly iniitalized device object.



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

def initialize(attributes = {})
  @id = attributes[:id] || attributes[:device_id]
  @user_id = attributes[:user_id] || attributes[:uuid]
  @token = attributes[:token] || attributes[:push_token]
  @type = attributes[:type] || attributes[:push_type]
  @application = attributes[:application]
end

Instance Attribute Details

#idObject

Public: ID of the device.



5
6
7
# File 'lib/hsquare/device.rb', line 5

def id
  @id
end

#tokenObject

Public: Push token received from service providers.



11
12
13
# File 'lib/hsquare/device.rb', line 11

def token
  @token
end

#typeObject

Public: Type of the device.



14
15
16
# File 'lib/hsquare/device.rb', line 14

def type
  @type
end

#user_idObject

Public: ID of owner of the device.



8
9
10
# File 'lib/hsquare/device.rb', line 8

def user_id
  @user_id
end

Class Method Details

.apns(attributes = {}) ⇒ Object

Public: Initializes new APNS type device.

attributes - Attributes of the device.

:id, :device_id     - ID of the device.
:user_id, :uuid     - ID of the owner.
:token, :push_token - Push token of the device.

Returns newly initialized APNS device.



40
41
42
# File 'lib/hsquare/device.rb', line 40

def self.apns(attributes = {})
  new(attributes.merge(type: 'apns'))
end

.find_by_user_id(user_or_user_id, options = {}) ⇒ Object

Public: Fetch devices for given user.

user_or_user_id - User or ID of user. options - Further options

:application - Label of the application.

Returns Array of HSquare::Device objects retrieved from the server.



23
24
25
26
27
28
29
30
# File 'lib/hsquare/device.rb', line 23

def self.find_by_user_id(user_or_user_id, options = {})
  user_id = user_or_user_id.respond_to?(:id) ? user_or_user_id.id : user_or_user_id
  response = Hsquare.application(options[:application]).admin_client.get('/v1/push/tokens', query: { uuid: user_id })

  response.parsed_response.map do |device_response|
    new(device_response.symbolize_keys)
  end
end

.gcm(attributes = {}) ⇒ Object

Public: Initializes new GCM type device.

attributes - Attributes of the device.

:id, :device_id     - ID of the device.
:user_id, :uuid     - ID of the owner.
:token, :push_token - Push token of the device.

Returns newly initialized APNS device.



52
53
54
# File 'lib/hsquare/device.rb', line 52

def self.gcm(attributes = {})
  new(attributes.merge(type: 'gcm'))
end

Instance Method Details

#registerObject

Public: Registers the device via Kakao API.

Returns Number of days before invalidation.



76
77
78
79
80
# File 'lib/hsquare/device.rb', line 76

def register
  client.post('/v1/push/register', body: {
    uuid: user_id, device_id: id, push_type: type, push_token: token.gsub(/\s/, '')
  })
end

#unregisterObject

Public: Unregisters the device via Kakao API.

Returns Number of days before invalidation.



85
86
87
88
89
# File 'lib/hsquare/device.rb', line 85

def unregister
  client.post('/v1/push/deregister', body: {
    uuid: user_id, device_id: id, push_type: type
  })
end