class SimpleHubspot::ApiClient

Public Class Methods

do_form_post(guid = nil, params = {}, headers = {}) click to toggle source
# File lib/simple_hubspot/api_client.rb, line 20
def do_form_post(guid = nil, params = {}, headers = {})
  if params[:hs_context]
    params[:hs_context] = params[:hs_context].to_json
  end
  response = RestClient.post "#{SimpleHubspot.configuration.form_submit_base}#{SimpleHubspot.configuration.portal_id}/#{guid}",
                             URI.encode_www_form(params),
                             { content_type: 'application/x-www-form-urlencoded'}
  response_success response.body
rescue RestClient::BadRequest => e
  response_fail e.response.body
rescue RestClient::NotFound => e
  response_fail e.response.body
end
do_get(path = nil, params = {}, headers = {}) click to toggle source
# File lib/simple_hubspot/api_client.rb, line 12
def do_get(path = nil, params = {}, headers = {})
  response = RestClient.get "#{SimpleHubspot.configuration.api_base}#{path}#{add_apikey}"
  json = JSON.parse(response.body, symbolize_names: true)
  json.merge(success: true)
rescue RestClient::BadRequest => e
  response_fail e.response.body
end
do_post(path = nil, params = {}, headers = {}) click to toggle source
# File lib/simple_hubspot/api_client.rb, line 5
def do_post(path = nil, params = {}, headers = {})
  response = RestClient.post "#{SimpleHubspot.configuration.api_base}#{path}#{add_apikey}", params.to_json, { content_type: :json }
  response_success response.body
rescue RestClient::BadRequest => e
  response_fail e.response.body
end

Private Class Methods

add_apikey() click to toggle source
# File lib/simple_hubspot/api_client.rb, line 35
def add_apikey
  "?hapikey=#{SimpleHubspot.configuration.hapikey}"
end
response_fail(response = {}) click to toggle source
# File lib/simple_hubspot/api_client.rb, line 39
def response_fail(response = {})
  return { success: false } if response.empty?
  json = JSON.parse(response, symbolize_names: true)
  json.merge(success: false)
end
response_success(response = {}) click to toggle source
# File lib/simple_hubspot/api_client.rb, line 45
def response_success(response = {})
  return { success: true } if response.empty?
  json = JSON.parse(response, symbolize_names: true)
  json.merge(success: true)
end