class Surveymonkey::API

Object representing the SurveyMonkey API.

Constants

Api_methods

Hash defining the methods in the SurveyMonkey API. Current as of 2015-12-14.

Api_version

String indicated the version of the SurveyMonkey API implemented.

Attributes

api_methods[R]

public methods

api_version[R]

public methods

Public Class Methods

new() click to toggle source

Create a new Surveymonkey::API object. The only parameter is an api_methods hash (use this if you want to override the definition of the SurveyMonkey API, I guess?)

# File lib/surveymonkey/api.rb, line 115
def initialize
  begin
    @api_methods = Api_methods
    @api_version = Api_version
  rescue StandardError => e
    $log.error(sprintf("%s: %s", __method__, e.message))
    raise
  end
end

Public Instance Methods

api_method(key, api_methods = self.api_methods) click to toggle source

Look up a SurveyMonkey API method and return its path and associated HTTP method.

# File lib/surveymonkey/api.rb, line 65
def api_method(key, api_methods = self.api_methods)
  begin
    $log.debug(sprintf("%s: api methods: %s", __method__, api_methods.inspect))
    $log.debug(sprintf("%s: fetching '%s' from API methods", __method__, key))
    value = api_methods.fetch(key)
    $log.debug(sprintf("%s: retrieved '%s'", __method__, value.inspect))

    path = value['path']
    $log.debug(sprintf("%s: path '%s'", __method__, path))
    method = (value['method'] || 'post')
    $log.debug(sprintf("%s: method '%s'", __method__, method))

    # return
    Surveymonkey::API::Method.new(path, method, method_name = key)

  rescue KeyError => e
    $log.error(sprintf("%s: '%s' not found in api methods", __method__, key))
    raise e
  rescue StandardError => e
    $log.error(sprintf("%s: %s", __method__, e.message))
    raise
  end
end
api_method_params(method_params) click to toggle source

SurveyMonkey API method params need to be a JSON-encoded string; this method passes through a string and tries to turn another data type into JSON.

# File lib/surveymonkey/api.rb, line 94
def api_method_params(method_params)
  begin
    # TODO validate params against API spec
    $log.debug(sprintf("%s: parsing api method params from '%s'", __method__, method_params))
    the_params = (method_params.kind_of?(String) ? method_params : JSON.generate(method_params || {}))
    $log.debug(sprintf("%s: parsed method params '%s'", __method__, the_params))

    # return
    the_params

  rescue StandardError => e
    $log.error(sprintf("%s: %s", __method__, e.message))
    raise
  end
end
to_s() click to toggle source

Stringify a Surveymonkey::API object.

# File lib/surveymonkey/api.rb, line 128
def to_s
  self.api_version
end