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_methods =

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

{
  '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',
  },
}

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



107
108
109
110
111
112
113
114
# File 'lib/surveymonkey/api.rb', line 107

def initialize
  begin
    @api_methods = Api_methods
  rescue StandardError => e
    $log.error(sprintf("%s: %s\n", __method__, e.message))
    raise
  end
end

Instance Attribute Details

#api_methodsObject (readonly)

public methods



52
53
54
# File 'lib/surveymonkey/api.rb', line 52

def api_methods
  @api_methods
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.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/surveymonkey/api.rb', line 57

def api_method(key, api_methods = self.api_methods)
  begin
    $log.debug(sprintf("%s: api methods: %s\n", __method__, api_methods.inspect))
    $log.debug(sprintf("%s: fetching '%s' from API methods\n", __method__, key))
    value = api_methods.fetch(key)
    $log.debug(sprintf("%s: retrieved '%s'\n", __method__, value.inspect))

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

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

  rescue KeyError => e
    $log.error(sprintf("%s: '%s' not found in api methods\n", __method__, key))
    raise e
  rescue StandardError => e
    $log.error(sprintf("%s: %s\n", __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.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/surveymonkey/api.rb', line 86

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

    # return
    the_params

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