module Mailchimp::Client::Remote

Constants

RETRY_EXCEPTIONS

Public Instance Methods

delete(path) click to toggle source
# File lib/mailchimp_api_v3/client/remote.rb, line 22
def delete(path)
  managed_remote path, :delete
end
get(path = '', options = {}) click to toggle source
# File lib/mailchimp_api_v3/client/remote.rb, line 10
def get(path = '', options = {})
  managed_remote path, :get, options
end
patch(path, data, options = {}) click to toggle source
# File lib/mailchimp_api_v3/client/remote.rb, line 18
def patch(path, data, options = {})
  managed_remote path, :patch, options, data
end
post(path, data, options = {}) click to toggle source
# File lib/mailchimp_api_v3/client/remote.rb, line 14
def post(path, data, options = {})
  managed_remote path, :post, options, data
end

Private Instance Methods

headers() click to toggle source
# File lib/mailchimp_api_v3/client/remote.rb, line 74
def headers
  @headers ||= {
    'Accept' => '*/*; q=0.5, application/xml',
    'Authorization' => "apikey #{@api_key}",
    'User-Agent' => 'Mailchimp API v3 Ruby gem https://rubygems.org/gems/mailchimp_api_v3'
  }.merge @extra_headers
end
managed_remote(path, method = :get, options = {}, payload = nil) click to toggle source
# File lib/mailchimp_api_v3/client/remote.rb, line 30
def managed_remote(path, method = :get, options = {}, payload = nil)
  headers_and_params = headers.merge params_from(options)
  YAML.safe_load(naked_remote("#{url_stub}#{path}", method, headers_and_params, payload) || '')
rescue *RETRY_EXCEPTIONS => e
  @retries ||= 0
  raise e if (@retries += 1) > 3
  retry
rescue => e # TODO: Find out why this doesn't fire if we rescue RestClient::Exception
  managed_remote_exception e
end
managed_remote_exception(e) click to toggle source
# File lib/mailchimp_api_v3/client/remote.rb, line 41
def managed_remote_exception(e)
  data = YAML.safe_load(e.http_body) if e.respond_to? :http_body
  exception_class_name = e.class.to_s

  if Mailchimp::Exception::MAPPED_EXCEPTIONS.key? exception_class_name
    raise Mailchimp::Exception::MAPPED_EXCEPTIONS[exception_class_name], data
  end

  if exception_class_name == Mailchimp::Exception::BAD_REQUEST
    return Mailchimp::Exception.parse_invalid_resource_exception data
  end

  raise e
end
naked_remote(url, method, headers_and_params, payload = nil) click to toggle source
# File lib/mailchimp_api_v3/client/remote.rb, line 56
def naked_remote(url, method, headers_and_params, payload = nil)
  if %i(get delete head options).include? method
    remote_no_payload(url, method, headers_and_params)
  else
    remote_with_payload(url, payload, method, headers_and_params)
  end
end
params_from(options = {}) click to toggle source
# File lib/mailchimp_api_v3/client/remote.rb, line 86
def params_from(options = {})
  options == {} ? {} : { 'params' => options }
end
remote_no_payload(url, method, headers_and_params = {}) click to toggle source
# File lib/mailchimp_api_v3/client/remote.rb, line 64
def remote_no_payload(url, method, headers_and_params = {})
  RestClient.__send__ method, url, headers_and_params
rescue RestClient::NotFound
  nil
end
remote_with_payload(url, payload, method, headers_and_params = {}) click to toggle source
# File lib/mailchimp_api_v3/client/remote.rb, line 70
def remote_with_payload(url, payload, method, headers_and_params = {})
  RestClient.__send__ method, url, payload.to_json, headers_and_params
end
url_stub() click to toggle source
# File lib/mailchimp_api_v3/client/remote.rb, line 82
def url_stub
  @url_stub ||= "https://#{dc}.api.mailchimp.com"
end