class Sogou::Search::Api::Core::ApiCommand
Attributes
operation[RW]
options[RW]
params[RW]
Public Class Methods
new(operation)
click to toggle source
# File lib/sogou/search/api/core/api_command.rb, line 17 def initialize(operation) @operation = operation @params = {} @options = {} end
Public Instance Methods
check_error_code(header, raise_error = true)
click to toggle source
More error codes and descriptions can be found here apihome.sogou.com/document/ss/doc11.jsp
# File lib/sogou/search/api/core/api_command.rb, line 60 def check_error_code(header, raise_error = true) res_header = header.is_a?(Array) ? header[0] : header error_klass = case res_header['code'].to_i when 6 # Invalid username InvalidUserNameError when 8 # Wrong password WrongPasswordError when 10 # Token is invalid InvalidTokenError when 18 # Insufficient quotas RateLimitError when 1000011 # Plan ID does not exist PlanIDNotExistError when 1000012 # Promotion group ID does not exist PromotionGroupIDNotExistError when 1000013 # Keyword ID does not exist KeywordIDNotExistError else UnknownError end exception = error_klass.new( res_header.fetch('message', 'Unknown error'), code: res_header['code'], header: header ) raise_error ? (raise exception) : exception end
check_status(response)
click to toggle source
# File lib/sogou/search/api/core/api_command.rb, line 41 def check_status(response) logger.debug("Response Header - #{response.header}") res_header = response.header['res_header'] check_error_code(res_header['failures']) if res_header['desc'] != 'success' end
decode_response_body(body)
click to toggle source
# File lib/sogou/search/api/core/api_command.rb, line 47 def decode_response_body(body) result = body["#{@operation}_response"] if result result = result.is_a?(Hash) ? result.values[0] : result result = convert_regions_to_string(result) if options[:convert_regions_to_string] result end end
execute(client, &block)
click to toggle source
# File lib/sogou/search/api/core/api_command.rb, line 23 def execute(client, &block) logger.debug("Executing SOAP #{operation}") resp = client.call(@operation, message: @params) result = process_response(resp) logger.debug("Success - #{result}") success(result, &block) rescue => e logger.debug("Error - #{e.inspect}") error(e, &block) end
process_response(response)
click to toggle source
# File lib/sogou/search/api/core/api_command.rb, line 36 def process_response(response) check_status(response) decode_response_body(response.body) end
Private Instance Methods
error(err) { |nil, err| ... }
click to toggle source
# File lib/sogou/search/api/core/api_command.rb, line 96 def error(err) if err.is_a?(Net::ReadTimeout) || err.is_a?(SocketError) err = TransmissionError.new(err) end if block_given? if err.respond_to?(:header) && err.header.is_a?(Array) err = err.header.map { |h| check_error_code(h, false) } end yield(nil, err) else raise err end end
success(result) { |result, nil| ... }
click to toggle source
# File lib/sogou/search/api/core/api_command.rb, line 91 def success(result) yield(result, nil) if block_given? result end