class BillHicks::Catalog

Catalog item response structure:

{
  product_name:      "...",
  upc:               "...",
  short_description: "...",
  long_description:  "...",
  category:          "...",
  price:             "...",
  weight:            "...",
  map:               "...",
  msrp:              "...",
}

Constants

CATALOG_FILENAME
CHUNK_SIZE
PERMITTED_FEATURES

Public Class Methods

all(options = {}) click to toggle source
# File lib/bill_hicks/catalog.rb, line 50
def self.all(options = {})
  requires!(options, :username, :password)
  new(options).all
end
new(options = {}) click to toggle source
# File lib/bill_hicks/catalog.rb, line 45
def initialize(options = {})
  requires!(options, :username, :password)
  @options = options
end

Public Instance Methods

all() click to toggle source
# File lib/bill_hicks/catalog.rb, line 55
def all
  items = []

  connect(@options) do |ftp|
    tempfile = Tempfile.new

    ftp.chdir(BillHicks.config.top_level_dir)
    ftp.getbinaryfile(CATALOG_FILENAME, tempfile.path)

    SmarterCSV.process(tempfile, {
      chunk_size: CHUNK_SIZE,
      force_utf8: true,
      convert_values_to_numeric: false,
      key_mapping: {
        universal_product_code: :upc,
        product_name:           :name,
        product_weight:         :weight,
        product_price:          :price,
        category_description:   :category,
        marp:                   :map_price,
      }
    }) do |chunk|
      chunk.each do |item|
        item.except!(:category_code)

        item[:item_identifier] = item[:name]
        item[:brand]           = BillHicks::BrandConverter.convert(item[:name])
        item[:mfg_number]      = item[:name].split.last

        if item[:long_description].present?
          features = parse_features(item[:long_description])

          item[:action]  = features.delete(:action)  if features[:action].present?
          item[:caliber] = features.delete(:caliber) if features[:caliber].present?
          item[:weight]  = features.delete(:weight)  if features[:weight].present?

          item[:features] = features
        end

        items << item
      end
    end

    tempfile.unlink
  end

  items
end

Protected Instance Methods

parse_features(text) click to toggle source
# File lib/bill_hicks/catalog.rb, line 106
def parse_features(text)
  features = Hash.new
  text = text.split("-")

  text.each do |feature|
    if feature.include?(':') && feature.length <= 45
      key, value = feature.split(':')

      if key.nil? || value.nil?
        next
      end

      key, value = key.strip.downcase, value.strip

      if PERMITTED_FEATURES.include?(key)
        features[key.gsub(" ", "_")] = value
      end
    end
  end

  features
end