Class: Baidu::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/baidu/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response = nil) ⇒ Response

Returns a new instance of Response.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/baidu/response.rb', line 7

def initialize(http_response=nil)
	if http_response.nil?
		@result = false
	else
		body = JSON.parse(http_response.body)
		if http_response.code.to_i == 200
			# success
			@result = true
			@request_id = body["request_id"]
			@response_params = body["response_params"]
		else
			# failed
			@result = false
			@request_id = body["request_id"]
			@error_code = body["error_code"]
			@error_msg = body["error_msg"]
		end
	end
end

Instance Attribute Details

#error_codeObject (readonly)

Returns the value of attribute error_code.



5
6
7
# File 'lib/baidu/response.rb', line 5

def error_code
  @error_code
end

#error_msgObject (readonly)

Returns the value of attribute error_msg.



5
6
7
# File 'lib/baidu/response.rb', line 5

def error_msg
  @error_msg
end

#request_idObject (readonly)

Returns the value of attribute request_id.



5
6
7
# File 'lib/baidu/response.rb', line 5

def request_id
  @request_id
end

#response_paramsObject (readonly)

Returns the value of attribute response_params.



5
6
7
# File 'lib/baidu/response.rb', line 5

def response_params
  @response_params
end

#resultObject (readonly)

Returns the value of attribute result.



5
6
7
# File 'lib/baidu/response.rb', line 5

def result
  @result
end

Instance Method Details

#to_jsonHash

to_json

Returns:

  • (Hash)

    返回一个Hash



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/baidu/response.rb', line 30

def to_json
	if @result
		{
			result: @result,
			request_id: @request_id,
			response_params: @response_params
		}
	else
		{
			result: @result,
			request_id: @request_id,
			error_code: @error_code,
			error_msg: @error_msg
		}
	end
end