Class: FetchAPI::Item

Inherits:
Base
  • Object
show all
Defined in:
lib/fetchapi/item.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

basic_auth, key

Constructor Details

#initialize(id_or_attributes) ⇒ Item

:nodoc:



5
6
7
8
9
10
11
12
13
# File 'lib/fetchapi/item.rb', line 5

def initialize(id_or_attributes) #:nodoc:
  case id_or_attributes
  when Integer, String
    @attributes = get("/#{self.class.pluralized_class_name}/#{id_or_attributes.to_s}")
    @attributes = @attributes[self.class.singularized_class_name]
  when Hash
    @attributes = id_or_attributes
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class FetchAPI::Base

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



3
4
5
# File 'lib/fetchapi/item.rb', line 3

def attributes
  @attributes
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/fetchapi/item.rb', line 3

def id
  @id
end

Class Method Details

.create(options) ⇒ Object

Creates a new Item



27
28
29
# File 'lib/fetchapi/item.rb', line 27

def self.create(options)
  return FetchAPI::Item.new(execute(:post, "/items/create", :item => options)["item"])
end

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

Find :all items or a specific SKU



21
22
23
# File 'lib/fetchapi/item.rb', line 21

def self.find(selector, params={})
  super(selector, params={})
end

Instance Method Details

#destroyObject

Permanently deletes the Item



36
37
38
# File 'lib/fetchapi/item.rb', line 36

def destroy
  delete("/items/#{sku}/delete")
end

#downloadsObject

Returns all the downloads associated with this Item



47
48
49
50
51
52
# File 'lib/fetchapi/item.rb', line 47

def downloads
  downloads = get("/items/#{sku}/downloads")
  if downloads
    downloads["downloads"].map { |data| FetchAPI::Download.new(data) }
  end
end

#update(options) ⇒ Object

Immediately updates the Item



41
42
43
44
# File 'lib/fetchapi/item.rb', line 41

def update(options)
  self.attributes = put("/items/#{sku}", :item => options)["item"]
  self.id = self.sku
end