Class: ArchivesSpaceResumptionToken

Inherits:
Object
  • Object
show all
Defined in:
backend/app/lib/oai/aspace_resumption_token.rb

Constant Summary collapse

PRODUCING_RECORDS_STATE =
'producing_records'
PRODUCING_DELETES_STATE =
'producing_deletes'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, available_record_types) ⇒ ArchivesSpaceResumptionToken

Returns a new instance of ArchivesSpaceResumptionToken.



6
7
8
9
10
11
12
13
14
15
16
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 6

def initialize(options, available_record_types)
  @options = Hash[options.map {|k, v| [k.to_s, v]}]

  @options['state'] ||= PRODUCING_RECORDS_STATE
  @options['last_delete_id'] ||= 0

  unless @options.has_key?('remaining_types')
    types_for_format = available_record_types.fetch(format) { raise OAI::FormatException.new }
    @options['remaining_types'] = Hash[types_for_format.record_types.map {|type| [type.to_s, 0]}]
  end
end

Class Method Details

.extract_format(token) ⇒ Object



18
19
20
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 18

def self.extract_format(token)
  self.parse(token, {}).format
end

.parse(token, available_record_types) ⇒ Object



83
84
85
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 83

def self.parse(token, available_record_types)
  new(ASUtils.json_parse(Base64::urlsafe_decode64(token)), available_record_types)
end

Instance Method Details

#any_records_left?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 66

def any_records_left?
  !remaining_types.empty?
end

#formatObject



38
39
40
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 38

def format
  @options.fetch('metadata_prefix')
end

#fromObject



46
47
48
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 46

def from
  @options.fetch('from', nil)
end

#last_delete_idObject



30
31
32
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 30

def last_delete_id
  @options.fetch('last_delete_id')
end

#last_delete_id=(value) ⇒ Object



34
35
36
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 34

def last_delete_id=(value)
  @options['last_delete_id'] = value
end

#remaining_typesObject



54
55
56
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 54

def remaining_types
  @options.fetch('remaining_types').sort_by {|type, _| type}
end

#serializeObject



87
88
89
90
91
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 87

def serialize
  issue_time = (Time.now.to_f * 1000).to_i

  Base64::urlsafe_encode64(@options.merge('issue_time' => issue_time).to_json)
end

#setObject



42
43
44
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 42

def set
  @options.fetch('set', nil)
end

#set_last_seen(oai_record) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 70

def set_last_seen(oai_record)
  return self unless oai_record

  aspace_record = oai_record.sequel_record

  # We've already depleted this record type
  return self unless @options['remaining_types'].has_key?(aspace_record.class.to_s)

  @options['remaining_types'][aspace_record.class.to_s] = aspace_record.id

  self
end

#start_deletes!Object



26
27
28
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 26

def start_deletes!
  @options['state'] = PRODUCING_DELETES_STATE
end

#stateObject



22
23
24
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 22

def state
  @options.fetch('state')
end

#to_xmlObject



93
94
95
96
97
98
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 93

def to_xml
  xml = Builder::XmlMarkup.new
  xml.resumptionToken(self.serialize)

  xml.target!
end

#untilObject



50
51
52
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 50

def until
  @options.fetch('until', nil)
end

#update_depleted(types) ⇒ Object



58
59
60
61
62
63
64
# File 'backend/app/lib/oai/aspace_resumption_token.rb', line 58

def update_depleted(types)
  types.each do |depleted_type|
    @options['remaining_types'].delete(depleted_type)
  end

  self
end