Class: Naver::Searchad::Api::Core::ApiCommand

Inherits:
HttpCommand
  • Object
show all
Defined in:
lib/naver/searchad/api/core/api_command.rb

Direct Known Subclasses

DownloadCommand

Constant Summary collapse

JSON_CONTENT_TYPE =
'application/json'.freeze
ERROR_CODE_MAPPING =
{
  '1002' => Naver::Searchad::Api::InvalidRequestError,
  '1018' => Naver::Searchad::Api::NotEnoughPermissionError,
  '3506' => Naver::Searchad::Api::DuplicatedCampaignNameError,
  '3710' => Naver::Searchad::Api::DuplicatedAdgroupNameError,
}

Instance Attribute Summary collapse

Attributes inherited from HttpCommand

#body, #header, #method, #options, #params, #query, #url

Instance Method Summary collapse

Methods inherited from HttpCommand

#_execute, #execute, #process_response, #release!

Methods included from Logging

#logger

Constructor Details

#initialize(method, url, body: nil, decode_snake_case: true) ⇒ ApiCommand

Returns a new instance of ApiCommand.



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

def initialize(method, url, body: nil, decode_snake_case: true)
  super(method, url, body: body)

  @decode_snake_case = decode_snake_case
end

Instance Attribute Details

#decode_snake_caseObject

Returns the value of attribute decode_snake_case.



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

def decode_snake_case
  @decode_snake_case
end

#request_objectObject

Returns the value of attribute request_object.



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

def request_object
  @request_object
end

Instance Method Details

#check_status(status, header = nil, body = nil, message = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/naver/searchad/api/core/api_command.rb', line 50

def check_status(status, header = nil, body = nil, message = nil)
  case status
  when 400, 402..500
    code, message = parse_error(body)
    raise ERROR_CODE_MAPPING[code].new(
      message,
      status_code: status,
      header: header,
      body: body
    ) if ERROR_CODE_MAPPING.key?(code)
  end

  super(status, header, body, message)
end

#decode_response_body(content_type, body) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/naver/searchad/api/core/api_command.rb', line 37

def decode_response_body(content_type, body)
  return super unless content_type
  return nil unless content_type.start_with?(JSON_CONTENT_TYPE)

  decoded_response = JSON.parse(body)
  deep_snake_case_params!(decoded_response) if @decode_snake_case
  if decoded_response.kind_of?(Hash)
    OpenStruct.new(decoded_response)
  elsif decoded_response.kind_of?(Array)
    decoded_response.map { |h| OpenStruct.new(h) }
  end
end

#prepare!Object



29
30
31
32
33
34
35
# File 'lib/naver/searchad/api/core/api_command.rb', line 29

def prepare!
  if request_object
    self.header['Content-Type'] ||= JSON_CONTENT_TYPE
    self.body = request_object.to_json
  end
  super
end