Class: UrbanAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/urban_api.rb

Defined Under Namespace

Classes: Definition

Constant Summary collapse

VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection = Faraday.default_connection) ⇒ UrbanAPI

Returns a new instance of UrbanAPI.



28
29
30
31
# File 'lib/urban_api.rb', line 28

def initialize(connection = Faraday.default_connection)
  @conn = connection
  @conn.url_prefix = self.class.urbandictionary_url
end

Class Attribute Details

.urbandictionary_urlObject

Returns the value of attribute urbandictionary_url.



20
21
22
# File 'lib/urban_api.rb', line 20

def urbandictionary_url
  @urbandictionary_url
end

Class Method Details

.define(*args) ⇒ Object



24
25
26
# File 'lib/urban_api.rb', line 24

def self.define(*args)
  new.define(*args)
end

Instance Method Details

#define(word, options = {}) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/urban_api.rb', line 34

def define(word, options = {})
  resp = @conn.get('define.php') do |req|
    req.params['term'] = word
    req.params['page'] = options[:page].to_i if options.key?(:page)
  end
  definitions_from_html resp.body
end

#definitions_from_html(html) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/urban_api.rb', line 42

def definitions_from_html(html)
  defs = []
  (Nokogiri::HTML(html) / "table#entries tr").each do |row|
    if defn = (row / 'div.definition').first
      defs << Definition.create(defn, (row / 'div.example').first)
    end
  end
  defs
end