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
# File 'lib/e621_export_downloader/client.rb', line 23

def config(&block)
  block.call(Options::Builder.new(@options))
end

#connectionObject



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

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

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



33
34
35
# File 'lib/e621_export_downloader/client.rb', line 33

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

#get(type) ⇒ Object

Raises:



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

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:



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

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(
      file_name:  d["file_name"],
      file_size:  d["file_size"].to_i,
      name:       Types.deserialize(d["name"]),
      updated_at: d["updated_at"],
      url:        d["url"],
    )
  end, T::Array[APIExportData])
  @export_cache = result
end

#get_deferred(type) ⇒ Object



48
49
50
51
52
53
# File 'lib/e621_export_downloader/client.rb', line 48

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