Class: NeocitiesRed::Services::Site::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/neocities_red/services/site/exporter.rb

Overview

Downloads all files from the remote Neocities site to the local filesystem.

Supports incremental pulls — files that haven't been updated since the last pull (and exist locally) are skipped. Progress is displayed per-file, or via a Whirly spinner in quiet mode. Pull metadata (timestamp and working directory) is persisted in the config file for future incremental pulls.

Examples:

Full pull

exporter = NeocitiesRed::Services::Site::Exporter.new(
  client, "my-site", config_data, config_path, display: display
)
exporter.export

Quiet pull with incremental support

exporter.export(quiet: true, last_pull_time: "2024-01-01", last_pull_loc: "/path/to/site")

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, sitename, data, app_config_path, display:) ⇒ Exporter

Returns a new instance of Exporter.

Parameters:

  • client (NeocitiesRed::Client)

    authenticated API client

  • sitename (String)

    the Neocities site name

  • data (Hash)

    current application config data

  • app_config_path (String)

    path to the config file

  • display (NeocitiesRed::CliDisplay)

    output helper



49
50
51
52
53
54
55
56
# File 'lib/neocities_red/services/site/exporter.rb', line 49

def initialize(client, sitename, data, app_config_path, display:)
  @client = client
  @sitename = sitename
  @data = data
  @app_config_path = app_config_path
  @display = display
  @pastel = Pastel.new(eachline: "\n")
end

Instance Attribute Details

#app_config_pathString (readonly)

Returns path to the config file for persisting pull metadata.

Returns:

  • (String)

    path to the config file for persisting pull metadata



42
43
44
# File 'lib/neocities_red/services/site/exporter.rb', line 42

def app_config_path
  @app_config_path
end

#clientNeocitiesRed::Client (readonly)

Returns the authenticated API client.

Returns:



33
34
35
# File 'lib/neocities_red/services/site/exporter.rb', line 33

def client
  @client
end

#dataHash (readonly)

Returns the current config data (modified in-place with last pull info).

Returns:

  • (Hash)

    the current config data (modified in-place with last pull info)



39
40
41
# File 'lib/neocities_red/services/site/exporter.rb', line 39

def data
  @data
end

#sitenameString (readonly)

Returns the site name being exported.

Returns:

  • (String)

    the site name being exported



36
37
38
# File 'lib/neocities_red/services/site/exporter.rb', line 36

def sitename
  @sitename
end

Instance Method Details

#export(quiet: false, last_pull_time: nil, last_pull_loc: nil) ⇒ void

This method returns an undefined value.

Downloads all site files to the current working directory.

After the download, persists the current timestamp and working directory to the config file for future incremental pulls.

Parameters:

  • quiet (Boolean) (defaults to: false)

    when true, shows a spinner instead of per-file output

  • last_pull_time (String, nil) (defaults to: nil)

    ISO timestamp of the last pull

  • last_pull_loc (String, nil) (defaults to: nil)

    working directory of the last pull



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/neocities_red/services/site/exporter.rb', line 67

def export(quiet: false, last_pull_time: nil, last_pull_loc: nil)
  if quiet
    Whirly.start spinner: ["😺", "😸", "😹", "😻", "😼", "😽", "🙀", "😿", "😾"],
                 status: "Retrieving files for #{@pastel.bold @sitename}"
  end

  fetch_files(last_pull_time, last_pull_loc, quiet)

  data["LAST_PULL"] = {
    time: Time.now,
    loc: Dir.pwd
  }

  ::File.write(app_config_path, data.to_json)
ensure
  Whirly.stop if quiet
end