Class: Rester::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rester/client.rb,
lib/rester/client/adapters.rb,
lib/rester/client/resource.rb,
lib/rester/client/response.rb,
lib/rester/client/middleware.rb

Defined Under Namespace

Modules: Adapters, Middleware Classes: Resource, Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, params = {}) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rester/client.rb', line 18

def initialize(adapter, params={})
  self.adapter = adapter
  self.version = params[:version]
  @error_threshold = (params[:error_threshold] || 3).to_i
  @retry_period = (params[:retry_period] || 1).to_f
  self.logger = params[:logger]
  @_breaker_enabled = params.fetch(:circuit_breaker_enabled,
    ENV['RACK_ENV'] != 'test' && ENV['RAILS_ENV'] != 'test'
  )

  @_resource = Resource.new(self)
  _init_requester

  # Send a test ping request to the service so we can store the producer's
  # name for future request logs
  fail Errors::ConnectionError unless connected?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)

Submits the method to the adapter.



92
93
94
# File 'lib/rester/client.rb', line 92

def method_missing(meth, *args, &block)
  @_resource.send(:method_missing, meth, *args, &block)
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

#error_thresholdObject (readonly)

Returns the value of attribute error_threshold.



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

def error_threshold
  @error_threshold
end

#loggerObject

Returns the value of attribute logger.



16
17
18
# File 'lib/rester/client.rb', line 16

def logger
  @logger
end

#retry_periodObject (readonly)

Returns the value of attribute retry_period.



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

def retry_period
  @retry_period
end

#versionObject

Returns the value of attribute version.



13
14
15
# File 'lib/rester/client.rb', line 13

def version
  @version
end

Instance Method Details

#circuit_breaker_enabled?Boolean

Returns:

  • (Boolean)


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

def circuit_breaker_enabled?
  !!@_breaker_enabled
end

#connected?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'lib/rester/client.rb', line 36

def connected?
  adapter.connected? && @_requester.call(:get, '/ping', {}).successful?
rescue Exception => e
  logger.error("Connection Error: #{e.inspect}")
  false
end

#nameObject



56
57
58
# File 'lib/rester/client.rb', line 56

def name
  @_producer_name
end

#request(verb, path, params = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/rester/client.rb', line 60

def request(verb, path, params={})
  path = _path_with_version(path)
  @_requester.call(verb, path, params)
rescue Utils::CircuitBreaker::CircuitOpenError
  # Translate this error so it's easier handle for clients.
  # Also, at some point we may want to extract CircuitBreaker into its own
  # gem, and this will make that easier.
  raise Errors::CircuitOpenError
end

#with_context(*args, &block) ⇒ Object

This is only implemented by the StubAdapter.



72
73
74
# File 'lib/rester/client.rb', line 72

def with_context(*args, &block)
  adapter.with_context(*args, &block)
end