class Meaning::MeaningLab

Attributes

dictionary[R]

Public Class Methods

new(word) click to toggle source
# File lib/meaning.rb, line 7
def initialize word
        word =  word.dup.capitalize
        @dictionary = {word: word}
        define
end

Private Instance Methods

api() click to toggle source
# File lib/meaning.rb, line 63
def api
        "http://dictionary.cambridge.org/dictionary/english/#{dictionary[:word].downcase}"
end
define() click to toggle source
# File lib/meaning.rb, line 14
def define
        if get_the_content
                sound
                fetch_for("definitions",".def")
                fetch_for("examples",".eg")
        end 
end
fetch_for(name,css_class) click to toggle source
# File lib/meaning.rb, line 34
def fetch_for name,css_class
        elements = []
        @doc.css(css_class).each do |element|
                elements  << sanatize_from_colon(element.text)
        end
        @dictionary[name.to_sym]  = elements unless elements.empty?
end
file() click to toggle source
# File lib/meaning.rb, line 60
def file
        open(api)
end
get_the_content() click to toggle source
# File lib/meaning.rb, line 45
def get_the_content
        load_page
        begin
                sound
                true
        rescue
                @dictionary[:error] =  "Not even a word"
                false
        end
end
load_page() click to toggle source
# File lib/meaning.rb, line 56
def load_page
        @doc ||= Nokogiri::HTML(file)
end
sanatize(text) click to toggle source
# File lib/meaning.rb, line 26
def sanatize text
        text.gsub!(/(\t)+|(\n)+|\s{2,}|\/+|#{word.capitalize}|#{word.downcase}/,"")
end
sanatize_from_colon(text) click to toggle source
# File lib/meaning.rb, line 41
def sanatize_from_colon text
        text.gsub!(":",".")
        text
end
sound() click to toggle source
# File lib/meaning.rb, line 22
def sound
        @dictionary[:sound] = sanatize(@doc.css(".pos-header").first.text)
end
word() click to toggle source
# File lib/meaning.rb, line 30
def word
        dictionary[:word]
end