Class: Rester::Client::Adapters::Adapter

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

Direct Known Subclasses

HttpAdapter, LocalAdapter, StubAdapter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service = nil, opts = {}) ⇒ Adapter

Returns a new instance of Adapter.



14
15
16
17
# File 'lib/rester/client/adapters/adapter.rb', line 14

def initialize(service = nil, opts = {})
  @timeout = opts[:timeout]
  connect(service) if service
end

Instance Attribute Details

#timeoutObject (readonly)

Class Methods



12
13
14
# File 'lib/rester/client/adapters/adapter.rb', line 12

def timeout
  @timeout
end

Class Method Details

.can_connect_to?(service) ⇒ Boolean

Returns whether or not the Adapter can connect to the service

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


7
8
9
# File 'lib/rester/client/adapters/adapter.rb', line 7

def can_connect_to?(service)
  raise NotImplementedError
end

Instance Method Details

#connect(*args) ⇒ Object

Connect to a service. The specific arguments depend on the Adapter subclass.

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/rester/client/adapters/adapter.rb', line 29

def connect(*args)
  raise NotImplementedError
end

#connected?Boolean

Returns whether or not the Adapter is connected to a service.

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


35
36
37
# File 'lib/rester/client/adapters/adapter.rb', line 35

def connected?
  raise NotImplementedError
end

#headers(new_headers = {}) ⇒ Object

Returns the headers defined for this Adapter. Optionally, you may also define additional headers you’d like to add/override.



22
23
24
# File 'lib/rester/client/adapters/adapter.rb', line 22

def headers(new_headers = {})
  (@headers ||= {}).merge!(new_headers)
end

#request(verb, path, params = nil) ⇒ Object

Sends a request (using one of the subclass adapters) to the service.

‘params` should be a hash if specified.



43
44
45
46
# File 'lib/rester/client/adapters/adapter.rb', line 43

def request(verb, path, params = nil)
  _validate_verb(verb)
  request!(verb, path.to_s, Utils.encode_www_data(params))
end

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

Sends an HTTP request to the service.

‘encoded_data` should be URL encoded set of parameters (e.g., “key1=value1&key2=value2”)



53
54
55
# File 'lib/rester/client/adapters/adapter.rb', line 53

def request!(verb, path, encoded_data)
  fail NotImplementedError
end