class Opentsdb::Faraday

Constants

DEFAULT_TIMEOUT

Attributes

options[R]
url[R]

Public Class Methods

new(url, options = {}) click to toggle source
# File lib/opentsdb/faraday.rb, line 8
def initialize(url, options = {})
  @url = url
  @options = options
end

Public Instance Methods

post(body) click to toggle source
# File lib/opentsdb/faraday.rb, line 13
def post(body)
  connection.post do |req|
    req.headers         = headers
    req.body            = body
    req.options.timeout = options[:timeout] || DEFAULT_TIMEOUT
    req.options.open_timeout = options[:open_timeout] || DEFAULT_TIMEOUT
  end
end

Private Instance Methods

auto_detect_adapter() click to toggle source
# File lib/opentsdb/faraday.rb, line 38
def auto_detect_adapter
  if defined?(::Patron)
    :partron
  elsif defined?(::Excon) && defined?(::Excon::VERSION)
    :excon
  elsif defined?(::Typhoeus)
    :typhoeus
  elsif defined?(::HTTPClient)
    :httpclient
  elsif defined?(::Net::HTTP::Persistent)
    :net_http_persistent
  else
    ::Faraday.default_adapter
  end
end
connection() click to toggle source
# File lib/opentsdb/faraday.rb, line 28
def connection
  @connection ||= begin
    ::Faraday.new(url: url) do |faraday|
      faraday.request  :url_encoded              # form-encode POST params
      faraday.response :logger                   # log requests to STDOUT
      faraday.adapter auto_detect_adapter
    end
  end
end
headers() click to toggle source
# File lib/opentsdb/faraday.rb, line 24
def headers
  { 'Content-Type' => 'application/json; charset=UTF-8' }
end