Class: Archaeo::ArchiveUrl

Inherits:
Object
  • Object
show all
Defined in:
lib/archaeo/archive_url.rb

Overview

Model representing a Wayback Machine archive URL.

Encapsulates URL construction and parsing for archive.org URLs, supporting both normal and identity (raw) modes.

Constant Summary collapse

BASE =
"https://web.archive.org/web"
TIMESTAMP_RE =
%r{web\.archive\.org/web/(\d{14})}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_url, timestamp:, identity: false) ⇒ ArchiveUrl

Returns a new instance of ArchiveUrl.



15
16
17
18
19
# File 'lib/archaeo/archive_url.rb', line 15

def initialize(original_url, timestamp:, identity: false)
  @original_url = original_url.to_s
  @timestamp = Timestamp.coerce(timestamp)
  @identity = identity
end

Instance Attribute Details

#original_urlObject (readonly)

Returns the value of attribute original_url.



13
14
15
# File 'lib/archaeo/archive_url.rb', line 13

def original_url
  @original_url
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



13
14
15
# File 'lib/archaeo/archive_url.rb', line 13

def timestamp
  @timestamp
end

Class Method Details

.parse(string) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/archaeo/archive_url.rb', line 21

def self.parse(string)
  match = string.match(TIMESTAMP_RE)
  unless match
    raise ArgumentError,
          "Not a valid archive URL: #{string}"
  end

  ts = Timestamp.parse(match[1])
  identity = string.include?("#{match[1]}id_/")
  rest = extract_original_url(string, match[1], identity)

  new(rest, timestamp: ts, identity: identity)
end

Instance Method Details

#identity?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/archaeo/archive_url.rb', line 35

def identity?
  @identity
end

#to_sObject



39
40
41
42
# File 'lib/archaeo/archive_url.rb', line 39

def to_s
  suffix = identity? ? "id_" : ""
  "#{BASE}/#{@timestamp}#{suffix}/#{@original_url}"
end