Sha256: 0b9ae50c2d69bb40b1fdfc05fd6bcc795b401539c3c2dc7ea528c7dd6908d1ef

Contents?: true

Size: 746 Bytes

Versions: 1

Compression:

Stored size: 746 Bytes

Contents

require 'nokogiri'
require 'open-uri'

module UrbanDictionary
  class Word
    attr_reader :word, :entries

    # Can raise SocketError if unable to connect to specified URL
    def self.from_url(url)
      html = open(url).read
      doc = Nokogiri::HTML(html)

      word = doc.css('.word').first.content.strip
      definitions = doc.css('.definition').map{|d| d.content.strip }
      examples = doc.css('.example').map{|e| e.content.strip }
      entries = definitions.zip(examples).map{|d,e| Entry.new(d, e)}

      Word.new(word, entries)
    end

    def initialize(word, entries)
      @word = word
      @entries = entries
    end

    def to_s
      @word
    end

    def size
      @word.size
    end
    alias :length :size
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
urban_dictionary-0.0.1 lib/urban_dictionary/word.rb