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

Inherits:
Object
  • Object
show all
Includes:
Helpers, 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.



25
26
27
28
29
30
31
32
33
# File 'lib/naver/searchad/api/core/http_command.rb', line 25

def initialize(method, url, body: nil)
  @options = RequestOptions.default.dup
  @url = url.is_a?(String) ? new_template(url) : url
  @method = method
  @header = {}
  @body = body
  @query = {}
  @params = {}
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#headerObject

Returns the value of attribute header.



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

def header
  @header
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#paramsObject

Returns the value of attribute params.



23
24
25
# File 'lib/naver/searchad/api/core/http_command.rb', line 23

def params
  @params
end

#queryObject

Returns the value of attribute query.



22
23
24
# File 'lib/naver/searchad/api/core/http_command.rb', line 22

def query
  @query
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#_execute(client) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/naver/searchad/api/core/http_command.rb', line 72

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



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
127
128
129
# File 'lib/naver/searchad/api/core/http_command.rb', line 99

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



131
132
133
# File 'lib/naver/searchad/api/core/http_command.rb', line 131

def decode_response_body(content_type, body)
  body
end

#execute(client, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/naver/searchad/api/core/http_command.rb', line 35

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/naver/searchad/api/core/http_command.rb', line 54

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



94
95
96
97
# File 'lib/naver/searchad/api/core/http_command.rb', line 94

def process_response(status, header, body)
  check_status(status, header, body)
  decode_response_body(header['Content-Type'].first, body)
end

#release!Object



69
70
# File 'lib/naver/searchad/api/core/http_command.rb', line 69

def release!
end