Sha256: f5ac69866675aafa902369caf8ea9a063dc0b4a19c088df9b28886ffee8c5541

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 KB

Contents

require "httparty"
require "json"

require "surveymonkey/logging"

##
# Class encapsulating the HTTParty client used to communicate with the SurveyMonkey API.

class Surveymonkey::Client
  include HTTParty

  $log.debug("Defined Surveymonkey::Client.")

  # constants

  # public methods
  attr_reader :baseuri, :api_key, :access_token

  ##
  # Create a new Surveymonkey::Client object.  Requires the following parameters:
  # * baseuri
  # * access_token
  # * api_key

  def initialize(baseuri, access_token, api_key)
    begin
      @baseuri      = baseuri
      @access_token = access_token
      @api_key      = api_key

      self.class.logger $log, :debug

      $log.debug(sprintf("%s: setting base_uri to '%s'\n", __method__, @baseuri))
      self.class.base_uri @baseuri

      $log.debug(sprintf("%s: setting headers'\n", __method__))
      http_headers = _http_headers(@access_token)
      self.class.headers http_headers

    rescue StandardError => e
      $log.error(sprintf("%s: %s\n", __method__, e.message))
      raise
    end
  end

  ##
  # Stringify a Surveymonkey::Client object

  def to_s
    self.baseuri
  end

  # private methods
  private

  def _http_headers(token) #:nodoc:
    begin
      $log.debug(sprintf("%s: constructing http headers with token '%s'\n", __method__, token))
      http_headers = {
        "Content-Type" => "application/json",
        "Authorization" => sprintf("bearer %s", token),
      }
      $log.debug(sprintf("%s: http headers: '%s'\n", __method__, http_headers))
      http_headers

    rescue StandardError => e
      $log.error(sprintf("%s: %s\n", __method__, e.message))
      raise
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
surveymonkey-0.4.0 lib/surveymonkey/client.rb
surveymonkey-0.3.0 lib/surveymonkey/client.rb
surveymonkey-0.2.2 lib/surveymonkey/client.rb