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
Constant Summary collapse
- API_ROOT =
"https://api.urbandictionary.com/v0"
- WWW_ROOT =
"https://www.urbandictionary.com"
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>
Parse a response from the Urban Dictionnary website.
-
.query(term, opts = {}) ⇒ Array<Hash>
Query the website and return a list of definitions for the provided term.
-
.random(opts = {}) ⇒ Object
Return a random definition.
-
.random_url(opts = {}) ⇒ Object
Return a URL for a random definition.
-
.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
108 109 110 |
# File 'lib/ud.rb', line 108 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
56 57 58 |
# File 'lib/ud.rb', line 56 def open_random system open_cmd, random_url(:api => false) end |
.open_url(term) ⇒ Nil
Open the search URL in the user’s browser
50 51 52 |
# File 'lib/ud.rb', line 50 def open_url(term) system open_cmd, search_url(term, :api => false) end |
.parse_response(text, opts = {}) ⇒ Array<Hash>
Parse a response from the Urban Dictionnary website.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ud.rb', line 80 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.
66 67 68 |
# File 'lib/ud.rb', line 66 def query(term, opts = {}) parse_response(open(search_url(term)).read, opts) end |
.random(opts = {}) ⇒ Object
Return a random definition
72 73 74 |
# File 'lib/ud.rb', line 72 def random(opts = {}) parse_response(open(random_url).read, opts) end |
.random_url(opts = {}) ⇒ Object
Return a URL for a random definition.
38 39 40 41 42 43 44 |
# File 'lib/ud.rb', line 38 def random_url(opts = {}) if opts[:api] != false "#{API_ROOT}/random" else "#{WWW_ROOT}/random.php" end end |
.search_url(term, opts = {}) ⇒ String
Get the search URL to query for a given term.
26 27 28 29 30 31 32 33 34 |
# File 'lib/ud.rb', line 26 def search_url(term, opts = {}) param = URI.encode_www_form("term" => term) if opts[:api] != false "#{API_ROOT}/define?#{param}" else "#{WWW_ROOT}/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.1" end |