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
- #fetch(id) ⇒ Object
- #fetch_all ⇒ Object
- #find_by_id(id) ⇒ Object
- #to_a ⇒ Object
- #url(id = nil, depth = 1) ⇒ Object
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_all ⇒ Object
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_a ⇒ Object
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 |