Class: Archaeo::ArchiveUrl
- Inherits:
-
Object
- Object
- Archaeo::ArchiveUrl
- 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
-
#original_url ⇒ Object
readonly
Returns the value of attribute original_url.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #as_json ⇒ Object
- #hash ⇒ Object
- #identity? ⇒ Boolean
- #identity_url ⇒ Object
-
#initialize(original_url, timestamp:, identity: false) ⇒ ArchiveUrl
constructor
A new instance of ArchiveUrl.
- #to_h ⇒ Object
- #to_s ⇒ Object
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() @identity = identity end |
Instance Attribute Details
#original_url ⇒ Object (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 |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
13 14 15 |
# File 'lib/archaeo/archive_url.rb', line 13 def @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
#==(other) ⇒ Object Also known as: eql?
39 40 41 42 43 44 |
# File 'lib/archaeo/archive_url.rb', line 39 def ==(other) other.is_a?(self.class) && original_url == other.original_url && == other. && identity? == other.identity? end |
#as_json ⇒ Object
67 68 69 70 |
# File 'lib/archaeo/archive_url.rb', line 67 def as_json(*) { original_url: @original_url, timestamp: @timestamp.to_s, identity: @identity, url: to_s } end |
#hash ⇒ Object
47 48 49 |
# File 'lib/archaeo/archive_url.rb', line 47 def hash [original_url, , identity?].hash end |
#identity? ⇒ Boolean
35 36 37 |
# File 'lib/archaeo/archive_url.rb', line 35 def identity? @identity end |
#identity_url ⇒ Object
56 57 58 59 60 |
# File 'lib/archaeo/archive_url.rb', line 56 def identity_url return to_s if identity? self.class.new(@original_url, timestamp: @timestamp, identity: true).to_s end |
#to_h ⇒ Object
62 63 64 65 |
# File 'lib/archaeo/archive_url.rb', line 62 def to_h { original_url: @original_url, timestamp: @timestamp, identity: @identity } end |
#to_s ⇒ Object
51 52 53 54 |
# File 'lib/archaeo/archive_url.rb', line 51 def to_s suffix = identity? ? "id_" : "" "#{BASE}/#{@timestamp}#{suffix}/#{@original_url}" end |