Class: Archaeo::Snapshot

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

Overview

A single CDX Server API record representing an archived document.

Maps the seven standard CDX fields and provides the computed archive URL via the ArchiveUrl model.

Constant Summary collapse

FIELDS =
%i[urlkey timestamp original_url
mimetype status_code digest length].freeze
BLOCKED_STATUS =
-1

Instance Method Summary collapse

Constructor Details

#initialize(urlkey:, timestamp:, original_url:, mimetype: nil, status_code: nil, digest: nil, length: nil) ⇒ Snapshot

Returns a new instance of Snapshot.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/archaeo/snapshot.rb', line 16

def initialize(urlkey:, timestamp:, original_url:,
               mimetype: nil, status_code: nil,
               digest: nil, length: nil)
  @urlkey = urlkey.to_s
  @timestamp = Timestamp.coerce(timestamp)
  @original_url = original_url.to_s
  @mimetype = mimetype.to_s
  @status_code = status_code.to_i
  @digest = digest.to_s
  @length = length.to_i
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



97
98
99
# File 'lib/archaeo/snapshot.rb', line 97

def ==(other)
  other.is_a?(self.class) && to_a == other.to_a
end

#archive_urlObject



28
29
30
# File 'lib/archaeo/snapshot.rb', line 28

def archive_url
  ArchiveUrl.new(original_url, timestamp: @timestamp).to_s
end

#as_jsonObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/archaeo/snapshot.rb', line 85

def as_json(*)
  {
    urlkey: @urlkey,
    timestamp: @timestamp.to_s,
    original_url: @original_url,
    mimetype: @mimetype,
    status_code: @status_code,
    digest: @digest,
    length: @length,
  }
end

#blocked?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/archaeo/snapshot.rb', line 32

def blocked?
  @status_code == BLOCKED_STATUS
end

#client_error?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/archaeo/snapshot.rb', line 44

def client_error?
  @status_code.between?(400, 499)
end

#error?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/archaeo/snapshot.rb', line 52

def error?
  client_error? || server_error?
end

#fetch(client: HttpClient.new, identity: false) ⇒ Object



56
57
58
59
60
# File 'lib/archaeo/snapshot.rb', line 56

def fetch(client: HttpClient.new, identity: false)
  Fetcher.new(client: client).fetch(
    original_url, timestamp: @timestamp, identity: identity
  )
end

#fetch_with_assets(client: HttpClient.new) ⇒ Object



62
63
64
65
66
# File 'lib/archaeo/snapshot.rb', line 62

def fetch_with_assets(client: HttpClient.new)
  Fetcher.new(client: client).fetch_page_with_assets(
    original_url, timestamp: @timestamp
  )
end

#hashObject



102
103
104
# File 'lib/archaeo/snapshot.rb', line 102

def hash
  to_a.hash
end

#redirect?Boolean

Returns:

  • (Boolean)


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

def redirect?
  @status_code.between?(300, 399)
end

#server_error?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/archaeo/snapshot.rb', line 48

def server_error?
  @status_code.between?(500, 599)
end

#success?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/archaeo/snapshot.rb', line 36

def success?
  @status_code == 200
end

#to_aObject



68
69
70
71
# File 'lib/archaeo/snapshot.rb', line 68

def to_a
  [@urlkey, @timestamp, @original_url, @mimetype,
   @status_code, @digest, @length]
end

#to_hObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/archaeo/snapshot.rb', line 73

def to_h
  {
    urlkey: @urlkey,
    timestamp: @timestamp,
    original_url: @original_url,
    mimetype: @mimetype,
    status_code: @status_code,
    digest: @digest,
    length: @length,
  }
end