class BooticClient::Strategies::Strategy

Attributes

config[R]
on_new_token[R]
options[R]

Public Class Methods

new(config, client_opts = {}, &on_new_token) click to toggle source
# File lib/bootic_client/strategies/strategy.rb, line 9
def initialize(config, client_opts = {}, &on_new_token)
  @config, @options, @on_new_token = config, client_opts, (on_new_token || Proc.new{})
  raise ArgumentError, 'must include a Configuration object' unless config
  validate!
end

Public Instance Methods

from_hash(hash, wrapper_class = Entity) click to toggle source
# File lib/bootic_client/strategies/strategy.rb, line 19
def from_hash(hash, wrapper_class = Entity)
  wrapper_class.new hash, self
end
from_url(url) click to toggle source
# File lib/bootic_client/strategies/strategy.rb, line 23
def from_url(url)
  request_and_wrap :get, url
end
inspect() click to toggle source
# File lib/bootic_client/strategies/strategy.rb, line 35
def inspect
  %(#<#{self.class.name} root: #{config.api_root}>)
end
request_and_wrap(request_method, href, payload = {}) click to toggle source
# File lib/bootic_client/strategies/strategy.rb, line 27
def request_and_wrap(request_method, href, payload = {})
  pre_flight
  retryable do
    resp = client.send(request_method, href, payload, request_headers)
    config.response_handlers.resolve(resp, self)
  end
end
root() click to toggle source
# File lib/bootic_client/strategies/strategy.rb, line 15
def root
  request_and_wrap :get, config.api_root
end

Protected Instance Methods

client() click to toggle source
# File lib/bootic_client/strategies/strategy.rb, line 77
def client
  @client ||= Client.new(options)
end
pre_flight() click to toggle source
# File lib/bootic_client/strategies/strategy.rb, line 49
def pre_flight
  # Runs before every request
  # Overwrite in sub classes to run checks
  # (ie authorisation status, missing options, expired token refresh)
end
request_headers() click to toggle source

Noop. Merge these headers into every request.

# File lib/bootic_client/strategies/strategy.rb, line 73
def request_headers
  {}
end
retryable() { || ... } click to toggle source

Noop. Overwrite in sub classes to implement retryable requests. Example:

def retryable(&block)
   begin
     yield # issue request
   rescue SomeException => e
     fix_cause_of_exception
     yield # try again
   end
end
# File lib/bootic_client/strategies/strategy.rb, line 68
def retryable(&block)
  yield
end
validate!() click to toggle source
# File lib/bootic_client/strategies/strategy.rb, line 43
def validate!
  # Overwrite in sub classes
  # to raise ArgumentErrors on
  # missing config attributes of options values.
end