class Staccato::Rack::FaradayAsyncHttpAdaper

Staccato HttpAdaper to do async post

Public Class Methods

new(logger = nil) click to toggle source
# File lib/staccato/rack/faraday_async_http_adapter.rb, line 7
def initialize(logger = nil)
  @logger = logger
  @conn = Faraday.new(url: 'https://ssl.google-analytics.com') do |faraday|
    faraday.request :url_encoded             # form-encode POST params
    faraday.response :logger, @logger if @logger
    faraday.adapter Faraday.default_adapter  # make requests with Net::HTTP
  end
end

Public Instance Methods

async_post(data, url = '/collect') click to toggle source
# File lib/staccato/rack/faraday_async_http_adapter.rb, line 16
def async_post(data, url = '/collect')
  Thread.new(data, url) do
    begin
      execute(data, url)
    rescue => e
      @logger.error "Could not collect #{data.inspect} => #{e.message}"
    end
  end
end
Also aliased as: post
post(data, url = '/collect')
Alias for: async_post

Private Instance Methods

execute(post_data, post_url) click to toggle source
# File lib/staccato/rack/faraday_async_http_adapter.rb, line 30
def execute(post_data, post_url)
  @conn.post do |req|
    req.url post_url
    req.options.timeout = 2           # open/read timeout in seconds
    req.body = post_data
  end
end