Class: Urbanairship::Devices::CreateAndSend

Inherits:
Object
  • Object
show all
Includes:
Common, Loggable
Defined in:
lib/urbanairship/devices/create_and_send.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')) ⇒ CreateAndSend

Returns a new instance of CreateAndSend.



16
17
18
# File 'lib/urbanairship/devices/create_and_send.rb', line 16

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

Instance Attribute Details

#addressesObject

Returns the value of attribute addresses.



9
10
11
# File 'lib/urbanairship/devices/create_and_send.rb', line 9

def addresses
  @addresses
end

#campaignsObject

Returns the value of attribute campaigns.



9
10
11
# File 'lib/urbanairship/devices/create_and_send.rb', line 9

def campaigns
  @campaigns
end

#device_typesObject

Returns the value of attribute device_types.



9
10
11
# File 'lib/urbanairship/devices/create_and_send.rb', line 9

def device_types
  @device_types
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/urbanairship/devices/create_and_send.rb', line 9

def name
  @name
end

#notificationObject

Returns the value of attribute notification.



9
10
11
# File 'lib/urbanairship/devices/create_and_send.rb', line 9

def notification
  @notification
end

#scheduled_timeObject

Returns the value of attribute scheduled_time.



9
10
11
# File 'lib/urbanairship/devices/create_and_send.rb', line 9

def scheduled_time
  @scheduled_time
end

Instance Method Details

#create_and_sendObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/urbanairship/devices/create_and_send.rb', line 51

def create_and_send
  response = @client.send_request(
    method: 'POST',
    body: JSON.dump(payload),
    path: create_and_send_path,
    content_type: 'application/json'
  )
  logger.info("Running create and send for addresses #{@addresses}")
  response
end

#payloadObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/urbanairship/devices/create_and_send.rb', line 28

def payload
  fail ArgumentError, 'addresses must be set for defining payload' if @addresses.nil?
  fail ArgumentError, 'device type array must be set for defining payload' if @device_types.nil?
  fail ArgumentError, 'notification object must be set for defining payload' if @notification.nil?

  validate_address

  full_payload = {
    'audience': {
      'create_and_send': addresses
    },
    'device_types': device_types,
    'notification': notification,
  }

  if campaigns
    campaign_object = {'categories': campaigns}
    full_payload[:campaigns] = campaign_object
  end

  full_payload
end

#scheduleObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/urbanairship/devices/create_and_send.rb', line 73

def schedule
  fail ArgumentError, 'scheduled time must be set to run an operation' if @scheduled_time.nil?

  scheduled_payload = {
    "schedule": {
      "scheduled_time": scheduled_time
    },
    "name": name,
    "push": payload
  }

  response = @client.send_request(
    method: 'POST',
    body: JSON.dump(scheduled_payload),
    path: schedules_path('create-and-send'),
    content_type: 'application/json'
  )
  logger.info("Scheduling create and send operation with name #{@name}")
  response
end

#validateObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/urbanairship/devices/create_and_send.rb', line 62

def validate
  response = @client.send_request(
    method: 'POST',
    body: JSON.dump(payload),
    path: create_and_send_path('validate'),
    content_type: 'application/json'
  )
  logger.info("Validating payload for create and send")
  response
end

#validate_addressObject



20
21
22
23
24
25
26
# File 'lib/urbanairship/devices/create_and_send.rb', line 20

def validate_address
  @addresses.each do |address|
    unless address.include?(:ua_address) or address.include?(:ua_msisdn && :ua_opted_in && :ua_sender)
      fail ArgumentError, 'Missing a component in address portion of the object'
    end
  end
end