Class: Clientele::Resource

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
Utils
Includes:
Utils
Defined in:
lib/clientele/resource.rb,
lib/clientele/resource/pagination.rb

Defined Under Namespace

Modules: Pagination

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Methods included from Utils

deep_camelize_keys, ensure_trailing_slash, merge_paths

Class Attribute Details

.subclassesObject (readonly)

Returns the value of attribute subclasses.



18
19
20
# File 'lib/clientele/resource.rb', line 18

def subclasses
  @subclasses
end

Instance Attribute Details

#responseObject

Returns the value of attribute response.



99
100
101
# File 'lib/clientele/resource.rb', line 99

def response
  @response
end

Class Method Details

.build(data, client: nil, response: nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/clientele/resource.rb', line 67

def build(data, client: nil, response: nil)
  new(
    catch(:build) do
      if data.kind_of? Hash
        if many = results(data)
          build many, client: client#, response: response
        elsif one = result(data)
          throw :build, one
        else
          throw :build, data
        end
      elsif data.respond_to? :map
        data.map do |dataset|
          build dataset, client: client#, response: response
        end
      end
    end
  ).tap do |instance|
    instance.instance_variable_set :@client, client if client
    instance.instance_variable_set :@response, response if response
  end
end

.default_pathObject



39
40
41
# File 'lib/clientele/resource.rb', line 39

def default_path
  self.name.split('::').last.pluralize.underscore
end

.method_nameObject



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

def method_name
  @method_name || default_path
end

.pathObject



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

def path
  @path || default_path
end

.request(verb, path = '', opts = {}, &callback) ⇒ Object



31
32
33
# File 'lib/clientele/resource.rb', line 31

def request(verb, path = '', opts = {}, &callback)
  send verb, path, opts, &callback
end

.result(data) ⇒ Object



59
60
61
# File 'lib/clientele/resource.rb', line 59

def result(data)
  data[result_key]
end

.result_keyObject



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

def result_key
  @result_key || method_name.to_s.singularize
end

.results(data) ⇒ Object



63
64
65
# File 'lib/clientele/resource.rb', line 63

def results(data)
  data[results_key] if data[results_key] and data[results_key].kind_of?(Array)
end

.results_keyObject



55
56
57
# File 'lib/clientele/resource.rb', line 55

def results_key
  @results_key || (@result_key || method_name).to_s.pluralize
end

.to_request(opts = {}, &callback) ⇒ Object



35
36
37
# File 'lib/clientele/resource.rb', line 35

def to_request(opts={}, &callback)
  get opts, &callback
end