Sha256: 929323d71366ac92c6bb6c9223f3dbb6b25888e7d061b9d4930b82a3a00f0b0e

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 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
      include Indexing

    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.adapter Faraday.default_adapter
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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