Method: SimpleSpark::Client#call

Defined in:
lib/simple_spark/client.rb

#call(opts) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/simple_spark/client.rb', line 34

def call(opts)
  method = opts[:method]
  path = opts[:path]
  body_values = opts[:body_values] || {}
  query_params = opts[:query_values] || {}
  extract_results = opts[:extract_results].nil? ? true : opts[:extract_results]

  fail Exceptions::InvalidConfiguration.new(method: method), 'Only GET, POST, PUT and DELETE are supported' unless [:get, :post, :put, :delete].include?(method)

  path = "#{@base_path}#{path}"
  params = { path: path, headers: headers }
  params[:body] = JSON.generate(body_values) unless body_values.empty?
  params[:query] = query_params unless query_params.empty?

  if @debug
    logger.debug("Calling #{method}")
    logger.debug(params)
  end

  response = @session.send(method.to_s, params)

  if @debug
    logger.debug("Response #{response.status}")
    logger.debug(response)
  end

  fail Exceptions::GatewayTimeoutExceeded, 'Received 504 from SparkPost API' if response.status == 504

  process_response(response, extract_results)

rescue Excon::Errors::Timeout
  raise Exceptions::GatewayTimeoutExceeded
end