Class: Ubi::Datum

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

Overview

Suppose to be html reader

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, words, links) ⇒ Datum

Returns a new instance of Datum.



6
7
8
9
10
11
# File 'lib/ubi/datum.rb', line 6

def initialize(data, words, links)
  # binding.pry
  @data = data
  @words = data.xpath(words).text
  @links = data.xpath(links).map { |a| a.values.join(' ') }
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

Returns the value of attribute links.



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

def links
  @links
end

#wordsObject

Returns the value of attribute words.



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

def words
  @words
end

Instance Method Details

#normalize(txt, subs = '') ⇒ Object



33
34
35
# File 'lib/ubi/datum.rb', line 33

def normalize(txt, subs = '')
  txt.to_s.gsub(subs, '').strip.chomp
end

#read_div(div) ⇒ Object



17
18
19
# File 'lib/ubi/datum.rb', line 17

def read_div(div)
  data.xpath("//#{div}").text
end

#read_list(list, args = []) ⇒ Object



21
22
23
24
# File 'lib/ubi/datum.rb', line 21

def read_list(list, args = [])
  s = struct_for(args)
  data.xpath(list).map { |i| s.new(*i.xpath) }
end

#read_table(table, args = [], subs = '') ⇒ Object



26
27
28
29
30
31
# File 'lib/ubi/datum.rb', line 26

def read_table(table, args = [], subs = '')
  s = struct_for(*args)
  data.xpath(table).map do |r|
    s.new(*r.xpath('td/text()').map { |t| normalize(t, subs) })
  end
end

#xpath(path) ⇒ Object



13
14
15
# File 'lib/ubi/datum.rb', line 13

def xpath(path)
  data.xpath(path)
end