class Rpush::Daemon::Pushy::Delivery

Attributes

batch[R]
http[R]
notification[R]
pushy_uri[R]

Public Class Methods

new(app, http, notification, batch) click to toggle source
# File lib/rpush/daemon/pushy/delivery.rb, line 9
def initialize(app, http, notification, batch)
  @http = http
  @notification = notification
  @batch = batch
  @pushy_uri = URI.parse("https://api.pushy.me/push?api_key=#{app.api_key}")
end

Public Instance Methods

perform() click to toggle source
# File lib/rpush/daemon/pushy/delivery.rb, line 16
def perform
  response = send_request
  process_response(response)
rescue SocketError => error
  mark_retryable(notification, Time.now + 10.seconds, error)
  raise
rescue StandardError => error
  mark_failed(error)
  raise
ensure
  batch.notification_processed
end

Private Instance Methods

deliver_after_header(response) click to toggle source
# File lib/rpush/daemon/pushy/delivery.rb, line 68
def deliver_after_header(response)
  Rpush::Daemon::RetryHeaderParser.parse(response.header['retry-after'])
end
fail_delivery(response) click to toggle source
# File lib/rpush/daemon/pushy/delivery.rb, line 77
def fail_delivery(response)
  fail_message = fail_message(response)
  log_error("#{notification.id} failed: #{fail_message}")
  fail Rpush::DeliveryError.new(response.code.to_i, notification.id, fail_message)
end
fail_message(response) click to toggle source
# File lib/rpush/daemon/pushy/delivery.rb, line 83
def fail_message(response)
  body = multi_json_load(response.body)
  body['error'] || Rpush::Daemon::HTTP_STATUS_CODES[response.code.to_i]
end
process_delivery(response) click to toggle source
# File lib/rpush/daemon/pushy/delivery.rb, line 49
def process_delivery(response)
  mark_delivered
  body = multi_json_load(response.body)
  external_device_id = body['id']
  notification.external_device_id = external_device_id
  Rpush::Daemon.store.update_notification(notification)
  log_info("#{notification.id} received an external id=#{external_device_id}")
end
process_response(response) click to toggle source
# File lib/rpush/daemon/pushy/delivery.rb, line 38
def process_response(response)
  case response.code.to_i
  when 200
    process_delivery(response)
  when 429, 500, 502, 503, 504
    retry_delivery(response)
  else
    fail_delivery(response)
  end
end
retry_delivery(response) click to toggle source
# File lib/rpush/daemon/pushy/delivery.rb, line 58
def retry_delivery(response)
  time = deliver_after_header(response)
  if time
    mark_retryable(notification, time)
  else
    mark_retryable_exponential(notification)
  end
  log_warn("Pushy responded with a #{response.code} error. #{retry_message}")
end
retry_message() click to toggle source
# File lib/rpush/daemon/pushy/delivery.rb, line 72
def retry_message
  deliver_after = notification.deliver_after.strftime('%Y-%m-%d %H:%M:%S')
  "Notification #{notification.id} will be retried after #{deliver_after} (retry #{notification.retries})."
end
send_request() click to toggle source
# File lib/rpush/daemon/pushy/delivery.rb, line 31
def send_request
  post = Net::HTTP::Post.new(pushy_uri)
  post.content_type = 'application/json'
  post.body = notification.to_json
  http.request(pushy_uri, post)
end