Class: Urbanairship::Push::ScheduledPush

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

Constant Summary

Constants included from Common

Common::CONTENT_TYPE

Instance Attribute Summary collapse

Class Method 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) ⇒ ScheduledPush

Initialize a Scheduled Push Object

Parameters:

  • client (Object)


74
75
76
# File 'lib/urbanairship/push/push.rb', line 74

def initialize(client)
  @client = client
end

Instance Attribute Details

#name=(value) ⇒ Object (writeonly)

Sets the attribute name

Parameters:

  • value

    the value to set the attribute name to.



66
67
68
# File 'lib/urbanairship/push/push.rb', line 66

def name=(value)
  @name = value
end

#pushObject

Returns the value of attribute push.



67
68
69
# File 'lib/urbanairship/push/push.rb', line 67

def push
  @push
end

#schedule=(value) ⇒ Object (writeonly)

Sets the attribute schedule

Parameters:

  • value

    the value to set the attribute schedule to.



66
67
68
# File 'lib/urbanairship/push/push.rb', line 66

def schedule=(value)
  @schedule = value
end

#urlObject

Returns the value of attribute url.



67
68
69
# File 'lib/urbanairship/push/push.rb', line 67

def url
  @url
end

Class Method Details

.from_url(client: required('client'), url: required('url')) ⇒ Object

Build a Scheduled Push Notification object from its existing Scheduled Push URL

Parameters:

  • client (Object) (defaults to: required('client'))

    The Client

  • url (Object) (defaults to: required('url'))

    The existing Scheduled Push URL

Returns:

  • (Object)

    Scheduled Push Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/urbanairship/push/push.rb', line 110

def self.from_url(client: required('client'), url: required('url'))
  scheduled_push = ScheduledPush.new(client)
  response_payload = client.send_request(
    method: 'GET',
    body: nil,
    url: url
  )
  payload = JSON.load(response_payload.to_json)

  p = Push.new(client)
  p.audience = payload['body']['push']['audience']
  p.notification = payload['body']['push']['notification']
  p.device_types = payload['body']['push']['device_types']
  p.message = payload['body']['push']['message']
  p.options = payload['body']['push']['options']

  scheduled_push.name = payload['body']['name']
  scheduled_push.schedule = payload['body']['schedule']
  scheduled_push.push = p
  scheduled_push.url = url
  scheduled_push
end

Instance Method Details

#cancelObject

Cancel the Scheduled Push

Returns:

  • (Object)

    Push Response



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/urbanairship/push/push.rb', line 136

def cancel
  fail ArgumentError,
     'Cannot cancel ScheduledPush without a url.' if @url.nil?

  response = @client.send_request(
    method: 'DELETE',
    body: nil,
    url: @url,
    content_type: 'application/json'
  )
  pr = PushResponse.new(http_response_body: response['body'], http_response_code: response['code'].to_s)
  logger.info { "Result of canceling scheduled push: #{@url} was a: [#{pr.status_code}]" }
  pr
end

#list(schedule_id: required('schedule_id')) ⇒ Object



168
169
170
171
172
173
174
175
176
177
# File 'lib/urbanairship/push/push.rb', line 168

def list(schedule_id: required('schedule_id'))
  fail ArgumentError,
     'schedule_id must be a string' unless schedule_id.is_a? String
  resp = @client.send_request(
    method: 'GET',
    path: schedules_path(schedule_id)
  )
  logger.info("Retrieved info for schedule_id #{schedule_id}")
  resp
end

#payloadObject



78
79
80
81
82
83
84
# File 'lib/urbanairship/push/push.rb', line 78

def payload
  compact_helper({
    name: @name,
    schedule: @schedule,
    push: @push.payload
  })
end

#send_pushPushResponse

Schedule the Push Notification

Returns:

  • (PushResponse)

    with ‘schedule_url` and other response data.

Raises:



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/urbanairship/push/push.rb', line 92

def send_push
  response = @client.send_request(
    method: 'POST',
    body: JSON.dump(payload),
    path: schedules_path,
    content_type: 'application/json'
  )
  pr = PushResponse.new(http_response_body: response['body'], http_response_code: response['code'].to_s)
  logger.info { pr.format }
  @url = pr.schedule_url
  pr
end

#updateObject

Update the Scheduled Push

Returns:

  • (Object)


154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/urbanairship/push/push.rb', line 154

def update
  fail ArgumentError,
     'Cannot update a ScheduledPush without a url.' if @url.nil?
  response = @client.send_request(
    method: 'PUT',
    body: JSON.dump(self.payload),
    url: @url,
    content_type: 'application/json'
  )
  pr = PushResponse.new(http_response_body: response['body'], http_response_code: response['code'].to_s)
  logger.info { pr.format }
  pr
end