Class: Oddb2xml::StammXML

Inherits:
Object
  • Object
show all
Defined in:
lib/oddb2xml/compare.rb

Constant Summary collapse

V3_NAME_REG =
/_([N,P])_/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#basenameObject (readonly)

Returns the value of attribute basename.



14
15
16
# File 'lib/oddb2xml/compare.rb', line 14

def basename
  @basename
end

#componentsObject

Returns the value of attribute components.



13
14
15
# File 'lib/oddb2xml/compare.rb', line 13

def components
  @components
end

#filenameObject (readonly)

Returns the value of attribute filename.



14
15
16
# File 'lib/oddb2xml/compare.rb', line 14

def filename
  @filename
end

#hashObject (readonly)

Returns the value of attribute hash.



14
15
16
# File 'lib/oddb2xml/compare.rb', line 14

def hash
  @hash
end

#keysObject (readonly)

Returns the value of attribute keys.



14
15
16
# File 'lib/oddb2xml/compare.rb', line 14

def keys
  @keys
end

#sub_key_namesObject (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

#versionObject (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



27
28
29
30
31
32
# File 'lib/oddb2xml/compare.rb', line 27

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



36
37
38
# File 'lib/oddb2xml/compare.rb', line 36

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



47
48
49
50
# File 'lib/oddb2xml/compare.rb', line 47

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



39
40
41
42
43
44
45
46
# File 'lib/oddb2xml/compare.rb', line 39

def get_items(component_name)
  if @version == 3
    items = @hash[component_name]
  else
    items = @hash[component_name].first.values.first
  end
  items
end

#get_limitation_from_v5(item) ⇒ Object



33
34
35
# File 'lib/oddb2xml/compare.rb', line 33

def get_limitation_from_v5(item)
  get_item('PRODUCTS', item['PRODNO'].first.to_i)['LIMNAMEBAG'] ? ['true'] : nil
end

#load_file(name) ⇒ Object



51
52
53
54
# File 'lib/oddb2xml/compare.rb', line 51

def load_file(name)
  Oddb2xml.log_timestamp "Reading #{name} #{(File.size(name)/1024/1024).to_i} MB. This may take some time"
  XmlSimple.xml_in(IO.read(name))
end