Class: CatalogParser

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

Constant Summary collapse

ITEM_SEPARATOR =
"\r\n"
VALUE_SEPARATOR =
"\t"
VALUES =
{:x => [:version, :prev_version, :form_date, :money], 
:v => [:id, :name], 
:e => [:id, :provider_id, :name, :money_restriction, :price, :agent_max_value, :agent_value, :client_attributes, :client_attributes_regexp],
:p => [:body] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCatalogParser

Returns a new instance of CatalogParser.



4
5
6
# File 'lib/catalog_parser.rb', line 4

def initialize()
  @catalog = { :v => [], :x => [], :e => [], :p => [] }
end

Instance Attribute Details

#catalogObject

Returns the value of attribute catalog.



2
3
4
# File 'lib/catalog_parser.rb', line 2

def catalog
  @catalog
end

Instance Method Details

#new_catalog(data) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/catalog_parser.rb', line 18

def new_catalog(data)   
  ic = Iconv.new('UTF-8', 'WINDOWS-1251')
  strings = data.split(ITEM_SEPARATOR)
  strings[1..-1].each do |string|
    values = string.split(VALUE_SEPARATOR)
    type = ic.iconv(values[0]).to_s
    key = type.last.to_sym
    if ["+v", "+x", "+e", "+p"].include?(type)
      hash = {}
      VALUES[key].each_with_index{|o, index| hash[VALUES[key][index]] = ic.iconv(values[index+1]).to_s}
      @catalog[key] << hash      
    end
  end
 @catalog
end