Module: Cornerstore::Resource::Remote

Included in:
Cart::Resource, Collection::Resource, LineItem::Resource, Product::Resource, Property::Resource
Defined in:
lib/cornerstore/resource/remote.rb

Instance Method Summary collapse

Instance Method Details

#fetch(id) ⇒ Object



12
13
14
15
16
# File 'lib/cornerstore/resource/remote.rb', line 12

def fetch(id)
  response = RestClient.get(url(id), Cornerstore.headers)
  hash = ActiveSupport::JSON.decode(response)
  @klass.new(hash, @parent)
end

#fetch_allObject



18
19
20
21
# File 'lib/cornerstore/resource/remote.rb', line 18

def fetch_all
  response = RestClient.get(url, Cornerstore.headers)
  from_array ActiveSupport::JSON.decode(response)
end

#find_by_id(id) ⇒ Object



7
8
9
10
# File 'lib/cornerstore/resource/remote.rb', line 7

def find_by_id(id)
  object = super
  object or fetch(id)
end

#to_aObject



23
24
25
26
# File 'lib/cornerstore/resource/remote.rb', line 23

def to_a
  fetch_all unless @load
  super
end

#url(id = nil, depth = 1) ⇒ Object



2
3
4
5
# File 'lib/cornerstore/resource/remote.rb', line 2

def url(id = nil, depth = 1)
  root = (@parent && depth > 0) ? @parent.url(depth-1) : Cornerstore.root_url
  "#{root}/#{@name or @klass.name.split('::').last.underscore.pluralize}/#{id}"
end