class ApiClient::Scope

Attributes

scopeable[R]

Public Class Methods

new(scopeable) click to toggle source
# File lib/api_client/scope.rb, line 13
def initialize(scopeable)
  @scopeable  = scopeable
  @params     = {}
  @headers    = {}
  @options    = {}
  @scopeable.default_scopes.each do |default_scope|
    self.instance_eval(&default_scope)
  end
end

Public Instance Methods

clone_only_headers() click to toggle source
# File lib/api_client/scope.rb, line 69
def clone_only_headers
  self.class.new(self.scopeable).headers(self.headers)
end
connection() click to toggle source
# File lib/api_client/scope.rb, line 23
def connection
  klass       = Connection.const_get((@adapter || Connection.default).to_s.capitalize)
  @connection = klass.new(@endpoint , @options || {})
  hooks      = @scopeable.connection_hooks || []
  hooks.each { |hook| hook.call(@connection, self) }
  @connection
end
delete(path, options = {}) click to toggle source
# File lib/api_client/scope.rb, line 108
def delete(path, options = {})
  request(:delete, path, options)
end
fetch(path, options = {}) click to toggle source

Half-level :) This is a swiss-army knife kind of method, extremely useful

# File lib/api_client/scope.rb, line 75
def fetch(path, options = {})
  scoped(self) do
    @scopeable.build get(path, options)
  end
end
get(path, options = {}) click to toggle source
# File lib/api_client/scope.rb, line 92
def get(path, options = {})
  request(:get, path, options)
end
headers(options = nil) click to toggle source
# File lib/api_client/scope.rb, line 57
def headers(options = nil)
  return @headers if options.nil?
  ApiClient::Utils.deep_merge(@headers, options) if options
  self
end
method_missing(method, *args, &block) click to toggle source

Dynamic delegation of scopeable methods

Calls superclass method
# File lib/api_client/scope.rb, line 113
def method_missing(method, *args, &block)
  if @scopeable.respond_to?(method)
    @scopeable.scoped(self) do
      @scopeable.send(method, *args, &block)
    end
  else
    super
  end
end
options(new_options = nil) click to toggle source

3 Pillars of scoping options - passed on the the adapter params - converted to query or request body headers - passed on to the request

# File lib/api_client/scope.rb, line 44
def options(new_options = nil)
  return @options if new_options.nil?
  ApiClient::Utils.deep_merge(@options, new_options)
  self
end
params(options = nil) click to toggle source
# File lib/api_client/scope.rb, line 50
def params(options = nil)
  return @params if options.nil?
  ApiClient::Utils.deep_merge(@params, options)  if options
  self
end
Also aliased as: scope
patch(path, options = {}) click to toggle source
# File lib/api_client/scope.rb, line 100
def patch(path, options = {})
  request(:patch, path, options)
end
post(path, options = {}) click to toggle source
# File lib/api_client/scope.rb, line 96
def post(path, options = {})
  request(:post, path, options)
end
put(path, options = {}) click to toggle source
# File lib/api_client/scope.rb, line 104
def put(path, options = {})
  request(:put, path, options)
end
raw() click to toggle source
# File lib/api_client/scope.rb, line 31
def raw
  @raw = true
  self
end
raw?() click to toggle source
# File lib/api_client/scope.rb, line 36
def raw?
  !!@raw
end
raw_body(options = nil) click to toggle source
# File lib/api_client/scope.rb, line 63
def raw_body(options = nil)
  return @raw_body if options.nil?
  @raw_body = options
  self
end
request(method, path, options = {}) click to toggle source

Low-level connection methods

# File lib/api_client/scope.rb, line 83
def request(method, path, options = {})
  options = options.dup

  raw = raw? || options.delete(:raw)
  params(options)
  response = connection.send method, path, (@raw_body || @params), @headers
  raw ? response : @scopeable.parse(response)
end
scope(options = nil)
Alias for: params