class Wrappi::Endpoint

Attributes

input_params[R]
options[R]

Public Class Methods

around_request(&block) click to toggle source
# File lib/wrappi/endpoint.rb, line 46
def self.around_request(&block)
  @around_request = block
end
async_callback(&block) click to toggle source
Configs =================
# File lib/wrappi/endpoint.rb, line 42
def self.async_callback(&block)
  @async_callback = block
end
body(*args) click to toggle source
# File lib/wrappi/endpoint.rb, line 33
def self.body(*args)
  new(*args).body
end
cache_options(&block) click to toggle source
# File lib/wrappi/endpoint.rb, line 54
def self.cache_options(&block)
  @cache_options = block
end
call(*args) click to toggle source
ClassMethods     ################
API class metnods ================
# File lib/wrappi/endpoint.rb, line 25
def self.call(*args)
  new(*args).call
end
call!(*args) click to toggle source
# File lib/wrappi/endpoint.rb, line 29
def self.call!(*args)
  new(*args).call!
end
inherited(subclass) click to toggle source
Inheritance =================
Calls superclass method
# File lib/wrappi/endpoint.rb, line 59
def self.inherited(subclass)
  super(subclass)
  subclass.instance_variable_set(:@async_callback, @async_callback)
  subclass.instance_variable_set(:@around_request, @around_request)
  subclass.instance_variable_set(:@retry_if, @retry_if)
  subclass.instance_variable_set(:@cache_options, @cache_options)
end
new(input_params = {}, options = {}) click to toggle source
# File lib/wrappi/endpoint.rb, line 75
def initialize(input_params = {}, options = {})
  @input_params = input_params
  @options = options
end
retry_if(&block) click to toggle source
# File lib/wrappi/endpoint.rb, line 50
def self.retry_if(&block)
  @retry_if = block
end
setup(&block) click to toggle source
# File lib/wrappi/endpoint.rb, line 37
def self.setup(&block)
  instance_exec(&block)
end
success?(request) click to toggle source
success behaviour ===================

overridable

# File lib/wrappi/endpoint.rb, line 69
def self.success?(request)
  request.code >= 200 && request.code < 300
end

Public Instance Methods

around_request() click to toggle source
# File lib/wrappi/endpoint.rb, line 141
def around_request
  self.class.instance_variable_get(:@around_request)
end
async(async_options = {}) click to toggle source
# File lib/wrappi/endpoint.rb, line 111
def async(async_options = {})
  async_handler.call(self, async_options)
end
body() click to toggle source
# File lib/wrappi/endpoint.rb, line 105
def body; response.body end
cache_key() click to toggle source
# File lib/wrappi/endpoint.rb, line 135
def cache_key
  # TODO: think headers have to be in the key as well
  @cache_key ||= "[#{verb.to_s.upcase}]##{url}#{params_cache_key}"
end
cache_options() click to toggle source
# File lib/wrappi/endpoint.rb, line 149
def cache_options
  self.class.instance_variable_get(:@cache_options)
end
call() click to toggle source
# File lib/wrappi/endpoint.rb, line 91
def call
  return false unless success?
  self
end
call!() click to toggle source
# File lib/wrappi/endpoint.rb, line 96
def call!
  raise UnsuccessfulResponse.new(self) unless success?
  self
end
consummated_params() click to toggle source

overridable

# File lib/wrappi/endpoint.rb, line 116
def consummated_params
  params
end
error?() click to toggle source
# File lib/wrappi/endpoint.rb, line 108
def error?; !success? end
fixture_content() click to toggle source
# File lib/wrappi/testing.rb, line 14
def fixture_content
  return {} unless success?
  {
    request: {
      method: verb.to_s,
      url: url,
      domain: domain,
      headers: headers,
      params: consummated_params,
      path: path_gen.path
    },
    response: {
      status: status,
      body: body
    }
  }
end
fixture_name() click to toggle source
# File lib/wrappi/testing.rb, line 4
def fixture_name
  "#{self.class}#{fixture_params_key}.json"
end
fixture_params_key() click to toggle source
# File lib/wrappi/testing.rb, line 8
def fixture_params_key
  return if processed_params.empty?
  d = Digest::MD5.hexdigest processed_params.to_json
  "-#{d}"
end
flush() click to toggle source
# File lib/wrappi/endpoint.rb, line 109
def flush; @response = nil end
on_error(&block) click to toggle source
# File lib/wrappi/endpoint.rb, line 86
def on_error(&block)
  block.call(self) unless success?
  self
end
on_success(&block) click to toggle source
# File lib/wrappi/endpoint.rb, line 81
def on_success(&block)
  block.call(self) if success?
  self  
end
perform_async_callback(async_options = {}) click to toggle source
# File lib/wrappi/endpoint.rb, line 131
def perform_async_callback(async_options = {})
  instance_exec(async_options, &async_callback)
end
response() click to toggle source
# File lib/wrappi/endpoint.rb, line 101
def response
  @response ||= Executer.call(self)
end
retry_if() click to toggle source
# File lib/wrappi/endpoint.rb, line 145
def retry_if
  self.class.instance_variable_get(:@retry_if)
end
status() click to toggle source
# File lib/wrappi/endpoint.rb, line 107
def status; response.status end
success?() click to toggle source
# File lib/wrappi/endpoint.rb, line 106
def success?; response.success? end
url() click to toggle source
# File lib/wrappi/endpoint.rb, line 120
def url
  _url.to_s
end
url_with_params() click to toggle source
# File lib/wrappi/endpoint.rb, line 124
def url_with_params
  return url unless verb == :get
  _url.tap do |u|
    u.query = URI.encode_www_form(consummated_params) if consummated_params.any?
  end.to_s
end

Private Instance Methods

_url() click to toggle source

URI behaviour example:

URI.join('https://hello.com/foo/bar', '/bin').to_s
=> "https://hello.com/bin"

URI.join('https://hello.com/foo/bar', 'bin').to_s
=> "https://hello.com/foo/bin"

URI.join('https://hello.com/foo/bar/', '/bin').to_s
=> "https://hello.com/bin"

We want this behaviour:
URI.join('https://hello.com/foo/bar/', 'bin').to_s
=> "https://hello.com/foo/bar/bin"
# File lib/wrappi/endpoint.rb, line 190
def _url
  URI.join(domain, path_gen.for_uri)
end
async_callback() click to toggle source
# File lib/wrappi/endpoint.rb, line 155
def async_callback
  self.class.instance_variable_get(:@async_callback) || proc {}
end
async_handler() click to toggle source

Overridable

# File lib/wrappi/endpoint.rb, line 164
def async_handler
  client.async_handler
end
domain() click to toggle source
# File lib/wrappi/endpoint.rb, line 194
def domain
  return client.domain if client.domain =~ /\/$/
  "#{client.domain}/"
end
logger() click to toggle source
# File lib/wrappi/endpoint.rb, line 159
def logger
  client.logger
end
params() click to toggle source
# File lib/wrappi/endpoint.rb, line 199
def params
  path_gen.params
end
params_cache_key() click to toggle source
# File lib/wrappi/endpoint.rb, line 168
def params_cache_key
  return if params.empty?
  d = Digest::MD5.hexdigest params.to_json
  "?#{d}"
end
path_gen() click to toggle source
# File lib/wrappi/endpoint.rb, line 207
def path_gen
  @path_gen ||= PathGen.new(path, processed_params)
end
processed_params() click to toggle source
# File lib/wrappi/endpoint.rb, line 203
def processed_params
  client.params.merge(default_params.merge(input_params))
end