Class: UrbanLexicophile::Reader
- Inherits:
-
Object
- Object
- UrbanLexicophile::Reader
- Defined in:
- lib/urbanlexicophile/reader.rb
Instance Method Summary collapse
- #build_definition_object(def_array) ⇒ Object
- #get_all_definitions ⇒ Object
- #is_definition_row?(row) ⇒ Boolean
- #is_index_row?(row) ⇒ Boolean
- #lookup_term(term) ⇒ Object
- #parse_definition_row(row) ⇒ Object
- #parse_title_row(row) ⇒ Object
- #term_defined? ⇒ Boolean
Instance Method Details
#build_definition_object(def_array) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/urbanlexicophile/reader.rb', line 105 def build_definition_object(def_array) title_details = def_array.first definition_details = def_array.last definition = Definition.new definition.title = title_details[:title] definition. = definition_details[:author] definition.definition = definition_details[:definition] definition.example = definition_details[:example] return definition end |
#get_all_definitions ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/urbanlexicophile/reader.rb', line 35 def get_all_definitions table = @ng.css("#entries") # a definition takes up at least 2 <tr> and has no semantic separator between definitions # to make it even harder, some definitions have more than 2 <tr>s # this is a best attempt at discovering definitions, and passing them to get_one_definition for parsing finished_definitions = [] definition_in_progress = [] table.css("tr").each do |row| # check if the row is an index. If it is, clear out the in_progress definition and parese the first line if is_index_row? row definition_in_progress = [] definition_in_progress << parse_title_row(row) elsif is_definition_row? row definition_in_progress << parse_definition_row(row) finished_definitions << build_definition_object(definition_in_progress) end end return finished_definitions # should be an array of complete Definition objects end |
#is_definition_row?(row) ⇒ Boolean
82 83 84 85 86 87 88 |
# File 'lib/urbanlexicophile/reader.rb', line 82 def is_definition_row?(row) if row.css("div.definition").count > 0 return true else return false end end |
#is_index_row?(row) ⇒ Boolean
74 75 76 77 78 79 80 |
# File 'lib/urbanlexicophile/reader.rb', line 74 def is_index_row?(row) if row.css("td.index").count > 0 return true else return false end end |
#lookup_term(term) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/urbanlexicophile/reader.rb', line 6 def lookup_term(term) @address = "http://www.urbandictionary.com/define.php?term=" url = @address + term.gsub(" ", "+") definitions = [] # perform the search @ng = Nokogiri::HTML(open(url+"&page=1")) #determine if search has a defined term if self.term_defined? #get definitions from first page of definitions definitions = get_all_definitions # same again for check for more pages page = 1 while @ng.css("a.next_page").count > 0 page += 1 @ng = Nokogiri::HTML(open(url+"&page=#{page.to_s}")) definitions += get_all_definitions end end return definitions end |
#parse_definition_row(row) ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/urbanlexicophile/reader.rb', line 96 def parse_definition_row(row) definition = row.css(".definition").first.content.to_s.strip example = row.css(".example").first.content.to_s.strip = row.css(".greenery a.author").first.content.to_s.strip return {:definition => definition, :example => example, :author => } end |
#parse_title_row(row) ⇒ Object
90 91 92 93 94 |
# File 'lib/urbanlexicophile/reader.rb', line 90 def parse_title_row(row) title = row.css(".word").first.content.to_s.strip return {:title => title} end |
#term_defined? ⇒ Boolean
66 67 68 69 70 71 72 |
# File 'lib/urbanlexicophile/reader.rb', line 66 def term_defined? if @ng.css("#not_defined_yet").count > 1 return false else return true end end |