class Hubspot::Connection

Public Class Methods

delete_json(path, opts) click to toggle source
# File lib/hubspot/connection.rb, line 51
def delete_json(path, opts)
  url = generate_url(path, opts)
  response = delete(url, format: :json, read_timeout: read_timeout(opts), open_timeout: open_timeout(opts))
  log_request_and_response url, response, opts[:body]
  raise(Hubspot::RequestError.new(response)) unless response.success?
  response
end
get_json(path, opts) click to toggle source
# File lib/hubspot/connection.rb, line 6
def get_json(path, opts)
  url = generate_url(path, opts)
  response = get(url, format: :json, read_timeout: read_timeout(opts), open_timeout: open_timeout(opts))
  log_request_and_response url, response
  handle_response(response)
end
post_json(path, opts) click to toggle source
# File lib/hubspot/connection.rb, line 13
def post_json(path, opts)
  no_parse = opts[:params].delete(:no_parse) { false }

  url = generate_url(path, opts[:params])
  response = post(
    url,
    body: opts[:body].to_json,
    headers: { 'Content-Type' => 'application/json' },
    format: :json,
    read_timeout: read_timeout(opts),
    open_timeout: open_timeout(opts)
  )

  log_request_and_response url, response, opts[:body]
  raise(Hubspot::RequestError.new(response)) unless response.success?

  no_parse ? response : response.parsed_response
end
put_json(path, options) click to toggle source
# File lib/hubspot/connection.rb, line 32
def put_json(path, options)
  no_parse = options[:params].delete(:no_parse) { false }
  url = generate_url(path, options[:params])

  response = put(
    url,
    body: options[:body].to_json,
    headers: { "Content-Type" => "application/json" },
    format: :json,
    read_timeout: read_timeout(options),
    open_timeout: open_timeout(options),
  )

  log_request_and_response(url, response, options[:body])
  raise(Hubspot::RequestError.new(response)) unless response.success?

  no_parse ? response : response.parsed_response
end

Protected Class Methods

converted_value(value) click to toggle source

convert into milliseconds since epoch

# File lib/hubspot/connection.rb, line 118
def converted_value(value)
  value.is_a?(Time) ? (value.to_i * 1000) : CGI.escape(value.to_s)
end
generate_url(path, params={}, options={}) click to toggle source
# File lib/hubspot/connection.rb, line 85
def generate_url(path, params={}, options={})
  if Hubspot::Config.access_token.present?
    options[:hapikey] = false
  else
    Hubspot::Config.ensure! :hapikey
  end
  path = path.clone
  params = params.clone
  base_url = options[:base_url] || Hubspot::Config.base_url
  params["hapikey"] = Hubspot::Config.hapikey unless options[:hapikey] == false

  if path =~ /:portal_id/
    Hubspot::Config.ensure! :portal_id
    params["portal_id"] = Hubspot::Config.portal_id if path =~ /:portal_id/
  end

  params.each do |k,v|
    if path.match(":#{k}")
      path.gsub!(":#{k}", CGI.escape(v.to_s))
      params.delete(k)
    end
  end
  raise(Hubspot::MissingInterpolation.new("Interpolation not resolved")) if path =~ /:/

  query = params.map do |k,v|
    v.is_a?(Array) ? v.map { |value| param_string(k,value) } : param_string(k,v)
  end.join("&")

  path += path.include?('?') ? '&' : "?" if query.present?
  base_url + path + query
end
handle_response(response) click to toggle source
# File lib/hubspot/connection.rb, line 69
def handle_response(response)
  if response.success?
    response.parsed_response
  else
    raise(Hubspot::RequestError.new(response))
  end
end
log_request_and_response(uri, response, body=nil) click to toggle source
# File lib/hubspot/connection.rb, line 77
      def log_request_and_response(uri, response, body=nil)
        Hubspot::Config.logger.info(<<~MSG)
          Hubspot: #{uri}.
          Body: #{body}.
          Response: #{response.code} #{response.body}
        MSG
      end
open_timeout(opts = {}) click to toggle source
# File lib/hubspot/connection.rb, line 65
def open_timeout(opts = {})
  opts.delete(:open_timeout) || Hubspot::Config.open_timeout
end
param_string(key,value) click to toggle source
# File lib/hubspot/connection.rb, line 122
def param_string(key,value)
  case key
  when /range/
    raise "Value must be a range" unless value.is_a?(Range)
    "#{key}=#{converted_value(value.begin)}&#{key}=#{converted_value(value.end)}"
  when /^batch_(.*)$/
    key = $1.gsub(/(_.)/) { |w| w.last.upcase }
    "#{key}=#{converted_value(value)}"
  else
    "#{key}=#{converted_value(value)}"
  end
end
read_timeout(opts = {}) click to toggle source
# File lib/hubspot/connection.rb, line 61
def read_timeout(opts = {})
  opts.delete(:read_timeout) || Hubspot::Config.read_timeout
end