Sha256: 3950312c1bddcbabcce63d6d017e1a3c79e8a9da67224a4864e24cfede100330

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

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

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/v1'

    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['api_secret'] = @api_secret
      data['client_platform'] = 'ruby'
      data['request_sent_at'] = 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(url)
      req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
      req.body = data.to_json
      Net::HTTP.start(uri.hostname, uri.port) do |http|
        http.request(req)
      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pushradar-0.9.0 lib/PushRadar/APIClient.rb