Class: TripSpark::API

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Connection
Defined in:
lib/tripspark_api/api.rb

Overview

A base class implementing common API operations

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Connection

#adapter, #connection, #register_adapter

Class Method Details

.include_api(name) ⇒ Object

Include another API’s functionality via a new method on this API. For example, ‘include_api :routes` would include the “Routes” API into the “Client” API, accessible as `Client#routes`.



14
15
16
17
18
19
20
# File 'lib/tripspark_api/api.rb', line 14

def include_api name
  klass = self.const_get(name.to_s.constantize)
  define_method(name) do
    klass.new
  end
  self.memoize name
end

.require_all(folder, *libs) ⇒ Object

Require all the files given in ‘names` that exist in the given folder



23
24
25
26
27
# File 'lib/tripspark_api/api.rb', line 23

def require_all folder, *libs
  libs.each do |lib|
    require_relative "#{File.join(folder, lib)}"
  end
end

.singletonObject

Return a singleton instance of this API



30
31
32
# File 'lib/tripspark_api/api.rb', line 30

def singleton
  @singleton ||= self.new
end

Instance Method Details

#get_request(endpoint, opts = {}, &block) ⇒ Object

Perform a GET request over the connection to the given endpoint.



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

def get_request endpoint, opts={}, &block
  connection.get endpoint, opts, &block
end

#post_request(endpoint, opts = {}, &block) ⇒ Object

Perform a POST request over the connection to the given endpoint.



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

def post_request endpoint, opts={}, &block
  connection.post endpoint, opts, &block
end

#refreshObject

For APIs that extend Memoist, allow the user to call ‘refresh` as an alias for `flush_cache`.



48
49
50
# File 'lib/tripspark_api/api.rb', line 48

def refresh
  send(:flush_cache) if respond_to?(:flush_cache)
end