Class: Clientele::API

Inherits:
Object
  • Object
show all
Extended by:
BlockParty::Configurable, SingleForwardable
Defined in:
lib/clientele/api.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ API

Returns a new instance of API.



75
76
77
78
79
80
# File 'lib/clientele/api.rb', line 75

def initialize(opts={})
  self.extend BlockParty::Configurable
  self.configure_with self.class.configuration.class
  self.configuration = self.class.configuration.clone
  self.configuration.load_hash opts
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



90
91
92
93
94
95
96
# File 'lib/clientele/api.rb', line 90

def method_missing(method_name, *args, &block)
  if resources.keys.include? method_name.to_s
    RequestBuilder.new(resources[method_name.to_s], client: self)
  elsif Request::VERBS.include? method_name.to_s
    RequestBuilder.new(Request.send(method_name.to_s, *args), client: self)
  else; super; end
end

Class Method Details

.client(opts = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/clientele/api.rb', line 26

def client(opts={})
  autoconfigure!
  if @client
    @client.tap do |client|
      client.configuration.load_hash(opts)
    end
  else
    @client = new(opts)
  end
end

.loggerObject



37
38
39
40
# File 'lib/clientele/api.rb', line 37

def logger
  autoconfigure!
  configuration.logger
end

.reconfigure_global_client!(opts = {}) ⇒ Object



51
52
53
54
# File 'lib/clientele/api.rb', line 51

def reconfigure_global_client!(opts={})
  reset_global_client!
  client(opts)
end

.reset_global_client!Object



47
48
49
# File 'lib/clientele/api.rb', line 47

def reset_global_client!
  @client = nil
end

.resource(klass) ⇒ Object



42
43
44
45
# File 'lib/clientele/api.rb', line 42

def resource(klass)
  klass.client = self
  self.resources = resources.merge(klass.method_name.to_sym => klass)
end

Instance Method Details

#clientObject



82
# File 'lib/clientele/api.rb', line 82

def client; self; end