Class: Rester::Client::Adapters::HttpAdapter

Inherits:
Adapter
  • Object
show all
Defined in:
lib/rester/client/adapters/http_adapter.rb

Defined Under Namespace

Classes: Connection

Instance Attribute Summary collapse

Attributes inherited from Adapter

#timeout

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Adapter

#headers, #initialize, #request

Constructor Details

This class inherits a constructor from Rester::Client::Adapters::Adapter

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/rester/client/adapters/http_adapter.rb', line 6

def connection
  @connection
end

Class Method Details

.can_connect_to?(service) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
# File 'lib/rester/client/adapters/http_adapter.rb', line 9

def can_connect_to?(service)
  if service.is_a?(URI)
    uri = service
  elsif service.is_a?(String) && URI::regexp.match(service)
    uri = URI(service)
  end

  !!uri && ['http', 'https'].include?(uri.scheme)
end

Instance Method Details

#connect(url) ⇒ Object

Class Methods



20
21
22
# File 'lib/rester/client/adapters/http_adapter.rb', line 20

def connect(url)
  nil.tap { @connection = Connection.new(url, timeout: timeout) }
end

#connected?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/rester/client/adapters/http_adapter.rb', line 24

def connected?
  !!connection
end

#request!(verb, path, encoded_data) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/rester/client/adapters/http_adapter.rb', line 28

def request!(verb, path, encoded_data)
  _require_connection

  data_key = [:get, :delete].include?(verb) ? :query : :data
  response = connection.request(verb, path,
    headers: headers, data_key => encoded_data)

  _prepare_response(response)
end