Class: E621ExportDownloader::Export

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/e621_export_downloader/export.rb

Constant Summary collapse

Model =
type_member

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, client:, type:, parser:) ⇒ Export

Returns a new instance of Export.



31
32
33
34
35
36
37
# File 'lib/e621_export_downloader/export.rb', line 31

def initialize(data:, client:, type:, parser:)
  @data = T.let(data, APIExportData)
  @client = T.let(client, Client)
  @type = T.let(type, Types)
  @parser = T.let(parser, Client::Options::Parser)
  @downloaded = T.let(nil, T.nilable(T::Boolean))
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



22
23
24
# File 'lib/e621_export_downloader/export.rb', line 22

def client
  @client
end

#dataObject

Returns the value of attribute data.



19
20
21
# File 'lib/e621_export_downloader/export.rb', line 19

def data
  @data
end

#downloadedObject

Returns the value of attribute downloaded.



28
29
30
# File 'lib/e621_export_downloader/export.rb', line 28

def downloaded
  @downloaded
end

#typeObject

Returns the value of attribute type.



25
26
27
# File 'lib/e621_export_downloader/export.rb', line 25

def type
  @type
end

Instance Method Details

#deleteObject



40
41
42
43
44
45
46
# File 'lib/e621_export_downloader/export.rb', line 40

def delete
  return false unless check_downloaded
  client.debug("deleting cached export", header: ["export:#{type.serialize}"])
  FileUtils.rm(file_path)
  @downloaded = false
  true
end

#download(&block) ⇒ Object

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/e621_export_downloader/export.rb', line 57

def download(&block)
  raise(ResolveError, "Export #{type.serialize} does not exist") unless exists?
  if check_downloaded
    client.debug("using cached export", header: ["export:#{type.serialize}"])
    return file_path
  end

  client.debug("downloading export", header: ["export:#{type.serialize}"])

  FileUtils.mkdir_p(Constants::TEMP_DIR)
  payload = { type: type.serialize, file_size: data.file_size, validate_checksum: client.options.validate_checksum }
  ActiveSupport::Notifications.instrument("download.e621_export_downloader", payload) do
    client.with_retries(header: ["export:#{type.serialize}"]) { perform_download(&block) }
  end

  @downloaded = true
  client.debug("download complete: #{file_path}", header: ["export:#{type.serialize}"])
  file_path
end

#exists?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
# File 'lib/e621_export_downloader/export.rb', line 78

def exists?
  res = client.with_retries(header: ["export:#{type.serialize}"]) { client.connection.head(data.url) }
  result = res.success?
  client.debug("checked export existence: #{result}", header: ["export:#{type.serialize}"])
  result
end

#read(&block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/e621_export_downloader/export.rb', line 86

def read(&block)
  download unless check_downloaded
  client.debug("reading export", header: ["export:#{type.serialize}"])
  total = line_count
  CSV.foreach(file_path, headers: true, converters: ->(f) { f.nil? ? "" : f }) do |row|
    args = [@parser.call(T.cast(row, CSV::Row).to_hash), total]
    args = args.slice(0, block.arity) if block.arity != -1
    block.call(*T.unsafe(args))
  end
  client.debug("finished reading export", header: ["export:#{type.serialize}"])
  delete unless client.options.cache
end

#read_allObject



100
101
102
103
104
105
106
107
# File 'lib/e621_export_downloader/export.rb', line 100

def read_all
  client.debug("reading all records", header: ["export:#{type.serialize}"])
  results = []
  read do |record|
    results << record
  end
  results
end