class UTApi::Connection

Attributes

request_interval[W]

Public Class Methods

new() click to toggle source
# File lib/utapi/connection.rb, line 13
def initialize
  @user_agent = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)'
end

Public Instance Methods

get(*args, &block) click to toggle source

TODO: Define a better interface for these function

# File lib/utapi/connection.rb, line 18
def get(*args, &block)
  connection.get(*args, &block)
end
post(*args, &block) click to toggle source
# File lib/utapi/connection.rb, line 22
def post(*args, &block)
  connection.post(*args, &block)
end
request_interval() click to toggle source
# File lib/utapi/connection.rb, line 30
def request_interval
  @request_interval ||= 2
end

Private Instance Methods

connection() click to toggle source
# File lib/utapi/connection.rb, line 36
def connection
  @connection ||= Faraday.new(ssl: {verify: false}, request: {timeout: 20, open_timeout: 20}, headers: { 'User-Agent' => @user_agent }) do |faraday|
    faraday.response :follow_redirects, limit: 5
    #faraday.response :raise_error # This catches generic http errors
    faraday.request :retry, exceptions: [UTApi::ConnectionError, UTApi::ServerError], interval: 1, backoff_factor: 2, max: 3
    faraday.use :error_handler # This is custom error handling
    faraday.use :cookie_jar, jar: cookie_jar
    faraday.request :url_encoded
    faraday.request :json
    faraday.response :json, content_type: 'application/json'
    faraday.request :rate_limiter, interval: request_interval if request_interval > 0
    faraday.adapter  :net_http_persistent
  end
end