Module: GeekDict::Youdao

Defined in:
lib/geekdict/youdao/api.rb,
lib/geekdict/youdao/config.rb

Constant Summary collapse

APIKEY =
"1127870837"
APIKEYFROM =
"wbinglee"
APIVERSION =
"1.1"
APIURL =
"http://fanyi.youdao.com/openapi.do"

Class Method Summary collapse

Class Method Details

.translate(word) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/geekdict/youdao/api.rb', line 17

def translate(word)
	@debugger = GeekDict.debugger
	client = HTTPClient.new
	query = { keyfrom: APIKEYFROM, key: APIKEY, type: 'data', doctype: 'json',version: APIVERSION, q: word }
	res = client.get(APIURL, query)
	@debugger.debug "Response status: #{res.status_code}"
	@debugger.debug "Response Content-Type: #{res.content_type}"
	@debugger.debug "Response Body:  #{res.body}"
	res = JSON.parse res.body
	error_code = res['errorCode']
	if error_code == 0 && res['basic'] && res['basic']['explains']
		res['basic']['explains']
	else
		[]
	end
end

.url(word) ⇒ Object



13
14
15
# File 'lib/geekdict/youdao/api.rb', line 13

def url(word)
  "http://dict.youdao.com/search?q=#{word}"
end