Sha256: 4228bfecfdcc1a7cb9676289e59056834330c54dfd657acf90fa0ccd802a19ec

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Neoon
  module Client
    class Connection

      attr_reader :connection

      def initialize(url)
        @uri = URI.parse(url)
        end_point = "#{@uri.scheme}://#{@uri.host}:#{@uri.port}#{@uri.path}"
        @connection ||= Faraday.new(end_point, connection_options.merge(:builder => middleware))
        @connection.basic_auth(@uri.user, @uri.password) if @uri.user && @uri.password
      end

      include Request

    private

      def connection_options
        @connection_options ||= {
          :headers => {
            :accept => 'application/json',
            :content_type => 'application/json; charset=UTF-8',
            :x_stream => 'true',
            :user_agent => ['Neoon', Neoon::VERSION].join(' ')
          },
          :request => {
            :open_timeout => 5,
            :timeout => 10,
          },
        }
      end

      def middleware
        @middleware ||= Faraday::Builder.new do |builder|
          builder.use FaradayMiddleware::EncodeJson
          builder.use FaradayMiddleware::Mashify
          builder.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
          builder.use Faraday::Neoon::RaiseError

          builder.adapter Faraday.default_adapter
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
neoon-0.0.4 lib/neoon/client/connection.rb