Class: Urbanairship::Devices::OpenChannel

Inherits:
Object
  • Object
show all
Includes:
Common, Loggable
Defined in:
lib/urbanairship/devices/open_channel.rb

Constant Summary

Constants included from Common

Common::CONTENT_TYPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

create_logger, logger, #logger

Methods included from Common

#apid_path, #channel_path, #compact_helper, #create_and_send_path, #custom_events_path, #device_token_path, #experiments_path, #lists_path, #named_users_path, #open_channel_path, #pipelines_path, #push_path, #reports_path, #required, #schedules_path, #segments_path, #tag_lists_path, #try_helper

Constructor Details

#initialize(client: required('client')) ⇒ OpenChannel

Returns a new instance of OpenChannel.



25
26
27
# File 'lib/urbanairship/devices/open_channel.rb', line 25

def initialize(client: required('client'))
  @client = client
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



8
9
10
# File 'lib/urbanairship/devices/open_channel.rb', line 8

def address
  @address
end

#alertObject

Returns the value of attribute alert.



8
9
10
# File 'lib/urbanairship/devices/open_channel.rb', line 8

def alert
  @alert
end

#channel_idObject

Returns the value of attribute channel_id.



8
9
10
# File 'lib/urbanairship/devices/open_channel.rb', line 8

def channel_id
  @channel_id
end

#extraObject

Returns the value of attribute extra.



8
9
10
# File 'lib/urbanairship/devices/open_channel.rb', line 8

def extra
  @extra
end

#fieldsObject

Returns the value of attribute fields.



8
9
10
# File 'lib/urbanairship/devices/open_channel.rb', line 8

def fields
  @fields
end

#identifiersObject

Returns the value of attribute identifiers.



8
9
10
# File 'lib/urbanairship/devices/open_channel.rb', line 8

def identifiers
  @identifiers
end

#interactiveObject

Returns the value of attribute interactive.



8
9
10
# File 'lib/urbanairship/devices/open_channel.rb', line 8

def interactive
  @interactive
end

#media_attachmentObject

Returns the value of attribute media_attachment.



8
9
10
# File 'lib/urbanairship/devices/open_channel.rb', line 8

def media_attachment
  @media_attachment
end

#open_platformObject

Returns the value of attribute open_platform.



8
9
10
# File 'lib/urbanairship/devices/open_channel.rb', line 8

def open_platform
  @open_platform
end

#opt_inObject

Returns the value of attribute opt_in.



8
9
10
# File 'lib/urbanairship/devices/open_channel.rb', line 8

def opt_in
  @opt_in
end

#platform_alertObject

Returns the value of attribute platform_alert.



8
9
10
# File 'lib/urbanairship/devices/open_channel.rb', line 8

def platform_alert
  @platform_alert
end

#summaryObject

Returns the value of attribute summary.



8
9
10
# File 'lib/urbanairship/devices/open_channel.rb', line 8

def summary
  @summary
end

#tagsObject

Returns the value of attribute tags.



8
9
10
# File 'lib/urbanairship/devices/open_channel.rb', line 8

def tags
  @tags
end

#template_idObject

Returns the value of attribute template_id.



8
9
10
# File 'lib/urbanairship/devices/open_channel.rb', line 8

def template_id
  @template_id
end

#titleObject

Returns the value of attribute title.



8
9
10
# File 'lib/urbanairship/devices/open_channel.rb', line 8

def title
  @title
end

Instance Method Details

#createObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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')) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# 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_idObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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_overrideObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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_identifiersObject



141
142
143
144
145
# 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')) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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