class Freshsales::Client

Public Class Methods

new(config) click to toggle source
# File lib/freshsales/client.rb, line 5
def initialize(config)
  @config = config
end

Public Instance Methods

httprequest(method_name, path, args = {}) click to toggle source
# File lib/freshsales/client.rb, line 9
def httprequest(method_name, path, args = {})
  parse_response(freshsales_request(method_name, path, args))
end
logger() click to toggle source
# File lib/freshsales/client.rb, line 13
def logger
  @config.logger
end

Private Instance Methods

connection() click to toggle source
# File lib/freshsales/client.rb, line 84
def connection
  @connection ||=
    begin
      Faraday.new(freshsales_domain, proxy: @config.proxy, ssl: { version: "TLSv1_2" }) do |c|
        # c.request  :url_encoded
        c.response :raise_error
        c.use Faraday::Request::Authorization, 'Token', "token=#{@config.freshsales_apikey}"
        if @config.debug
          c.response :logger, @config.logger, bodies: true do |logger|
            logger.filter(/(Token token=)(\w+)/, '\1[HIDDEN]')
          end
        end
        c.adapter @config.faraday_adapter
      end
    end
end
freshsales_domain() click to toggle source
# File lib/freshsales/client.rb, line 41
def freshsales_domain
  "https://#{@config.freshsales_domain}.freshsales.io"
end
freshsales_request(method, path, params: nil, headers: nil, body: nil) click to toggle source
# File lib/freshsales/client.rb, line 45
def freshsales_request(method, path, params: nil, headers: nil, body: nil)
  connection.send(method) do |request|
    request.headers['Content-Type'] = 'application/json'
    request.headers.update(headers) if headers
    request.params.update(params) if params
    if body
      body = MultiJson.dump(body) unless body.is_a? String
      request.body = body
    end
    request.url path
  end
rescue StandardError => e
  handle_request_error(e)
end
handle_request_error(error) click to toggle source
# File lib/freshsales/client.rb, line 60
def handle_request_error(error)
  error_params = {}
  if error.is_a?(Faraday::ClientError) && error.response
    error_params[:status_code] = error.response[:status]
    error_params[:raw_body] = error.response[:body]

    parsed_response = jsonify_body(error.response[:body], ignore_parsing_errors: true)

    if parsed_response
      error_params[:body] = parsed_response

      code_key = @config.symbolize_keys ? :code : "code"
      message_key = @config.symbolize_keys ? :message : "message"

      error_params[:code] = parsed_response[code_key] if parsed_response[code_key]
      error_params[:detail] = parsed_response[message_key] if parsed_response[message_key]
    end
  end
  message = error_params[:detail] || error.message
  error_to_raise = FreshsalesError.new(message, error_params)

  raise error_to_raise
end
jsonify_body(body, ignore_parsing_errors: false) click to toggle source
# File lib/freshsales/client.rb, line 32
def jsonify_body(body, ignore_parsing_errors: false)
  MultiJson.load(body, symbolize_keys: @config.symbolize_keys)
rescue MultiJson::ParseError => e
  return if ignore_parsing_errors
  error_params = { detail: e.message, status_code: 500, raw_body: body }
  error = FreshsalesError.new("Unparseable response", error_params)
  raise error
end
parse_response(response) click to toggle source
# File lib/freshsales/client.rb, line 19
def parse_response(response)
  parsed_response = nil

  if response.body && !response.body.empty?
    headers = response.headers
    body = response.body
    body = jsonify_body(body) unless @config.raw_data
    parsed_response = Response.new(headers: headers, body: body)
  end

  parsed_response
end