Sha256: 518ec194aa72488de0b9eb98819e11741558ad42231fab92275f737e4059e3f2

Contents?: true

Size: 1.41 KB

Versions: 7

Compression:

Stored size: 1.41 KB

Contents

require 'PushRadar/version'
require 'PushRadar/Targeting'
require 'net/http'
require 'json'
require 'openssl'

module PushRadar

  # A client that interacts with PushRadar's API
  class APIClient

    # Creates a new instance of the PushRadar API client with the specified API secret
    def initialize(api_secret)

      # Set the API secret
      api_secret.strip!
      @api_secret = api_secret

      # Set the API endpoint
      @api_endpoint = 'https://api.pushradar.com/v2'

    end

    # Performs a POST request to the API destination specified
    def post(destination, data)

      # Add in the fields that should be sent with each request
      data['apiSecret'] = @api_secret
      data['clientPlatform'] = 'ruby'
      data['timestamp'] = Time.now.to_i

      # Make sure the destination does not contain the base URL or forward slash characters
      destination = destination.gsub('/', '')
      destination.sub! @api_endpoint, ''

      # Construct the actual URL to POST the data to
      url = @api_endpoint + '/' + destination

      # Send a POST request to the server
      uri = URI.parse(url)
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      req = Net::HTTP::Post.new(url)
      req.content_type = 'application/x-www-form-urlencoded'
      data = URI.encode_www_form(data)
      req.body = data
      http.request(req)

    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pushradar-2.0.4 lib/PushRadar/APIClient.rb
pushradar-2.0.3 lib/PushRadar/APIClient.rb
pushradar-2.0.2 lib/PushRadar/APIClient.rb
pushradar-2.0.0 lib/PushRadar/APIClient.rb
pushradar-1.8.2 lib/PushRadar/APIClient.rb
pushradar-1.8.1 lib/PushRadar/APIClient.rb
pushradar-1.8.0 lib/PushRadar/APIClient.rb