Class: DDG::Wrapper::Client
- Inherits:
-
Object
- Object
- DDG::Wrapper::Client
- Defined in:
- lib/ddg-wrapper/client.rb
Constant Summary collapse
- API_URL =
'http://api.duckduckgo.com/'
Instance Attribute Summary collapse
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #build_params(query, format = 'json', pretty = '1') ⇒ Object
- #get_data(params) ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #query(query) ⇒ Object
Constructor Details
Instance Attribute Details
#url ⇒ Object
Returns the value of attribute url.
4 5 6 |
# File 'lib/ddg-wrapper/client.rb', line 4 def url @url end |
Instance Method Details
#build_params(query, format = 'json', pretty = '1') ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/ddg-wrapper/client.rb', line 17 def build_params(query, format='json',pretty='1') params = { q: query, format: format, pretty: pretty } params end |
#get_data(params) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/ddg-wrapper/client.rb', line 26 def get_data(params) uri = URI(url) uri.query = URI.encode_www_form(params) res = Net::HTTP.get_response(uri) JSON.parse(res.body) end |
#query(query) ⇒ Object
11 12 13 14 15 |
# File 'lib/ddg-wrapper/client.rb', line 11 def query(query) params = build_params(query) data = get_data(params) data end |