Class: Librato::Metrics::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/librato/metrics/connection.rb

Constant Summary collapse

DEFAULT_API_ENDPOINT =
'https://metrics-api.librato.com'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



17
18
19
20
# File 'lib/librato/metrics/connection.rb', line 17

def initialize(options={})
  @client = options[:client]
  @api_endpoint = options[:api_endpoint]
end

Instance Method Details

#api_endpointObject

API endpoint that will be used for requests.



24
25
26
# File 'lib/librato/metrics/connection.rb', line 24

def api_endpoint
  @api_endpoint || DEFAULT_API_ENDPOINT
end

#transportObject

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/librato/metrics/connection.rb', line 28

def transport
  raise NoClientProvided unless @client
  @transport ||= Faraday::Connection.new(:url => api_endpoint + "/v1/") do |f|
    #f.use FaradayMiddleware::EncodeJson
    f.use Librato::Metrics::Middleware::Retry
    f.use Librato::Metrics::Middleware::CountRequests
    f.use Librato::Metrics::Middleware::ExpectsStatus
    #f.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
    f.adapter Faraday.default_adapter
  end.tap do |transport|
    transport.headers[:user_agent] = user_agent
    transport.headers[:content_type] = 'application/json'
    transport.basic_auth @client.email, @client.api_key
  end
end

#user_agentObject

User-agent used when making requests.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/librato/metrics/connection.rb', line 46

def user_agent
  ua_chunks = []
  agent_identifier = @client.agent_identifier
  if agent_identifier && !agent_identifier.empty?
    ua_chunks << agent_identifier
  end
  ua_chunks << "librato-metrics/#{Metrics::VERSION}"
  ua_chunks << "(#{ruby_engine}; #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}; #{RUBY_PLATFORM})"
  ua_chunks << "direct-faraday/#{Faraday::VERSION}"
  # TODO: include adapter information
  #ua_chunks << "(#{transport_adapter})"
  ua_chunks.join(' ')
end