Class: OEIS::OEISParser

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

Overview

Your code goes here…

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ls) ⇒ OEISParser

Returns a new instance of OEISParser.



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

def initialize(ls)
  url = BASE_URL + ls.join(',')
  begin
    doc = Nokogiri::HTML(open(url))
  rescue SocketError
    puts 'Connection could not be established. Check your internet connection.'
  end
  
  first_result = doc.css('table table:eq(2)').first
  info = first_result.css('tr:eq(3) table > tr > td > a').first
  
  if info.nil?
    puts 'No sequence found.'
    return nil
  end
  
  @id = info.attributes['href'].value[1..-1]
  @title = info.attributes['title'].value
  
  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.



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

def id
  @id
end

#sequenceObject

Returns the value of attribute sequence.



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

def sequence
  @sequence
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#to_sObject



36
37
38
# File 'lib/oeis.rb', line 36

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