Class: DDG::Wrapper::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ddg-wrapper/client.rb

Constant Summary collapse

API_URL =
'http://api.duckduckgo.com/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



7
8
9
# File 'lib/ddg-wrapper/client.rb', line 7

def initialize
	@url = API_URL
end

Instance Attribute Details

#urlObject

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