module SparkApi::Connection

Connection

Mixin module for handling http connection information

Constants

ACCEPT_ENCODING
COMPRESS_ACCEPT_ENCODING
HTTPS_SCHEME
HTTP_SCHEME
MIME_JSON
MIME_RESO
REG_HTTP
REG_HTTPS
X_REQUEST_ID_CHAIN

Public Instance Methods

connection(force_ssl = false) click to toggle source

Main connection object for running requests. Bootstraps the Faraday abstraction layer with our client configuration.

# File lib/spark_api/connection.rb, line 19
def connection(force_ssl = false)
  opts = {
    :headers => headers
  }
  if(force_ssl || self.ssl)
    opts[:ssl] = {:verify => false } unless self.ssl_verify
    opts[:url] = @endpoint.sub REG_HTTP, HTTPS_SCHEME
  else 
    opts[:url] = @endpoint.sub REG_HTTPS, HTTP_SCHEME
  end

  if self.compress
    opts[:headers][ACCEPT_ENCODING] = COMPRESS_ACCEPT_ENCODING
  end

  if request_id_chain
    opts[:headers][X_REQUEST_ID_CHAIN] = request_id_chain
  end

  conn = Faraday.new(opts) do |conn|
    conn.response self.middleware.to_sym
    conn.options[:timeout] = self.timeout
    conn.adapter Faraday.default_adapter
  end
  SparkApi.logger.debug { "Connection: #{conn.inspect}" }
  conn
end
headers() click to toggle source

HTTP request headers for client requests

# File lib/spark_api/connection.rb, line 48
def headers
  if self.middleware.to_sym == :reso_api
    reso_headers
  else
    spark_headers
  end
end
reso_headers() click to toggle source
# File lib/spark_api/connection.rb, line 65
def reso_headers
  {
    :accept => MIME_RESO,
    :user_agent => Configuration::DEFAULT_USER_AGENT,
    Configuration::X_SPARK_API_USER_AGENT => user_agent
  }
end
spark_headers() click to toggle source
# File lib/spark_api/connection.rb, line 56
def spark_headers
  {
    :accept => MIME_JSON,
    :content_type => MIME_JSON,
    :user_agent => Configuration::DEFAULT_USER_AGENT,
    Configuration::X_SPARK_API_USER_AGENT => user_agent
  }
end