Class: Naver::Searchad::Api::Core::HttpCommand

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/naver/searchad/api/core/http_command.rb

Direct Known Subclasses

ApiCommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

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

#bodyObject

Returns the value of attribute body.



16
17
18
# File 'lib/naver/searchad/api/core/http_command.rb', line 16

def body
  @body
end

#headerObject

Returns the value of attribute header.



17
18
19
# File 'lib/naver/searchad/api/core/http_command.rb', line 17

def header
  @header
end

#methodObject (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

#optionsObject

Returns the value of attribute options.



18
19
20
# File 'lib/naver/searchad/api/core/http_command.rb', line 18

def options
  @options
end

#paramsObject

Returns the value of attribute params.



20
21
22
# File 'lib/naver/searchad/api/core/http_command.rb', line 20

def params
  @params
end

#queryObject

Returns the value of attribute query.



19
20
21
# File 'lib/naver/searchad/api/core/http_command.rb', line 19

def query
  @query
end

#urlObject (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 = {}
  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) ⇒ 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, 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) ⇒ 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 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) ⇒ 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