Sha256: a821f4e6bc11750cdbe231773d3371d720f41815da0d7d3d8f8484f797ef8a0f

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

require 'forwardable'
require 'faraday'
require 'uri'

module Binnacle
  class Connection
    extend Forwardable

    attr_reader :connection
    attr_reader :active_url
    attr_reader :contact_url

    def_delegators :@connection, :get, :post, :put, :delete, :head, :patch, :options

    def initialize(api_key = nil, api_secret = nil, url = nil)
      @contact_url = url || Binnacle.configuration.url
      @active_url = @contact_url
      @api_key = api_key || Binnacle.configuration.api_key
      @api_secret = api_secret || Binnacle.configuration.api_secret

      raise Binnacle::ConfigurationError.new("Binnacle URL not provided, set BINNACLE_URL or provide in the constructor") unless @contact_url

      build_connection
      randomize_endpoint
    end

    def endpoints
      begin
        response = @connection.get do |req|
          req.url "/api/endpoints"
          req.headers['Content-Type'] = 'application/json'
        end

        if response.status == 401
          Binnacle.binnacle_logger.error("Error communicating with Binnacle (/api/endpoints): #{response.body}")
          []
        else
          JSON.parse(response.body)
        end
      rescue Faraday::Error::ConnectionFailed => cf
        Binnacle.binnacle_logger.error("Error communicating with Binnacle (/api/endpoints): #{cf.message}")
        []
      end
    end

    def randomize_endpoint
      fresh_endpoints = endpoints
      if fresh_endpoints.size > 1
        Binnacle.configuration.endpoint = fresh_endpoints
        @active_url = Binnacle.configuration.url
        build_connection()
      end
    end

    def build_connection
      @connection ||= Faraday.new(:url => @active_url) do |faraday|
        faraday.request :basic_auth, @api_key, @api_secret
        faraday.request  :url_encoded             # form-encode POST params
        #faraday.response :logger                  # log requests to STDOUT TODO set a client log file
        faraday.adapter :httpclient
      end
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
binnacle-0.5.0 lib/binnacle/connection.rb
binnacle-0.4.9 lib/binnacle/connection.rb
binnacle-0.4.8 lib/binnacle/connection.rb