Class: DogShoe::Table

Inherits:
Base
  • Object
show all
Defined in:
lib/dog_shoe/table.rb

Instance Attribute Summary

Attributes inherited from Base

#doc

Class Method Summary collapse

Methods inherited from Base

#[], fetch, #initialize, #method_missing, parse, values

Constructor Details

This class inherits a constructor from DogShoe::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class DogShoe::Base

Class Method Details

.css(selector, args = []) ⇒ Object



13
14
15
16
# File 'lib/dog_shoe/table.rb', line 13

def css(selector, args=[])
  out = super(selector)#, *args)
  table_to_magic(out, *args)
end

.find(attrs) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/dog_shoe/table.rb', line 5

def find(attrs)
  @doc = parse(fetch(attrs[:url]))
  css(attrs[:css])
  @values.collect do |result|
    new(values: result, keys: @keys)
  end
end

.scrub(text) ⇒ Object



42
43
44
# File 'lib/dog_shoe/table.rb', line 42

def scrub(text)
  text.gsub("\n",'').gsub(/^\s*/,'').gsub(/\s*$/,'')
end

.table_to_magic(table, *args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dog_shoe/table.rb', line 18

def table_to_magic(table, *args)
  trs = table.css('tr')

  @keys = table.css('th').collect do |td|
    scrub(td.text).gsub(/[^0-9a-zA-Z\s]/,'').gsub(/\s/,'_').downcase
  end 
  
  @values = trs.collect do |tr|
    tr.css('td').collect do |td|
      td.text
    end
  end.reject{|tr| tr.empty? }

  @values = @values.collect do |trs|
    trs.collect do |td|
      #td =~ /[0-9]/ ? 
      td.gsub(/[^0-9]/,'').to_f 
      #: td
    end
  end if args.include?(:numeric)
  #throw DogShoe::NotFound if nothing found
  @values
end