Class: OrionWholesale::Category

Inherits:
Base
  • Object
show all
Defined in:
lib/orion_wholesale/category.rb

Overview

Category item response structure:

{
  code:        "...",  # ':category_code' in Catalog response.
  description: "..."   # ':category_description' in Catalog response.
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connect

Constructor Details

#initialize(options = {}) ⇒ Category

Returns a new instance of Category.



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

def initialize(options = {})
  requires!(options, :username, :password)
  @options = options
end

Class Method Details

.all(options = {}, &block) ⇒ Object



15
16
17
18
# File 'lib/orion_wholesale/category.rb', line 15

def self.all(options = {}, &block)
  requires!(options, :username, :password)
  new(options).all(&block)
end

Instance Method Details

#all(&block) ⇒ Object

Returns an array of hashes with category details.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/orion_wholesale/category.rb', line 21

def all(&block)
  categories = []

  # Categories are listed in catalog xml, so fetch that.
  catalog = Catalog.new(@options)
  catalog.all do |item|
    categories << {
      code: item[:subcategory].gsub("&amp;", "").gsub(/[^A-Za-z0-9]/, "_").squeeze("_").downcase,
      description: item[:subcategory].gsub("&amp;", "&")
    }
  end

  categories.uniq! { |c| c[:code] }
  categories.sort_by! { |c| c[:code] }

  categories.each do |category|
    yield category
  end
end