module PusherNotification

Constants

INTERESTS_MAX_LENGTH
VERSION

Attributes

config[RW]

Public Class Methods

publish(payload = {}) click to toggle source
# File lib/pusher_notifications_ruby.rb, line 12
def publish(payload = {})
  validate_payload(payload)

  Faraday.post(
    base_url + path,
    payload.to_json,
    {
      'Content-Type'  => 'application/json',
      'Authorization' => "Bearer #{config[:secret_key]}"
    }
  )
end

Private Class Methods

base_url() click to toggle source
# File lib/pusher_notifications_ruby.rb, line 41
def base_url
  "https://#{config[:instance_id]}.pushnotifications.pusher.com"
end
path() click to toggle source
# File lib/pusher_notifications_ruby.rb, line 45
def path
  "/publish_api/v1/instances/#{config[:instance_id]}/publishes"
end
validate_payload(payload = {}) click to toggle source
# File lib/pusher_notifications_ruby.rb, line 27
def validate_payload(payload = {})
  unless config.key?(:instance_id) && config.key?(:secret_key)
    raise 'Config must be provided before publishing'
  end

  unless payload.key?(:interests)
    raise 'Interests is a required parameter.'
  end

  if payload[:interests].any? {|interest| interest.length > INTERESTS_MAX_LENGTH }
    raise "Interests can not be longer than the maximum of #{INTERESTS_MAX_LENGTH}."
  end
end