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



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

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



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

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



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

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

.mandatory_text_at(n, xpath) ⇒ Object



44
45
46
# File 'lib/onix/helper.rb', line 44

def self.mandatory_text_at(n,xpath)
  self.text_at(n,xpath)
end

.strip_html(html) ⇒ Object



48
49
50
# File 'lib/onix/helper.rb', line 48

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

.text_at(n, xpath) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/onix/helper.rb', line 36

def self.text_at(n,xpath)
  if n.at_xpath(xpath)
    n.at_xpath(xpath).text.strip
  else
    nil
  end
end

.to_date(date_format, date_str_f) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/onix/helper.rb', line 52

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