Class: OEIS::Parser

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

Constant Summary collapse

BASE_URL =
'http://oeis.org/search?q='

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ls) ⇒ Parser

Returns a new instance of Parser.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/oeis.rb', line 12

def initialize(ls)
  url = BASE_URL + ls.join(',')
  doc = Nokogiri::HTML(open(url))

  first_result = doc.css('table table:eq(2)').first
  info = first_result.css('tr:eq(3) table > tr').first

  if info.nil?
    puts 'No sequence found.'
    return nil
  end

  @id = info.css('td:eq(1) > a').text.strip
  @title = info.css('td:eq(3)').text.strip

  digits = first_result.css('tr:eq(4) table tr tt').text.split(', ')
  @sequence = digits.map &:to_i
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/oeis.rb', line 10

def id
  @id
end

#sequenceObject

Returns the value of attribute sequence.



10
11
12
# File 'lib/oeis.rb', line 10

def sequence
  @sequence
end

#titleObject

Returns the value of attribute title.



10
11
12
# File 'lib/oeis.rb', line 10

def title
  @title
end

Instance Method Details

#to_sObject



31
32
33
# File 'lib/oeis.rb', line 31

def to_s
  "#{@id}: #{@title}"
end