class PushRadar::Client

Public Class Methods

new(secret_key) click to toggle source
# File lib/pushradar/client.rb, line 7
def initialize(secret_key)
  unless secret_key.is_a?(String)
    raise PushRadar::Error, 'Secret key must be a string.'
  end

  unless secret_key.start_with?('sk_')
    raise PushRadar::Error, 'Please provide your PushRadar secret key. You can find it on the API page of your dashboard.'
  end

  @secret_key = secret_key
  @api_endpoint = 'https://api.pushradar.com/v3'
end

Public Instance Methods

auth(channel_name, socket_id) click to toggle source
# File lib/pushradar/client.rb, line 47
def auth(channel_name, socket_id)
  unless channel_name.is_a?(String)
    raise PushRadar::Error, 'Channel name must be a string.'
  end

  if channel_name.nil? || channel_name.strip.empty?
    raise PushRadar::Error, 'Channel name empty. Please provide a channel name.'
  end

  unless channel_name.start_with?('private-') || channel_name.start_with?('presence-')
    raise PushRadar::Error, 'Channel authentication can only be used with private and presence channels.'
  end

  unless socket_id.is_a?(String)
    raise PushRadar::Error, 'Socket ID must be a string.'
  end

  if socket_id.nil? || socket_id.strip.empty?
    raise PushRadar::Error, 'Socket ID empty. Please pass through a socket ID.'
  end

  response = do_http_request('GET', @api_endpoint + "/channels/auth?channel=" + CGI.escape(channel_name) + "&socketID=" + CGI.escape(socket_id), {})
  if response[:status] === 200
    JSON(response[:body])['token']
  else
    raise PushRadar::Error, 'There was a problem receiving a channel authentication token. Server returned: ' + response[:body]
  end
end
broadcast(channel_name, data) click to toggle source
# File lib/pushradar/client.rb, line 28
def broadcast(channel_name, data)
  unless channel_name.is_a?(String)
    raise PushRadar::Error, 'Channel name must be a string.'
  end

  if channel_name.nil? || channel_name.strip.empty?
    raise PushRadar::Error, 'Channel name empty. Please provide a channel name.'
  end

  validate_channel_name(channel_name)
  response = do_http_request('POST', @api_endpoint + "/broadcasts", { channel: channel_name, data: data.to_json })

  if response[:status] === 200
    true
  else
    raise PushRadar::Error, 'An error occurred while calling the API. Server returned: ' + response[:body]
  end
end
register_client_data(socket_id, client_data) click to toggle source
# File lib/pushradar/client.rb, line 76
def register_client_data(socket_id, client_data)
  unless socket_id.is_a?(String)
    raise PushRadar::Error, 'Socket ID must be a string.'
  end

  if socket_id.nil? || socket_id.strip.empty?
    raise PushRadar::Error, 'Socket ID empty. Please pass through a socket ID.'
  end

  response = do_http_request('POST', @api_endpoint + "/client-data", { socketID: socket_id, clientData: client_data.to_json })

  if response[:status] === 200
    true
  else
    raise PushRadar::Error, 'An error occurred while calling the API. Server returned: ' + response[:body]
  end
end

Private Instance Methods

do_http_request(method, url, data) click to toggle source
# File lib/pushradar/client.rb, line 94
def do_http_request(method, url, data)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  if method.downcase === 'post'
    req = Net::HTTP::Post.new(url)
    req['X-PushRadar-Library'] = 'pushradar-server-ruby ' + VERSION
    req['Authorization'] = "Bearer " + @secret_key
    req.content_type = 'application/json'
    data = data.to_json
    req.body = data
  else
    req = Net::HTTP::Get.new(url)
    req['X-PushRadar-Library'] = 'pushradar-server-ruby ' + VERSION
    req['Authorization'] = "Bearer " + @secret_key
    req.content_type = 'application/json'
  end

  response = http.request(req)
  { body: response.body, status: response.code.to_i }
end
validate_channel_name(channel_name) click to toggle source
# File lib/pushradar/client.rb, line 20
def validate_channel_name(channel_name)
  if /[^A-Za-z0-9_\-=@,.;]/.match(channel_name)
    raise PushRadar::Error, "Invalid channel name: #{channel_name}. Channel names cannot contain spaces, and must consist of only " +
      "upper and lowercase letters, numbers, underscores, equals characters, @ characters, commas, periods, semicolons, " +
      "and hyphens (A-Za-z0-9_=@,.;-)."
  end
end