Class: Surveymonkey::API

Inherits:
Object
  • Object
show all
Defined in:
lib/surveymonkey/api.rb

Overview

Object representing the SurveyMonkey API.

Defined Under Namespace

Classes: Method

Constant Summary collapse

Api_version =

String indicated the version of the SurveyMonkey API implemented.

'v2'
Api_methods =

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

{
  'create_flow' => {
    'path' => '/v2/batch/create_flow',
  },
  'send_flow' => {
    'path' => '/v2/batch/send_flow',
  },
  'create_collector' => {
    'path' => '/v2/collectors/create_collector',
  },
  'get_survey_list' => {
    'path' => '/v2/surveys/get_survey_list',
  },
  'get_survey_details' => {
    'path' => '/v2/surveys/get_survey_details',
  },
  'get_collector_list' => {
    'path' => '/v2/surveys/get_collector_list',
  },
  'get_respondent_list' => {
    'path' => '/v2/surveys/get_respondent_list',
  },
  'get_responses' => {
    'path' => '/v2/surveys/get_responses',
  },
  'get_response_counts' => {
    'path' => '/v2/surveys/get_response_counts',
  },
  'get_template_list' => {
    'path' => '/v2/templates/get_template_list',
  },
  'get_user_details' => {
    'path' => '/v2/user/get_user_details',
  },
  'create_recipients' => {
    'path' => '/v2/collectors/create_recipients',
  },
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAPI

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?)



115
116
117
118
119
120
121
122
123
# 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

Instance Attribute Details

#api_methodsObject (readonly)

public methods



60
61
62
# File 'lib/surveymonkey/api.rb', line 60

def api_methods
  @api_methods
end

#api_versionObject (readonly)

public methods



60
61
62
# File 'lib/surveymonkey/api.rb', line 60

def api_version
  @api_version
end

Instance Method Details

#api_method(key, api_methods = self.api_methods) ⇒ Object

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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) ⇒ Object

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.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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_sObject

Stringify a Surveymonkey::API object.



128
129
130
# File 'lib/surveymonkey/api.rb', line 128

def to_s
  self.api_version
end