Sha256: 3375b93ddf2641e765eeafa6fb06e3a137c93d53e2ccd2576f7bfefdb413d1f7

Contents?: true

Size: 1.74 KB

Versions: 8

Compression:

Stored size: 1.74 KB

Contents

module GnipApi
  class Adapter
    GET = 'GET'
    POST = 'POST'
    DELETE = 'DELETE'

    attr_reader :adapter

    def initialize
      raise GnipApi::Errors::MissingCredentials unless GnipApi.credentials?
      raise GnipApi::Errors::MissingAdapter unless GnipApi.adapter_class?
      @adapter = GnipApi.config.adapter_class.new
      @logger = GnipApi.config.logger
    end

    def get request
      log_request(request)
      response = adapter.get(request)
      check_response_for_errors(response)
      return response.body unless response.body.empty?
      return true
    end

    def post request
      log_request(request)
      response = adapter.post(request)
      check_response_for_errors(response)
      return response.body unless response.body.empty?
      return true
    end

    def delete request
      log_request(request)
      response = adapter.delete(request)
      check_response_for_errors(response)
      return response.body unless response.body.empty?
      return true
    end

    def stream_get request
      log_request(request)
      adapter.stream_get(request) do |data|
        yield(data)
      end
    end

    def check_response_for_errors response
      if response.ok?
        @logger.info "#{response.request_method} request to #{response.request_uri} returned with status #{response.status} OK"
      else
        error_message = response.error_message
        @logger.error "#{response.request_method} request to #{response.request_uri} returned with status #{response.status} FAIL: #{error_message}"
        raise GnipApi::Errors::Adapter::RequestError.new, error_message
      end
    end

    private
    def log_request request
      @logger.info "Starting #{request.request_method} request to #{request.uri}"
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gnip_api-0.0.9 lib/gnip_api/adapter.rb
gnip_api-0.0.8 lib/gnip_api/adapter.rb
gnip_api-0.0.7 lib/gnip_api/adapter.rb
gnip_api-0.0.6 lib/gnip_api/adapter.rb
gnip_api-0.0.5 lib/gnip_api/adapter.rb
gnip_api-0.0.4 lib/gnip_api/adapter.rb
gnip_api-0.0.3 lib/gnip_api/adapter.rb
gnip_api-0.0.2 lib/gnip_api/adapter.rb