Class: ApiClient::Scope

Inherits:
Object
  • Object
show all
Extended by:
Mixins::Configuration, Mixins::Delegation
Defined in:
lib/api_client/scope.rb

Direct Known Subclasses

Resource::Scope

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Configuration

dsl_accessor

Methods included from Mixins::Delegation

delegate

Constructor Details

#initialize(scopeable) ⇒ Scope

Returns a new instance of Scope.



13
14
15
16
17
18
19
20
21
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Dynamic delegation of scopeable methods



113
114
115
116
117
118
119
120
121
# 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

Instance Attribute Details

#scopeableObject (readonly)

Returns the value of attribute scopeable.



11
12
13
# File 'lib/api_client/scope.rb', line 11

def scopeable
  @scopeable
end

Instance Method Details

#clone_only_headersObject



69
70
71
# File 'lib/api_client/scope.rb', line 69

def clone_only_headers
  self.class.new(self.scopeable).headers(self.headers)
end

#connectionObject



23
24
25
26
27
28
29
# 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 = {}) ⇒ Object



108
109
110
# File 'lib/api_client/scope.rb', line 108

def delete(path, options = {})
  request(:delete, path, options)
end

#fetch(path, options = {}) ⇒ Object

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



75
76
77
78
79
# 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 = {}) ⇒ Object



92
93
94
# File 'lib/api_client/scope.rb', line 92

def get(path, options = {})
  request(:get, path, options)
end

#headers(options = nil) ⇒ Object



57
58
59
60
61
# 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

#options(new_options = nil) ⇒ Object

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



44
45
46
47
48
# 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) ⇒ Object Also known as: scope



50
51
52
53
54
# 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

#patch(path, options = {}) ⇒ Object



100
101
102
# File 'lib/api_client/scope.rb', line 100

def patch(path, options = {})
  request(:patch, path, options)
end

#post(path, options = {}) ⇒ Object



96
97
98
# File 'lib/api_client/scope.rb', line 96

def post(path, options = {})
  request(:post, path, options)
end

#put(path, options = {}) ⇒ Object



104
105
106
# File 'lib/api_client/scope.rb', line 104

def put(path, options = {})
  request(:put, path, options)
end

#rawObject



31
32
33
34
# File 'lib/api_client/scope.rb', line 31

def raw
  @raw = true
  self
end

#raw?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/api_client/scope.rb', line 36

def raw?
  !!@raw
end

#raw_body(options = nil) ⇒ Object



63
64
65
66
67
# 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 = {}) ⇒ Object

Low-level connection methods



83
84
85
86
87
88
89
90
# 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