class Naver::Searchad::Api::Core::HttpCommand
Attributes
body[RW]
header[RW]
method[R]
options[RW]
params[RW]
query[RW]
url[R]
Public Class Methods
new(method, url, body: nil)
click to toggle source
# File lib/naver/searchad/api/core/http_command.rb, line 23 def initialize(method, url, body: nil) @options = RequestOptions.default.dup @url = url.is_a?(String) ? Addressable::Template.new(url) : url @method = method @header = {} @body = body @query = {} @params = {} end
Public Instance Methods
_execute(client)
click to toggle source
# File lib/naver/searchad/api/core/http_command.rb, line 70 def _execute(client) logger.debug("Executing HTTP #{method} #{url}") request_header = {} apply_request_options(request_header) http_res = client.request(method.to_s.upcase, url.to_s, query: nil, body: body, header: request_header, follow_redirect: true) logger.debug("Returned status(#{http_res.status}) and #{http_res.inspect}") response = process_response(http_res.status.to_i, http_res.header, http_res.body) logger.debug("Success - #{response}") success(response) rescue => e logger.debug("Error - #{e.inspect}") error(e) end
check_status(status, header = nil, body = nil, message = nil)
click to toggle source
# File lib/naver/searchad/api/core/http_command.rb, line 97 def check_status(status, header = nil, body = nil, message = nil) case status when 200...300 nil when 301, 302, 303, 307 message ||= "Redirect to #{header['Location']}" raise Naver::Searchad::Api::RedirectError.new( message, status_code: status, header: header, body: body) when 401 message ||= 'Unauthorized' raise Naver::Searchad::Api::AuthorizationError.new( message, status_code: status, header: header, body: body) when 429 message ||= 'Rate limit exceeded' raise Naver::Searchad::Api::RateLimitError.new( message, status_code: status, header: header, body: body) when 400, 402...500 message ||= 'Invalid request' raise Naver::Searchad::Api::RequestError.new( message, status_code: status, header: header, body: body) when 500...600 message ||= 'Server error' raise Naver::Searchad::Api::ServerError.new( message, status_code: status, header: header, body: body) else logger.warn("Encountered unexpected status code #{status}") message ||= 'Unknown error' raise Naver::Searchad::Api::UnknownError.new( message, status_code: status, header: header, body: body) end end
decode_response_body(content_type, body)
click to toggle source
# File lib/naver/searchad/api/core/http_command.rb, line 129 def decode_response_body(content_type, body) body end
execute(client) { |result, nil| ... }
click to toggle source
# File lib/naver/searchad/api/core/http_command.rb, line 33 def execute(client, &block) prepare! _execute(client).tap do |result| if block_given? yield result, nil end end rescue => e if block_given? yield nil, e else raise e end ensure release! end
prepare!()
click to toggle source
# File lib/naver/searchad/api/core/http_command.rb, line 52 def prepare! normalize_unicode = true if options @header.merge!(options.header) if options.header normalize_unicode = options.normalize_unicode end if url.is_a?(Addressable::Template) @url = url.expand(params, nil, normalize_unicode) @url.query_values = query.merge(url.query_values || {}) end @body = '' unless body end
process_response(status, header, body)
click to toggle source
# File lib/naver/searchad/api/core/http_command.rb, line 92 def process_response(status, header, body) check_status(status, header, body) decode_response_body(header['Content-Type'].first, body) end
release!()
click to toggle source
# File lib/naver/searchad/api/core/http_command.rb, line 67 def release! end
Private Instance Methods
apply_request_options(req_header)
click to toggle source
# File lib/naver/searchad/api/core/http_command.rb, line 135 def apply_request_options(req_header) options.authorization.apply(req_header, url.path, method) if options.authorization.respond_to?(:apply) req_header.merge!(header) end
error(err, &block)
click to toggle source
# File lib/naver/searchad/api/core/http_command.rb, line 145 def error(err, &block) if err.is_a?(HTTPClient::BadResponseError) begin res = err.res check_status(res.status.to_i, res.header, res.body) rescue Naver::Searchad::Api::Error => e err = e end elsif err.is_a?(HTTPClient::TimeoutError) || err.is_a?(SocketError) err = Naver::Searchad::Api::TransmissionError.new(err) end if block_given? block.call(nil, err) else raise err end end
success(result, &block)
click to toggle source
# File lib/naver/searchad/api/core/http_command.rb, line 140 def success(result, &block) block.call(result, nil) if block_given? result end