class MgeWholesale::Catalog

Constants

CATALOG_FILENAME
ITEM_NODE_NAME
PERMITTED_FEATURES

Public Class Methods

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

Public Instance Methods

all() click to toggle source
# File lib/mge_wholesale/catalog.rb, line 52
def all
  tempfile = get_file(CATALOG_FILENAME)
  items = []

  Nokogiri::XML::Reader.from_io(tempfile).each do |node|
    next unless node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT
    next unless node.name == ITEM_NODE_NAME

    _map_hash = map_hash(Nokogiri::XML::DocumentFragment.parse(node.inner_xml))

    items << _map_hash unless _map_hash.nil?
  end

  tempfile.close
  tempfile.unlink

  items
end

Protected Instance Methods

map_features(node) click to toggle source
# File lib/mge_wholesale/catalog.rb, line 113
def map_features(node)
  features = Hash.new

  node.elements.each do |n|
    if PERMITTED_FEATURES.include?(n.name.strip)
      # fix their typo
      n.name = "Magnification" if n.name.strip == "Magnifactaion"

      features[n.name.strip] = n.text.gsub("_", " ").strip
    end
  end

  features.delete_if { |k, v| v.to_s.blank? }
  features.transform_keys! { |k| k.downcase }
  features.transform_keys! { |k| k.gsub(/[^0-9A-Za-z\_]/, '') }
  features.symbolize_keys!
end
map_hash(node) click to toggle source
# File lib/mge_wholesale/catalog.rb, line 73
def map_hash(node)
  features = map_features(node)

  category = content_for(node, 'category')
  subcategory = content_for(node, 'subCategory')

  case category
  when 'Firearms'
    case subcategory
    when 'Pistol', 'Revolver'
      product_type = :handgun
    when 'Rifle', 'Rifle Frame', 'Shotgun', 'Short Barrel Rifle'
      product_type = :long_gun
    end
  when 'NFA - Class 3'
    case subcategory
    when 'Suppressors'
      product_type = :suppressor
    end
  end

  {
    name:              content_for(node, 'name'),
    upc:               content_for(node, 'barcod'),
    item_identifier:   content_for(node, 'id'),
    quantity:          content_for(node, 'qty').to_i,
    price:             content_for(node, 'price'),
    map_price:         content_for(node, 'MAP'),
    short_description: content_for(node, 'description'),
    product_type:      product_type,
    category:          category,
    subcategory:       subcategory,
    mfg_number:        content_for(node, 'vendorItemNo'),
    weight:            content_for(node, 'Weight'),
    brand:             content_for(node, 'Brand'),
    caliber:           content_for(node, 'Caliber') || content_for(node, 'Caliber_Gauge'),
    features:          features
  }
end