module Missinglink::Connection

Public Instance Methods

credential_hash=(key_pair) click to toggle source
# File lib/missinglink/connection.rb, line 16
def credential_hash=(key_pair)
  @credential_hash = { api_key: nil, token: nil }.merge(key_pair)
end
credentials_provided?() click to toggle source
# File lib/missinglink/connection.rb, line 20
def credentials_provided?
  !!(@credential_hash && @credential_hash[:api_key] && @credential_hash[:token])
end
request(request_type, body = { }) click to toggle source
# File lib/missinglink/connection.rb, line 7
def request(request_type, body = { })
  (puts "Please provide credentials before making a request." && return) unless credentials_provided?

  JSON.parse(typh_request(request_type,
                          @credential_hash[:api_key],
                          @credential_hash[:token],
                          body.to_json).tap {|x| x.run}.response.body)['data']
end

Private Instance Methods

typh_request(fragment, api_key, token, body = '{ }') click to toggle source
# File lib/missinglink/connection.rb, line 25
def typh_request(fragment, api_key, token, body = '{ }')
  # survey monkey's API limits are obnoxious, this is hacky but an easy way
  # to ensure that we're always under the 2 QPS limit
  sleep 0.5 unless ENV["RAILS_ENV"] == 'test'

  Typhoeus::Request.new(
    "https://api.surveymonkey.net/v2/surveys/#{fragment}",
    method: :post,
    params: { api_key: api_key },
    body: body,
    headers: { Authorization: "bearer #{token}", :"Content-Type" => "application/json" }
  )
end