Class: DuckDuckGo
- Inherits:
-
Object
- Object
- DuckDuckGo
- Defined in:
- lib/rubyduck.rb
Overview
Search for things on DuckDuckGo.com.
Instance Method Summary collapse
-
#describe(q) ⇒ Object
Give a short description about the query.
-
#initialize ⇒ DuckDuckGo
constructor
A new instance of DuckDuckGo.
-
#json_query(q) ⇒ Object
Return the JSON-encoded Hash given to us when we search for
q
.
Constructor Details
#initialize ⇒ DuckDuckGo
Returns a new instance of DuckDuckGo.
8 9 10 11 |
# File 'lib/rubyduck.rb', line 8 def initialize() @con = Net::HTTP.new('api.duckduckgo.com', 443) @con.use_ssl = true end |
Instance Method Details
#describe(q) ⇒ Object
Give a short description about the query. Return nil if we don’t know anything about it.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/rubyduck.rb', line 15 def describe(q) res = json_query(q) if not(res["AbstractText"].empty?) return res["AbstractText"] elsif (_=res["RelatedTopics"][0]) and _["Text"] return _["Text"] else return nil end end |
#json_query(q) ⇒ Object
Return the JSON-encoded Hash given to us when we search for q
.
30 31 32 33 34 35 36 |
# File 'lib/rubyduck.rb', line 30 def json_query(q) # 'o' specifies the output format; 'p' determines whether or not safe search # is on (-1 is off, 1 is on); 'q' is the query string. req_path = '/?' + URI.encode_www_form(q: q, p: -1, o: 'json') res_json = @con.get(req_path).body JSON.parse(res_json) end |