Module: UD
- Defined in:
- lib/ud.rb,
lib/ud/formatting.rb
Overview
This module provide some methods to scrape definitions from the Urban Dictionary website.
Defined Under Namespace
Modules: Formatting
Class Method Summary collapse
-
.format_results(results, color = true) ⇒ String
Format results for output.
-
.open_random ⇒ Nil
Open a random definition URL in the user’s browser.
-
.open_url(term) ⇒ Nil
Open the search URL in the user’s browser.
- .parse_response(text, opts = {}) ⇒ Array<Hash>
-
.query(term, opts = {}) ⇒ Array<Hash>
Query the website and return a list of definitions for the provided term.
- .random(opts = {}) ⇒ Object
- .random_url(opts = {}) ⇒ Object
-
.search_url(term, opts = {}) ⇒ String
Get the search URL to query for a given term.
-
.version ⇒ String
The current gem’s version.
Class Method Details
.format_results(results, color = true) ⇒ String
Format results for output
101 102 103 |
# File 'lib/ud.rb', line 101 def format_results(results, color = true) UD::Formatting.text(results, color) end |
.open_random ⇒ Nil
Open a random definition URL in the user’s browser
52 53 54 |
# File 'lib/ud.rb', line 52 def open_random system open_cmd, random_url(:api => false) end |
.open_url(term) ⇒ Nil
Open the search URL in the user’s browser
46 47 48 |
# File 'lib/ud.rb', line 46 def open_url(term) system open_cmd, search_url(term, :api => false) end |
.parse_response(text, opts = {}) ⇒ Array<Hash>
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ud.rb', line 73 def parse_response(text, opts = {}) opts = { :count => 1 }.merge(opts || {}) return [] if opts[:count] <= 0 resp = JSON.parse(text, :symbolize_names => true) results = resp[:list].map do |res| { :id => res[:defid], :word => res[:word], :author => res[:author], :permalink => res[:permalink], :definition => res[:definition].strip, :example => res[:example].strip, :upvotes => res[:thumbs_up], :downvotes => res[:thumbs_down], } end results.take opts[:count] end |
.query(term, opts = {}) ⇒ Array<Hash>
Query the website and return a list of definitions for the provided term. This list may be empty if there’s no result.
62 63 64 |
# File 'lib/ud.rb', line 62 def query(term, opts = {}) parse_response(open(search_url(term)).read, opts) end |
.random(opts = {}) ⇒ Object
66 67 68 |
# File 'lib/ud.rb', line 66 def random(opts = {}) parse_response(open(random_url).read, opts) end |
.random_url(opts = {}) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/ud.rb', line 34 def random_url(opts = {}) if opts[:api] != false "http://api.urbandictionary.com/v0/random" else "http://www.urbandictionary.com/random.php" end end |
.search_url(term, opts = {}) ⇒ String
Get the search URL to query for a given term.
23 24 25 26 27 28 29 30 31 |
# File 'lib/ud.rb', line 23 def search_url(term, opts = {}) param = URI.encode_www_form("term" => term) if opts[:api] != false "http://api.urbandictionary.com/v0/define?#{param}" else "http://www.urbandictionary.com/define.php?#{param}" end end |
.version ⇒ String
Returns the current gem’s version.
14 15 16 |
# File 'lib/ud.rb', line 14 def version "0.3.0" end |