lib/urban/web.rb in urban-1.0.0 vs lib/urban/web.rb in urban-2.0.0
- old
+ new
@@ -1,36 +1,48 @@
require 'open-uri'
-require 'urban/version'
module Urban
- module Web
- extend self
+ class Web
Response = Struct.new(:url, :stream)
- URL = 'http://www.urbandictionary.com'
-
def search(phrase)
- result = fetch "define.php", :term => phrase
- Response.new(result.base_uri.to_s, result)
+ build_response fetch :define, :term => phrase
end
def random
- result = fetch "random.php"
- Response.new(result.base_uri.to_s, result)
+ build_response fetch :random
end
- def fetch(page, parameters = {})
- params = '?' + parameters.map { |k,v| "#{k}=#{v}" }.join('&') unless parameters.empty?
- open(escape_uri("#{URL}/#{page}#{params}"))
+ private
+
+ def fetch(*args)
+ open build_uri(*args)
end
- private
+ def build_response(response)
+ Response.new response.base_uri.to_s, response
+ end
+
+ def build_uri(page, params = nil)
+ query = build_query(params) unless params.nil?
+ escape_uri "#{url}/#{page}.php#{query}"
+ end
+
+ def build_query(parameters)
+ "?" + parameters.map { |k,v| "#{k}=#{v}" }.join("&")
+ end
+
def escape_uri(uri)
if RUBY_VERSION > '1.9'
- URI::Parser.new.escape(uri)
+ URI::Parser.new.escape uri
else
- URI.escape(uri)
+ URI.escape uri
end
end
+
+ def url
+ "http://www.urbandictionary.com"
+ end
+
end
end