Class: Ubi::Datum
- Inherits:
-
Object
- Object
- Ubi::Datum
- Defined in:
- lib/ubi/datum.rb
Overview
Suppose to be html reader
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#links ⇒ Object
Returns the value of attribute links.
-
#words ⇒ Object
Returns the value of attribute words.
Instance Method Summary collapse
-
#initialize(data, words, links) ⇒ Datum
constructor
A new instance of Datum.
- #normalize(txt, subs = '') ⇒ Object
- #read_div(div) ⇒ Object
- #read_list(list, args = []) ⇒ Object
- #read_table(table, args = [], subs = '') ⇒ Object
- #xpath(path) ⇒ Object
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
#data ⇒ Object
Returns the value of attribute data.
4 5 6 |
# File 'lib/ubi/datum.rb', line 4 def data @data end |
#links ⇒ Object
Returns the value of attribute links.
4 5 6 |
# File 'lib/ubi/datum.rb', line 4 def links @links end |
#words ⇒ Object
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 |