Class: ApiResource::Resource

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/api-resource/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil, raw_result = nil) ⇒ Resource

Returns a new instance of Resource.



50
51
52
53
# File 'lib/api-resource/resource.rb', line 50

def initialize(hash=nil, raw_result=nil)
  self.attributes = hash if hash
  @raw_result = raw_result
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &_) ⇒ Object (protected)



192
193
194
195
196
197
198
199
200
# File 'lib/api-resource/resource.rb', line 192

def method_missing(m, *args, &_)
  case m
    when /^by_(.+)/
      self.resource_path = self.class.build_path($1.pluralize, args[0], resource_path)
      self
    else
      super
  end
end

Instance Attribute Details

#raw_resultObject (readonly)

Returns the value of attribute raw_result.



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

def raw_result
  @raw_result
end

Class Method Details

.allObject



77
78
79
# File 'lib/api-resource/resource.rb', line 77

def self.all
  self.where({})
end

.all_pages(params = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/api-resource/resource.rb', line 81

def self.all_pages(params={})
  params  = { page: 1, per_page: default_per_page }.merge(params)
  per_page = params[:per_page].to_i
  page     = params[:page].to_i
  begin
    params[:page] = page
    result         = self.where(params)
    total_count    = result.try :total_count
    arr            = result.to_a
    count          = arr.length
    break if yield(arr, page, per_page, total_count) || (page - 1) * per_page + count >= total_count
    page += 1
  end while true
end

.destroy!(id) ⇒ Object



101
102
103
104
# File 'lib/api-resource/resource.rb', line 101

def self.destroy!(id)
  client(:delete, {}, resource_path, id)
  self
end

.filter_submitted_attributes(&block) ⇒ Object



46
47
48
# File 'lib/api-resource/resource.rb', line 46

def self.(&block)
  self.pre_submit_filter = block if block_given?
end

.find(id, params = {}) ⇒ Object



71
72
73
74
75
# File 'lib/api-resource/resource.rb', line 71

def self.find(id, params={})
  result = client(:get, params, resource_path, id)
  json   = JSON.parse(result)
  self.new(json['data'] || json, result)
end

.resource_pathObject



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

def self.resource_path
  @resource_path || name.to_s.tableize.split('/').last
end

.resource_path=(path) ⇒ Object



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

def self.resource_path=(path)
  @resource_path = path.gsub(%r{\A/+|/+\Z}, '')
end

.where(params) ⇒ Object



96
97
98
99
# File 'lib/api-resource/resource.rb', line 96

def self.where(params)
  result = client(:get, params, resource_path)
  create_resource_collection(result)
end

.with_hmac(api_id, api_secret, options = {}) ⇒ Object



39
40
41
42
43
44
# File 'lib/api-resource/resource.rb', line 39

def self.with_hmac(api_id, api_secret, options={})
  self.hmac         = true
  self.api_id       = api_id
  self.api_secret   = api_secret
  self.hmac_options = options
end

Instance Method Details

#attributesObject



130
131
132
# File 'lib/api-resource/resource.rb', line 130

def attributes
  instance_values.with_indifferent_access.except(:errors, :validation_context, :resource_path)
end

#destroy!(options = {}) ⇒ Object



125
126
127
128
# File 'lib/api-resource/resource.rb', line 125

def destroy!(options={})
  submit_resource(resource_path, false, options[:id_attr], :delete)
  self
end

#resource_pathObject



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

def resource_path
  @resource_path ||= self.class.resource_path
end

#resource_path=(path) ⇒ Object



67
68
69
# File 'lib/api-resource/resource.rb', line 67

def resource_path=(path)
  @resource_path = path
end

#save!(options = {}) ⇒ Object



106
107
108
109
# File 'lib/api-resource/resource.rb', line 106

def save!(options={})
  submit_resource(resource_path, true, options[:id_attr])
  self
end

#submit!(options = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/api-resource/resource.rb', line 111

def submit!(options={})
  path = options.fetch(:path, resource_path)
  type = options[:type]
  json = submit_resource(path, false, options[:id_attr])
  meta = json && json['meta']
  type ||= meta && meta['type']
  if type
    returned_type = self.class.load_class(type)
    returned_type.new(json['data'] || json, raw_result)
  else
    self
  end
end