Class: Bitly::HTTP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bitly/http/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(adapter = Bitly::HTTP::Adapters::NetHTTP.new) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


6
7
8
9
# File 'lib/bitly/http/client.rb', line 6

def initialize(adapter=Bitly::HTTP::Adapters::NetHTTP.new)
  @adapter = adapter
  raise ArgumentError, "Adapter must have a request method." unless @adapter.respond_to?(:request)
end

Instance Method Details

#request(request) ⇒ Bitly::HTTP::Response

The main method for the HTTP client. It receives a Bitly::HTTP::Request object, makes the request described and returns a Bitly::HTTP::Response.

Parameters:

Returns:

Raises:

  • (Bitly::Error)

    If the response is not a successful response in the 2xx range, then we raise an error with the response passed as an argument. It is up to the application to catch this error.



22
23
24
25
26
27
28
29
30
# File 'lib/bitly/http/client.rb', line 22

def request(request)
  status, body, headers, success = @adapter.request(request)
  response = Bitly::HTTP::Response.new(status: status, body: body, headers: headers, request: request)
  if success
    return response
  else
    raise Bitly::Error, response
  end
end