class OxfordLearnersDictionaries::English

Attributes

definition[R]
examples[R]
type[R]
urls[R]
word[R]

Public Class Methods

new(word) click to toggle source
# File lib/oxford_learners_dictionaries/english.rb, line 8
def initialize word
  formatted_word = word.strip.gsub(' ', '-') rescue ''
  param_word = formatted_word.gsub('-', '+')
  main_url =  "http://www.oxfordlearnersdictionaries.com/definition/english/#{formatted_word}?q=#{param_word}"

  @urls = [ main_url, main_url.gsub('?q=', '1?q=') ]
  @word = formatted_word
  @definition = Array.new
  self
end

Public Instance Methods

look_up() click to toggle source
# File lib/oxford_learners_dictionaries/english.rb, line 19
def look_up
  @urls.each do |url|
    begin
      @page = Nokogiri::HTML(open(url))
      break
    rescue OpenURI::HTTPError
      @page = nil
    end
  end
  return nil if @page.nil?

  parse
  self
end

Private Instance Methods

parse() click to toggle source
# File lib/oxford_learners_dictionaries/english.rb, line 35
def parse
  @page.css('.idm-gs').remove
  @type = ::OxfordLearnersDictionaries::Type.new(@page).parse

  if @page.css('.num').count > 0
    parse_multiple_definitions
  else
    parse_single_definition
  end
end
parse_multiple_definitions() click to toggle source
# File lib/oxford_learners_dictionaries/english.rb, line 50
def parse_multiple_definitions
  @page.css('.num').count.times do |index|
    @definition.push(::OxfordLearnersDictionaries::Definition.new(@page).parse_multiple_definitions(index))
  end
end
parse_single_definition() click to toggle source
# File lib/oxford_learners_dictionaries/english.rb, line 46
def parse_single_definition
  @definition.push(::OxfordLearnersDictionaries::Definition.new(@page).parse_single_definition)
end