Class: E621ExportDownloader::Client

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/e621_export_downloader/client.rb,
lib/e621_export_downloader/client/options.rb,
lib/e621_export_downloader/client/options/builder.rb,
lib/e621_export_downloader/client/options/builder/parsers.rb

Defined Under Namespace

Classes: Options

Constant Summary collapse

Logger =
::Logger.new($stdout)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Client

Returns a new instance of Client.



17
18
19
20
# File 'lib/e621_export_downloader/client.rb', line 17

def initialize(options = nil)
  @options = T.let(options || Options.new, Options)
  @export_cache = T.let(nil, T.nilable(T::Array[APIExportData]))
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/e621_export_downloader/client.rb', line 14

def options
  @options
end

Instance Method Details

#config(&block) ⇒ Object



23
24
25
26
27
# File 'lib/e621_export_downloader/client.rb', line 23

def config(&block)
  builder = Options::Builder.new(@options)
  block.call(builder)
  @options = Options.new(cache: builder.cache, validate_checksum: builder.validate_checksum, parsers: builder.parsers)
end

#connectionObject



30
31
32
# File 'lib/e621_export_downloader/client.rb', line 30

def connection
  Faraday.new(headers: { "User-Agent" => Constants::USER_AGENT })
end

#debug(msg, header: []) ⇒ Object



35
36
37
# File 'lib/e621_export_downloader/client.rb', line 35

def debug(msg, header: [])
  Logger.debug("[e621_export_downloader#{":#{header.join(':')}" unless header.empty?}] #{msg}")
end

#get(type) ⇒ Object

Raises:



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

def get(type)
  type = string_to_type(type) if type.is_a?(String)
  T.assert_type!(type, Types)
  data = get_data.find { |d| d.name == type }
  raise(ResolveError, "Export data for \"#{type.serialize}\" not found") if data.nil?
  debug("creating export for #{type.serialize}")
  Export.new(data: data, client: self, type: type, parser: options.parsers.public_send(type.serialize))
end

#get_dataObject

Raises:



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

def get_data
  return @export_cache unless @export_cache.nil?
  debug("fetching export data from api")
  res = connection.get(Constants::API_URL)
  raise(ResolveError, "Failed to fetch exports: #{res.status} #{res.reason_phrase}") unless res.success?
  data = JSON.parse(T.unsafe(res).body)
  result = T.let(data.map do |d|
    APIExportData.new(
      name:       Types.deserialize(d["name"]),
      file_name:  d["file_name"],
      file_size:  d["file_size"].to_i,
      checksum:   d["checksum"],
      updated_at: d["updated_at"],
      url:        d["url"],
    )
  end, T::Array[APIExportData])
  @export_cache = result
end

#get_deferred(type) ⇒ Object



50
51
52
53
54
55
# File 'lib/e621_export_downloader/client.rb', line 50

def get_deferred(type)
  type = string_to_type(type) if type.is_a?(String)
  T.assert_type!(type, Types)
  debug("creating deferred export for #{type.serialize}")
  ExportHelper.new(type: type, client: self)
end