class Iterable::Services::BaseService

Attributes

api_key[RW]

Protected Class Methods

build_url(url, params = nil) click to toggle source

Build a url from the base url and query parameters hash. Query parameters should not be URL encoded because this method will handle that @param [String] url - The base url @param [Hash] params - A hash with query parameters @return [String] - the url with query parameters hash

# File lib/iterable/services/base_service.rb, line 55
def build_url(url, params = nil)
  if params.respond_to? :each
    params.each do |key, value|
      # Convert dates to CC date format
      if value.respond_to? :iso8601
        params[key] = value.iso8601
      end

      if key.to_s == 'next' && value.match(/^.*?next=(.*)$/)
        params[key] = $1
      end
    end
  else
    params ||= {}
  end
  url + '?' + Util::Helpers.http_build_query(params)
end
get(path, params = {}, response_type = Iterable::Responses::General) click to toggle source
# File lib/iterable/services/base_service.rb, line 15
def get(path, params = {}, response_type = Iterable::Responses::General)
  url = URI::join(Util::Config.get('endpoints.base_url'), path).to_s
  url = build_url(url, params)
  response = RestClient.get(url, get_headers())
  begin
    response_type.new JSON.parse(response.body)
  rescue
    Hashie::Mash.new JSON.parse(response.body)
  end
end
get_headers(content_type = 'application/json') click to toggle source

Return required headers for making an http request with Iterable @param [String] content_type - The MIME type of the body of the request, default is 'application/json' @return [Hash] - authorization headers

# File lib/iterable/services/base_service.rb, line 40
def get_headers(content_type = 'application/json')
  {
    :content_type           => content_type,
    :accept                 => 'application/json',
    :user_agent             => "Iterable Ruby SDK v#{Iterable::VERSION} (#{RUBY_DESCRIPTION})",
    :x_ctct_request_source  => "sdk.ruby.#{Iterable::VERSION}",
    'Api-Key'               => BaseService.api_key
  }
end
post(path, body = {}, params = {}, response_type = Iterable::Responses::General) click to toggle source
# File lib/iterable/services/base_service.rb, line 26
def post(path, body = {}, params = {}, response_type = Iterable::Responses::General)
  url = URI::join(Util::Config.get('endpoints.base_url'), path).to_s
  url = build_url(url, params)
  response = RestClient.post(url, body.to_json, get_headers())
  begin
    response_type.new JSON.parse(response.body)
  rescue
    Hashie::Mash.new JSON.parse(response.body)
  end
end