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

Class Method Details

.format_results(results, color = true) ⇒ String

Format results for output

Parameters:

  • results (Array)

    this must be an array of results, as returned by UD.query.

  • color (Boolean) (defaults to: true)

    colored output

Returns:

  • (String)


108
109
110
# File 'lib/ud.rb', line 108

def format_results(results, color = true)
  UD::Formatting.text(results, color)
end

.open_randomNil

Open a random definition URL in the user’s browser

Returns:

  • (Nil)


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

Parameters:

  • term (String)

    the term to search for. It must be a string, spaces are allowed.

Returns:

  • (Nil)


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.

Parameters:

  • opts (Hash) (defaults to: {})

    options. This is used by the command-line tool. :count is the maximum number of results to return

Returns:

  • (Array<Hash>)


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.

Parameters:

  • term (String)

    the term to search for

  • opts (Hash) (defaults to: {})

    options. This is used by the command-line tool. :count is the maximum number of results to return

Returns:

  • (Array<Hash>)


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

Parameters:

  • opts (Hash) (defaults to: {})

    options.



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.

Parameters:

  • opts (Hash) (defaults to: {})

    options.



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.

Parameters:

  • term (String)

    the term to search for. It must be a string, spaces are allowed.

  • opts (Hash) (defaults to: {})

    options.

Returns:

  • (String)


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

.versionString

Returns the current gem’s version.

Returns:

  • (String)

    the current gem’s version



14
15
16
# File 'lib/ud.rb', line 14

def version
  "0.3.1"
end