Module: UD
- Defined in:
- lib/ud.rb,
lib/ud/formatting.rb
Overview
! /usr/bin/env ruby -*- coding: UTF-8 -*-
Defined Under Namespace
Modules: Formatting
Class Method Summary collapse
-
.format_results(results, color = true) ⇒ Object
Format results for output [results] this must be an array of results, as returned by
UD.query. -
.query(term, *opts) ⇒ Object
Query the website and return a list of definitions for the provided term.
-
.search_url(term = '') ⇒ Object
Get the search URL to query for a given term.
-
.text(el) ⇒ Object
Get the text of an element.
-
.thumbs(ids) ⇒ Object
Get the thumbs (up/down) for a list of definitions’ ids.
-
.version ⇒ Object
The current version of the module.
Class Method Details
.format_results(results, color = true) ⇒ Object
Format results for output
- results
-
this must be an array of results, as returned by
UD.query.
111 112 113 |
# File 'lib/ud.rb', line 111 def UD.format_results(results, color=true) UD::Formatting.text(results, color) end |
.query(term, *opts) ⇒ Object
Query the website and return a list of definitions for the provided term. This list may be empty if there’s no result. It only scraps the first page of results.
- term
-
the term to search for
- opts
-
options. This is used by the command-line tool.
:countis the
maximum number of results to return, :ratio is the minimum upvotes/downvotes ratio. Other options may be added in the future.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ud.rb', line 63 def UD.query(term, *opts) opts = {:count => 1, :ratio => 0.0}.merge(opts[0] || {}) return [] if opts[:count] <= 0 url = search_url(term) doc = Nokogiri::HTML(open(url)) return [] unless doc.css('#not_defined_yet').empty? words = doc.css('#entries .box') ids = words.take(opts[:count]).map do |w| w.css('.thumb.up').first.attr('data-defid') end thumbs = thumbs(ids) if opts[:ratio] > 0 ids.delete_if do |id| t = thumbs[id.to_i] || {:up => 1, :down => 1} (t[:up] / t[:down].to_f) < opts[:ratio] end end ids.map do |id| box = doc.css(".add_to_list[data-defid=\"#{id}\"]").first.parent word = text box.css(".word > a").first t = thumbs[id.to_i] || {} { :id => id, :word => word, :definition => text(box.css('.definition')), :example => text(box.css('.example')), :upvotes => t[:up], :downvotes => t[:down] } end end |
.search_url(term = '') ⇒ Object
Get the search URL to query for a given term.
- term
-
the term to search for. It must be a string, spaces are allowed
22 23 24 25 |
# File 'lib/ud.rb', line 22 def UD.search_url(term='') param = URI.encode_www_form('term' => term) "http://www.urbandictionary.com/define.php?#{param}" end |
.text(el) ⇒ Object
Get the text of an element. This is an helper for internal usage.
50 51 52 53 54 |
# File 'lib/ud.rb', line 50 def UD.text(el) el.text.strip.gsub(/\r/, "\n") rescue '' end |
.thumbs(ids) ⇒ Object
Get the thumbs (up/down) for a list of definitions’ ids. This is an helper for internal usage.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ud.rb', line 29 def UD.thumbs(ids) param = URI.encode_www_form('ids' => ids.join(',')) json = open "http://api.urbandictionary.com/v0/uncacheable?#{param}" response = JSON.parse(json.read) thumbs = {} response['thumbs'].each do |t| thumbs[t['defid']] = { :up => t['thumbs_up'], :down => t['thumbs_down'] } end thumbs end |
.version ⇒ Object
The current version of the module
16 17 18 |
# File 'lib/ud.rb', line 16 def UD.version '0.1.3' end |