class Pusher::PushNotifications::Client

Constants

Response

Attributes

config[R]

Public Class Methods

new(config: PushNotifications) click to toggle source
# File lib/pusher/push_notifications/client.rb, line 14
def initialize(config: PushNotifications)
  @config = config
end

Public Instance Methods

delete(user) click to toggle source
# File lib/pusher/push_notifications/client.rb, line 36
def delete(user)
  url_encoded_user_id = CGI.escape(user)
  url = build_users_url(url_encoded_user_id)

  RestClient::Request.execute(
    method: :delete, url: url,
    headers: headers
  ) do |response|
    status = response.code
    case status
    when 200
      Response.new(status, nil, true)
    else
      Response.new(status, nil, false)
    end
  end
end
post(resource, payload = {}) click to toggle source
# File lib/pusher/push_notifications/client.rb, line 18
def post(resource, payload = {})
  url = build_publish_url(resource)
  body = payload.to_json

  RestClient::Request.execute(
    method: :post, url: url,
    payload: body, headers: headers
  ) do |response|
    status = response.code
    if json?(response.body)
      body = JSON.parse(response.body)
      Response.new(status, body, status == 200)
    else
      Response.new(status, nil, false)
    end
  end
end

Private Instance Methods

build_publish_url(resource) click to toggle source
# File lib/pusher/push_notifications/client.rb, line 60
def build_publish_url(resource)
  "#{endpoint}/publish_api/v1/instances/#{instance_id}/#{resource}"
end
build_users_url(user) click to toggle source
# File lib/pusher/push_notifications/client.rb, line 64
def build_users_url(user)
  "#{endpoint}/customer_api/v1/instances/#{instance_id}/users/#{user}"
end
headers() click to toggle source
# File lib/pusher/push_notifications/client.rb, line 68
def headers
  {
    content_type: 'application/json',
    accept: :json,
    Authorization: "Bearer #{secret_key}",
    'X-Pusher-Library':
    'pusher-push-notifications-ruby ' \
    "#{Pusher::PushNotifications::VERSION}"
  }
end
json?(response) click to toggle source
# File lib/pusher/push_notifications/client.rb, line 79
def json?(response)
  JSON.parse(response)
  true
rescue JSON::ParserError
  false
end