Class: NeocitiesRed::Services::Site::Informer

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

Overview

Retrieves and formats site information for the Neocities site.

Queries the Neocities API for site metadata (domain, bandwidth, created_at, last_updated, etc.) and formats it as rows suitable for display in a TTY::Table.

Examples:

informer = NeocitiesRed::Services::Site::Informer.new(client, ["my-site"], "my-site")
rows = informer.pretty_print
puts TTY::Table.new(rows)

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, subargs = [], sitename = nil) ⇒ Informer

Returns a new instance of Informer.

Parameters:

  • client (NeocitiesRed::Client)

    authenticated API client

  • subargs (Array<String>) (defaults to: [])

    CLI subarguments; the first element may be a sitename to query

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

    default sitename (used when subargs is empty)



29
30
31
32
33
34
# File 'lib/neocities_red/services/site/informer.rb', line 29

def initialize(client, subargs = [], sitename = nil)
  @client = client
  @subargs = subargs
  @sitename = sitename
  @pastel = Pastel.new(eachline: "\n")
end

Instance Attribute Details

#clientNeocitiesRed::Client (readonly)

Returns the authenticated API client.

Returns:



23
24
25
# File 'lib/neocities_red/services/site/informer.rb', line 23

def client
  @client
end

Instance Method Details

#pretty_printArray<Array(String, Object)>

Formats the site stats as bold-labeled rows.

Timestamp fields (+created_at+, last_updated) are converted to local time for readability.

Returns:

  • (Array<Array(String, Object)>)

    array of [label, value] pairs



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/neocities_red/services/site/informer.rb', line 54

def pretty_print
  out = []

  stats[:info].each do |k, v|
    v = Time.parse(v).localtime if v && %i[created_at last_updated].include?(k)

    out << [@pastel.bold(k.to_s), v]
  end

  out
end

#statsHash

Fetches site information from the Neocities API.

Returns:

  • (Hash)

    parsed API response containing :info with site metadata

Raises:



40
41
42
43
44
45
46
# File 'lib/neocities_red/services/site/informer.rb', line 40

def stats
  response = @client.info(@subargs[0] || @sitename)

  raise NeocitiesRed::APIError, response[:message] if response[:result] == "error"

  response
end