Class: Naver::Searchad::Api::Core::DownloadCommand

Inherits:
ApiCommand show all
Defined in:
lib/naver/searchad/api/core/download_command.rb

Constant Summary collapse

OK_STATUSES =
[200, 201, 206]

Constants inherited from ApiCommand

ApiCommand::ERROR_CODE_MAPPING, ApiCommand::JSON_CONTENT_TYPE

Instance Attribute Summary collapse

Attributes inherited from ApiCommand

#decode_snake_case, #request_object

Attributes inherited from HttpCommand

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

Instance Method Summary collapse

Methods inherited from ApiCommand

#check_status, #decode_response_body, #initialize

Methods inherited from HttpCommand

#check_status, #decode_response_body, #execute, #initialize, #process_response

Methods included from Logging

#logger

Constructor Details

This class inherits a constructor from Naver::Searchad::Api::Core::ApiCommand

Instance Attribute Details

#download_destObject

Returns the value of attribute download_dest.



11
12
13
# File 'lib/naver/searchad/api/core/download_command.rb', line 11

def download_dest
  @download_dest
end

Instance Method Details

#_execute(client) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/naver/searchad/api/core/download_command.rb', line 33

def _execute(client)
  request_header = {}
  apply_request_options(request_header)
  download_offset = nil

  http_res = client.get(url.to_s,
                        query: nil,
                        header: request_header,
                        follow_redirect: true) do |res, chunk|
    status = res.http_header.status_code.to_i
    next unless OK_STATUSES.include?(status)

    download_offset ||= (status == 206 ? @offset : 0)
    download_offset += chunk.bytesize

    if download_offset - chunk.bytesize == @offset
      next_chunk = chunk
    else
      chunk_index = @offset - (download_offset - chunk.bytesize)
      next_chunk = chunk.byteslice(chunk_index..-1)
      next if next_chunk.nil?
    end

    @download_io.write(next_chunk)
    @offset += next_chunk.bytesize
  end

  @download_io.flush

  if @close_io_on_finish
    result = nil
  else
    result = @download_io
  end
  check_status(http_res.status.to_i, http_res.header, http_res.body)
  logger.debug("DownloadCommand: Success")
  success(result)
rescue => e
  @download_io.flush
  logger.debug("DownloadCommand: Error - #{e.inspect}")
  error(e)
end

#prepare!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/naver/searchad/api/core/download_command.rb', line 13

def prepare!
  @state = :start
  @download_url = nil
  @offset = 0

  if download_dest.is_a?(String)
    @download_io = File.open(download_dest, 'wb')
    @close_io_on_finish = true
  else
    @download_io = StringIO.new('', 'wb')
    @close_io_on_finish = false
  end

  super
end

#release!Object



29
30
31
# File 'lib/naver/searchad/api/core/download_command.rb', line 29

def release!
  @download_io.close if @close_io_on_finish
end