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

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)


101
102
103
# File 'lib/ud.rb', line 101

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)


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

Parameters:

  • term (String)

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

Returns:

  • (Nil)


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>

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>)


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.

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>)


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

Parameters:

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

    options.



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.

Parameters:

  • term (String)

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

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

    options.

Returns:

  • (String)


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

.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.0"
end