Sha256: 0d2a8b8b979de2f957ddfccf5050985e195633235c48f3752d41cb5af237dac4

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 KB

Contents

require "horizon_client/version"
require "faraday"
require "ox"

require 'horizon_client/response/parse_xml'
require 'horizon_client/request/encode_xml'

require "horizon_client/resource"
require "horizon_client/collection"
require "horizon_client/entity"

module HorizonClient
  def self.new(*args)
    Connection.new(*args)
  end

  class Connection
    attr_accessor :logger

    def initialize(url = nil)
      url ||=  ENV['HORIZON_REST_URL']

      @connection = Faraday.new url do |conn|
        conn.response :raise_error
        conn.use HorizonClient::Response::ParseXml
        conn.use HorizonClient::Request::EncodeXml

        conn.adapter Faraday.default_adapter
      end
    end

    def url_prefix
      @connection.url_prefix
    end

    def get(path = '', params = {})
      log "GET #{path}", params do
        response = @connection.get path, params
        response.body
      end
    end

    def post(path = '', body)
      log "GET #{path}", {body: body} do
        response = @connection.post do |req|
          req.url path
          req.headers['Content-Type'] = 'application/xml;charset=UTF-8'
          req.body = body
        end

        response.body
      end
    end

    def log(message, params = {})
      t1 = Time.now
      yield
    ensure
      t2 = Time.now
      duration = (t2 - t1) / 1000
      logger.info "Horizon (#{duration}ms) #{message} #{params.inspect}" if logger
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
horizon_client-0.2.4 lib/horizon_client.rb
horizon_client-0.2.3 lib/horizon_client.rb
horizon_client-0.2.2 lib/horizon_client.rb
horizon_client-0.2.1 lib/horizon_client.rb