class Urbanairship::Devices::OpenChannel

Attributes

address[RW]
alert[RW]
channel_id[RW]
extra[RW]
fields[RW]
identifiers[RW]
interactive[RW]
media_attachment[RW]
open_platform[RW]
opt_in[RW]
platform_alert[RW]
summary[RW]
tags[RW]
template_id[RW]
title[RW]

Public Class Methods

new(client: required('client')) click to toggle source
# File lib/urbanairship/devices/open_channel.rb, line 25
def initialize(client: required('client'))
  @client = client
end

Public Instance Methods

create() click to toggle source
# File lib/urbanairship/devices/open_channel.rb, line 29
def create()
  fail TypeError, 'address must be set to create open channel' unless address.is_a? String
  fail TypeError, 'open_platform must be set to create open channel' unless open_platform.is_a? String
  fail TypeError, 'opt_in must be boolean' unless [true, false].include? opt_in

  channel_data = {
    'type': 'open',
    'open': {:open_platform_name => open_platform},
    'opt_in': opt_in,
    'address': address,
    'tags': tags
  }.delete_if {|key, value| value.nil?} #this removes the nil key value pairs

  set_identifiers

  body = {'channel': channel_data}

  response = @client.send_request(
    method: 'POST',
    path: open_channel_path,
    body: JSON.dump(body),
    content_type: 'application/json'
  )
  logger.info("Registering open channel with address: #{address}")
  response
end
lookup(channel_id: required('channel_id')) click to toggle source
# File lib/urbanairship/devices/open_channel.rb, line 88
def lookup(channel_id: required('channel_id'))
  fail ArgumentError, 'channel_id needs to be a string' unless channel_id.is_a? String

  response = @client.send_request(
    method: 'GET',
    path: channel_path(channel_id)
  )
  logger.info("Looking up info on device token #{channel_id}")
  response
end
notification_with_template_id() click to toggle source
# File lib/urbanairship/devices/open_channel.rb, line 99
def notification_with_template_id
  fail TypeError, 'open_platform cannot be nil' if open_platform.nil?

  if alert
    payload = {
      "open::#{open_platform}":{
        'template': {
          'template_id': template_id,
          'fields': {
            'alert': alert
          }
        }
      }
    }
  else
    payload = {
      "open::#{open_platform}":{
        'template': {
          'template_id': template_id,
        }
      }
    }
  end

  payload
end
open_channel_override() click to toggle source
# File lib/urbanairship/devices/open_channel.rb, line 126
def open_channel_override
  fail TypeError, 'open_platform cannot be nil' if open_platform.nil?
  payload = {
      'alert': platform_alert,
      'extra': extra,
      'media_attachment': media_attachment,
      'summary': summary,
      'title': title,
      'interactive': interactive
    }.delete_if {|key, value| value.nil?} #this removes the nil key value pairs

  {'alert': alert,
   "open::#{open_platform}": payload}
end
set_identifiers() click to toggle source
# File lib/urbanairship/devices/open_channel.rb, line 141
def set_identifiers
  if identifiers
    channel_data[:open][:identifiers] = identifiers
  end
end
update(set_tags: required('set_tags')) click to toggle source
# File lib/urbanairship/devices/open_channel.rb, line 56
def update(set_tags: required('set_tags'))
  fail ArgumentError, 'set_tags must be boolean' unless [true, false].include? set_tags
  fail ArgumentError, 'set_tags cannot be true when tags are not set' unless set_tags == true && tags != nil
  fail TypeError, 'opt_in must be boolean' unless [true, false].include? opt_in
  fail TypeError, 'address or channel_id must not be nil' unless address.is_a? String || channel_id.is_a?(String)
  fail TypeError, 'open_platform cannot be nil' unless open_platform.is_a? String
  fail TypeErorr, 'address must not be nil if opt_in is true' unless opt_in.is_a? TrueClass

  channel_data = {
    'type': 'open',
    'open': {'open_platform_name': open_platform},
    'opt_in': opt_in,
    'set_tags': set_tags,
    'channel_id': channel_id,
    'address': address,
    'tags': tags
  }.delete_if {|key, value| value.nil?} #this removes the nil key value pairs

  set_identifiers

  body = {'channel': channel_data}

  response = @client.send_request(
    method: 'POST',
    path: open_channel_path,
    body: JSON.dump(body),
    content_type: 'application/json'
  )
  logger.info("Updating open channel with address #{address}")
  response
end