Module: OpenDMM::Utils

Defined in:
lib/opendmm/utils.rb

Class Method Summary collapse

Class Method Details

.force_utf8(content) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/opendmm/utils.rb', line 64

def self.force_utf8(content)
  # This is to get rid of the annoying error message:
  #   'encoding error : input conversion failed due to input error'
  $stderr.reopen('/dev/null', 'w')
  encoding = Nokogiri::HTML(content).encoding
  $stderr = STDERR
  content = content.encode('UTF-8', encoding, invalid: :replace, undef: :replace, replace: '')
end

.hash_by_split(array, delimiter = /[::]/) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/opendmm/utils.rb', line 56

def self.hash_by_split(array, delimiter = /[::]/)
  array.map do |str|
    slices = str.split(delimiter, 2).map(&:squish)
  end.select do |pair|
    pair.size == 2
  end.to_h
end

.hash_from_dl(dl) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/opendmm/utils.rb', line 40

def self.hash_from_dl(dl)
  dts = dl.css('dt').map(&:text).map(&:squish)
  dds = dl.children.select do |node|
    node.name == 'dt' || node.name == 'dd'
  end.split do |node|
    node.name == 'dt'
  end[1..-1].map do |nodes|
    node_set = Nokogiri::XML::NodeSet.new(dl.document)
    nodes.each do |node|
      node_set << node
    end
    node_set
  end
  Hash[dts.zip(dds)]
end