Class: Oddb2xml::StammXML
- Inherits:
-
Object
- Object
- Oddb2xml::StammXML
- Defined in:
- lib/oddb2xml/compare.rb
Constant Summary collapse
- V3_NAME_REG =
/_([N,P])_/
Instance Attribute Summary collapse
-
#basename ⇒ Object
readonly
Returns the value of attribute basename.
-
#components ⇒ Object
Returns the value of attribute components.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
-
#sub_key_names ⇒ Object
readonly
Returns the value of attribute sub_key_names.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
- #get_field_from_v5_product(item, field_name) ⇒ Object
- #get_item(component_name, id) ⇒ Object
- #get_items(component_name) ⇒ Object
- #get_limitation_from_v5(item) ⇒ Object
-
#initialize(filename, components = ["ITEMS"]) ⇒ StammXML
constructor
A new instance of StammXML.
- #load_file(name) ⇒ Object
Constructor Details
#initialize(filename, components = ["ITEMS"]) ⇒ StammXML
Returns a new instance of StammXML.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/oddb2xml/compare.rb', line 15 def initialize(filename, components = ["ITEMS"]) raise "File #{filename} must exist" unless File.exist?(filename) @filename = filename @basename = File.basename(filename) @version = V3_NAME_REG.match(filename) ? 3 : 5 @components = components if @version == 5 @hash = load_file(@filename) else raise "Unsupported version #{@version}" end end |
Instance Attribute Details
#basename ⇒ Object (readonly)
Returns the value of attribute basename.
14 15 16 |
# File 'lib/oddb2xml/compare.rb', line 14 def basename @basename end |
#components ⇒ Object
Returns the value of attribute components.
13 14 15 |
# File 'lib/oddb2xml/compare.rb', line 13 def components @components end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
14 15 16 |
# File 'lib/oddb2xml/compare.rb', line 14 def filename @filename end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
14 15 16 |
# File 'lib/oddb2xml/compare.rb', line 14 def hash @hash end |
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
14 15 16 |
# File 'lib/oddb2xml/compare.rb', line 14 def keys @keys end |
#sub_key_names ⇒ Object (readonly)
Returns the value of attribute sub_key_names.
14 15 16 |
# File 'lib/oddb2xml/compare.rb', line 14 def sub_key_names @sub_key_names end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
14 15 16 |
# File 'lib/oddb2xml/compare.rb', line 14 def version @version end |
Class Method Details
.get_component_key_name(component_name) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/oddb2xml/compare.rb', line 28 def self.get_component_key_name(component_name) return "LIMNAMEBAG" if /LIMITATION/i.match?(component_name) return "PRODNO" if /PRODUCT/i.match?(component_name) return "GTIN" if /ITEM/i.match?(component_name) raise "Cannot determine keyname for component #{component_name}" end |
Instance Method Details
#get_field_from_v5_product(item, field_name) ⇒ Object
39 40 41 |
# File 'lib/oddb2xml/compare.rb', line 39 def get_field_from_v5_product(item, field_name) get_item("PRODUCTS", item["PRODNO"].first.to_i)[field_name] end |
#get_item(component_name, id) ⇒ Object
51 52 53 54 |
# File 'lib/oddb2xml/compare.rb', line 51 def get_item(component_name, id) keyname = StammXML.get_component_key_name(component_name) get_items(component_name).find { |item| item[keyname].first.to_i == id } end |
#get_items(component_name) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/oddb2xml/compare.rb', line 43 def get_items(component_name) if @version == 3 @hash[component_name] else @hash[component_name].first.values.first end end |
#get_limitation_from_v5(item) ⇒ Object
35 36 37 |
# File 'lib/oddb2xml/compare.rb', line 35 def get_limitation_from_v5(item) get_item("PRODUCTS", item["PRODNO"].first.to_i)["LIMNAMEBAG"] ? ["true"] : nil end |
#load_file(name) ⇒ Object
56 57 58 59 |
# File 'lib/oddb2xml/compare.rb', line 56 def load_file(name) Oddb2xml. "Reading #{name} #{(File.size(name) / 1024 / 1024).to_i} MB. This may take some time" XmlSimple.xml_in(IO.read(name)) end |