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.



20
21
22
# File 'lib/clientele/resource.rb', line 20

def subclasses
  @subclasses
end

Instance Attribute Details

#responseObject

Returns the value of attribute response.



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

def response
  @response
end

Class Method Details

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



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

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



41
42
43
# File 'lib/clientele/resource.rb', line 41

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

.method_nameObject



49
50
51
# File 'lib/clientele/resource.rb', line 49

def method_name
  @method_name || default_path
end

.pathObject



45
46
47
# File 'lib/clientele/resource.rb', line 45

def path
  @path || default_path
end

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



33
34
35
# File 'lib/clientele/resource.rb', line 33

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

.result(data) ⇒ Object



61
62
63
# File 'lib/clientele/resource.rb', line 61

def result(data)
  data[result_key]
end

.result_keyObject



53
54
55
# File 'lib/clientele/resource.rb', line 53

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

.results(data) ⇒ Object



65
66
67
# File 'lib/clientele/resource.rb', line 65

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

.results_keyObject



57
58
59
# File 'lib/clientele/resource.rb', line 57

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

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



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

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