Class: Naver::Searchad::Api::Core::HttpCommand
- Inherits:
-
Object
- Object
- Naver::Searchad::Api::Core::HttpCommand
- Includes:
- Logging
- Defined in:
- lib/naver/searchad/api/core/http_command.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#header ⇒ Object
Returns the value of attribute header.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#options ⇒ Object
Returns the value of attribute options.
-
#params ⇒ Object
Returns the value of attribute params.
-
#query ⇒ Object
Returns the value of attribute query.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #_execute(client) ⇒ Object
- #check_status(status, header = nil, body = nil, message = nil) ⇒ Object
- #decode_response_body(content_type, body) ⇒ Object
- #execute(client, &block) ⇒ Object
-
#initialize(method, url, body: nil) ⇒ HttpCommand
constructor
A new instance of HttpCommand.
- #prepare! ⇒ Object
- #process_response(status, header, body) ⇒ Object
- #release! ⇒ Object
Methods included from Logging
Constructor Details
#initialize(method, url, body: nil) ⇒ HttpCommand
Returns a new instance of HttpCommand.
22 23 24 25 26 27 28 29 30 |
# File 'lib/naver/searchad/api/core/http_command.rb', line 22 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 |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
16 17 18 |
# File 'lib/naver/searchad/api/core/http_command.rb', line 16 def body @body end |
#header ⇒ Object
Returns the value of attribute header.
17 18 19 |
# File 'lib/naver/searchad/api/core/http_command.rb', line 17 def header @header end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
15 16 17 |
# File 'lib/naver/searchad/api/core/http_command.rb', line 15 def method @method end |
#options ⇒ Object
Returns the value of attribute options.
18 19 20 |
# File 'lib/naver/searchad/api/core/http_command.rb', line 18 def @options end |
#params ⇒ Object
Returns the value of attribute params.
20 21 22 |
# File 'lib/naver/searchad/api/core/http_command.rb', line 20 def params @params end |
#query ⇒ Object
Returns the value of attribute query.
19 20 21 |
# File 'lib/naver/searchad/api/core/http_command.rb', line 19 def query @query end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
14 15 16 |
# File 'lib/naver/searchad/api/core/http_command.rb', line 14 def url @url end |
Instance Method Details
#_execute(client) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/naver/searchad/api/core/http_command.rb', line 69 def _execute(client) logger.debug("Executing HTTP #{method} #{url}") request_header = {} (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) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/naver/searchad/api/core/http_command.rb', line 96 def check_status(status, header = nil, body = nil, = nil) case status when 200...300 nil when 301, 302, 303, 307 ||= "Redirect to #{header['Location']}" raise Naver::Searchad::Api::RedirectError.new( , status_code: status, header: header, body: body) when 401 ||= 'Unauthorized' raise Naver::Searchad::Api::AuthorizationError.new( , status_code: status, header: header, body: body) when 429 ||= 'Rate limit exceeded' raise Naver::Searchad::Api::RateLimitError.new( , status_code: status, header: header, body: body) when 400, 402...500 ||= 'Invalid request' raise Naver::Searchad::Api::RequestError.new( , status_code: status, header: header, body: body) when 500...600 ||= 'Server error' raise Naver::Searchad::Api::ServerError.new( , status_code: status, header: header, body: body) else logger.warn("Encountered unexpected status code #{status}") ||= 'Unknown error' raise Naver::Searchad::Api::UnknownError.new( , status_code: status, header: header, body: body) end end |
#decode_response_body(content_type, body) ⇒ Object
128 129 130 |
# File 'lib/naver/searchad/api/core/http_command.rb', line 128 def decode_response_body(content_type, body) body end |
#execute(client, &block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/naver/searchad/api/core/http_command.rb', line 32 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! ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/naver/searchad/api/core/http_command.rb', line 51 def prepare! normalize_unicode = true if @header.merge!(.header) if .header normalize_unicode = .normalize_unicode end if url.is_a?(Addressable::Template) @url = url.(params, nil, normalize_unicode) @url.query_values = query.merge(url.query_values || {}) end @body = '' unless body end |
#process_response(status, header, body) ⇒ Object
91 92 93 94 |
# File 'lib/naver/searchad/api/core/http_command.rb', line 91 def process_response(status, header, body) check_status(status, header, body) decode_response_body(header['Content-Type'].first, body) end |
#release! ⇒ Object
66 67 |
# File 'lib/naver/searchad/api/core/http_command.rb', line 66 def release! end |