Module: SparkApi::Connection

Included in:
Client
Defined in:
lib/spark_api/connection.rb

Overview

Connection

Mixin module for handling http connection information

Constant Summary collapse

REG_HTTP =
/^http:/
REG_HTTPS =
/^https:/
HTTP_SCHEME =
'http:'
HTTPS_SCHEME =
'https:'
ACCEPT_ENCODING =
'Accept-Encoding'
X_REQUEST_ID_CHAIN =
'X-Request-Id-Chain'
MIME_JSON =
'application/json'
MIME_RESO =
'application/json, application/xml'

Instance Method Summary collapse

Instance Method Details

#connection(force_ssl = false) ⇒ Object

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/spark_api/connection.rb', line 18

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 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
  if self.verbose
    SparkApi.logger.debug { "Connection: #{conn.inspect}" }
  end
  conn
end

#headersObject

HTTP request headers for client requests



45
46
47
48
49
50
51
# File 'lib/spark_api/connection.rb', line 45

def headers
  if self.middleware.to_sym == :reso_api
    reso_headers
  else
    spark_headers
  end
end

#reso_headersObject



62
63
64
65
66
67
68
# File 'lib/spark_api/connection.rb', line 62

def reso_headers
  {
    :accept => MIME_RESO,
    :user_agent => Configuration::DEFAULT_USER_AGENT,
    Configuration::X_SPARK_API_USER_AGENT => user_agent
  }
end

#spark_headersObject



53
54
55
56
57
58
59
60
# File 'lib/spark_api/connection.rb', line 53

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