Class: ONIX::Helper

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

Class Method Summary collapse

Class Method Details

.arg_to_data(arg) ⇒ Object

convert arbitrary arg to File/String

Parameters:

  • arg (String, File)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/onix/helper.rb', line 5

def self.arg_to_data(arg)
  data = ""
  case arg
  when String
    if File.file?(arg)
      data = File.read(arg)
    else
      data = arg
    end
  when File, Tempfile
    data = arg.read
  end
  data
end

.each_xml_product(arg, &block) ⇒ Object

traverse each product as xml string



21
22
23
24
25
26
27
28
# File 'lib/onix/helper.rb', line 21

def self.each_xml_product(arg, &block)
  data = self.arg_to_data(arg)
  Nokogiri::XML::Reader(data).each do |node|
    if node.name == 'Product' and node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT
      yield(node.outer_xml)
    end
  end
end

.each_xml_product_as_full_message(arg, &block) ⇒ Object

traverse each product as xml string with added ONIXMessage



31
32
33
34
35
# File 'lib/onix/helper.rb', line 31

def self.each_xml_product_as_full_message(arg, &block)
  self.each_xml_product(arg) do |p|
    yield("<ONIXMessage>\n" + p + "\n</ONIXMessage>\n")
  end
end

.strip_html(html) ⇒ String

Parameters:

  • html (String)

Returns:

  • (String)


39
40
41
# File 'lib/onix/helper.rb', line 39

def self.strip_html(html)
  html.gsub(/&nbsp;/, " ").gsub(/<[^>]*(>+|\s*\z)/m, '')
end

.to_date(date_format, date_str_f) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/onix/helper.rb', line 43

def self.to_date(date_format, date_str_f)
  case date_format
  when "00"
    Date.strptime(date_str_f, "%Y%m%d")
  when "01"
    Date.strptime(date_str_f, "%Y%m")
  when "14"
    Time.strptime(date_str_f, "%Y%m%dT%H%M%S")
  else
    nil
  end
end