Class: AppnexusApi::LogLevelDataDownloadService

Inherits:
Service
  • Object
show all
Defined in:
lib/appnexusapi/log_level_data_download_service.rb

Defined Under Namespace

Classes: BadChecksumException

Constant Summary

Constants inherited from Service

Service::DEFAULT_NUMBER_OF_ELEMENTS

Instance Method Summary collapse

Methods inherited from Service

#create, #delete, #get_all, #name, #parse_response, #plural_name, #plural_uri_name, #resource_class, #resource_name, #update, #uri_suffix

Constructor Details

#initialize(connection, options = {}) ⇒ LogLevelDataDownloadService

Returns a new instance of LogLevelDataDownloadService.



5
6
7
8
9
# File 'lib/appnexusapi/log_level_data_download_service.rb', line 5

def initialize(connection, options = {})
  @read_only = true
  @downloaded_files_path = options[:downloaded_files_path] || '.'
  super(connection)
end

Instance Method Details

#download_location(params = {}) ⇒ Object



11
12
13
# File 'lib/appnexusapi/log_level_data_download_service.rb', line 11

def download_location(params = {})
  @connection.get(uri_suffix, params).headers['location']
end

#download_resource(data_resource) ⇒ Object

Parameter is a LogLevelDataResource Downloads a gzipped file Returns an array of paths to downloaded files



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/appnexusapi/log_level_data_download_service.rb', line 18

def download_resource(data_resource)
  data_resource.download_params.map do |params|
    uri = URI.parse(download_location(params.reject { |k,v| k == :checksum }))
    filename = File.join(@downloaded_files_path, "#{params[:siphon_name]}_#{params[:hour]}_#{params[:split_part]}.gz")
    Pester.retry_with_exponential_backoff do
      download_file(uri, filename)
      calculated_checksum = Digest::MD5.hexdigest(File.read(filename))
      if calculated_checksum != params[:checksum]
        error_message = "Calculated checksum of #{calculated_checksum} doesn't match API provided checksum #{params[:checksum]}"
        puts error_message
        fail(BadChecksumException, error_message)
      end
    end

    filename
  end
end

#getObject



36
37
38
# File 'lib/appnexusapi/log_level_data_download_service.rb', line 36

def get
  fail(AppnexusApi::NotImplemented, 'This service is designed to work through download_location method.')
end

#uri_nameObject



40
41
42
# File 'lib/appnexusapi/log_level_data_download_service.rb', line 40

def uri_name
  'siphon-download'
end