class Oxforddictionaries::Parser

Parser class

Attributes

word[R]

Public Class Methods

new() click to toggle source
# File lib/oxforddictionaries/parser.rb, line 10
def initialize
  @word, @html, @page = '', '', nil
end

Public Instance Methods

exist?() click to toggle source
# File lib/oxforddictionaries/parser.rb, line 14
def exist?
  first = !(regex =~ single_match).nil?
  second = !(regex =~ plural_matches).nil?
  (first || second)
end
word=(text) click to toggle source
# File lib/oxforddictionaries/parser.rb, line 20
def word=(text)
  @word = text
  @html = read
  @page = Nokogiri::HTML(@html)
end

Private Instance Methods

host() click to toggle source
# File lib/oxforddictionaries/parser.rb, line 28
def host
  'http://www.oxforddictionaries.com/definition/english/'
end
path() click to toggle source
# File lib/oxforddictionaries/parser.rb, line 32
def path
  "#{host}#{word}"
end
plural_matches() click to toggle source
# File lib/oxforddictionaries/parser.rb, line 47
def plural_matches
  tag = @page.search("##{@word}").first
  tag && tag.text
end
read() click to toggle source
# File lib/oxforddictionaries/parser.rb, line 36
def read
  open(path).read
rescue
  nil
end
regex() click to toggle source
# File lib/oxforddictionaries/parser.rb, line 52
def regex
  @regex ||= /.*[Dd]efinitions* of #{@word} in English:/
end
single_match() click to toggle source
# File lib/oxforddictionaries/parser.rb, line 42
def single_match
  tag = @page.search('h1').first
  tag && tag.text
end