Class: EcwidApi::Category

Inherits:
Entity
  • Object
show all
Defined in:
lib/ecwid_api/category.rb

Instance Method Summary collapse

Methods inherited from Entity

#[], #assign_attributes, #assign_raw_attributes, define_accessor, #destroy!, ecwid_accessor, ecwid_reader, ecwid_writer, #initialize, #save, #to_hash, #to_json, #update_attributes, #update_raw_attributes, #url

Constructor Details

This class inherits a constructor from EcwidApi::Entity

Instance Method Details

#all_sub_categories(params = {}) ⇒ Object

Public: Returns an Array of all of the sub categories (deep) for the

Category


17
18
19
20
21
# File 'lib/ecwid_api/category.rb', line 17

def all_sub_categories(params = {})
  @all_sub_categories ||= sub_categories(params) + sub_categories.flat_map do |sub|
    sub.all_sub_categories(params)
  end
end

#parentObject

Public: Returns the parent EcwidApi::Category, or nil if there isn’t one



24
25
26
27
28
29
# File 'lib/ecwid_api/category.rb', line 24

def parent
  @parent ||= begin
    parent_id = data["parentId"]
    client.categories.find(parent_id) if parent_id
  end
end

#parentsObject



31
32
33
34
35
36
37
# File 'lib/ecwid_api/category.rb', line 31

def parents
  if parent
    parent.parents + [parent]
  else
    []
  end
end

#product_idsObject



50
51
52
# File 'lib/ecwid_api/category.rb', line 50

def product_ids
  super || []
end

#products(params = {}) ⇒ Object

Public: Returns the Products that belong to the Category

params - a Hash of values that can be used for a Prdocut search

Returns an Enumeration of Products



44
45
46
47
48
# File 'lib/ecwid_api/category.rb', line 44

def products(params = {})
  @products ||= product_ids.map do |product_id|
    client.products.find(product_id)
  end
end

#sub_categories(params = {}) ⇒ Object

Public: Returns an Array of sub categories for the Category



11
12
13
# File 'lib/ecwid_api/category.rb', line 11

def sub_categories(params = {})
  @sub_categories ||= client.categories.all(params.merge(parent: id))
end

#upload_image!(filename) ⇒ Object

Public: Uploads an image for the Category

filename - a String that is the path to a local file or a URL

Returns a Faraday::Response object



59
60
61
# File 'lib/ecwid_api/category.rb', line 59

def upload_image!(filename)
  client.post_image("categories/#{id}/image", filename)
end