Class: OAI::Provider::ResumptionToken

Inherits:
Object
  • Object
show all
Defined in:
lib/oai/provider/resumption_token.rb

Overview

OAI::Provider::ResumptionToken

The ResumptionToken class forms the basis of paging query results. It provides several helper methods for dealing with resumption tokens.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(last_id, options, expiration = nil, total = nil) ⇒ ResumptionToken

Returns a new instance of ResumptionToken.



51
52
53
54
55
56
57
58
59
60
# File 'lib/oai/provider/resumption_token.rb', line 51

def initialize(last_id, options, expiration = nil, total = nil)
  @prefix = options[:metadata_prefix]
  @set = options[:set]
  @last = options[:last]
  @from = options[:from] if options[:from]
  @until = options[:until] if options[:until]
  @expiration = expiration if expiration
  @total = total if total
  @last_id = last_id
end

Instance Attribute Details

#expirationObject (readonly)

Returns the value of attribute expiration.



12
13
14
# File 'lib/oai/provider/resumption_token.rb', line 12

def expiration
  @expiration
end

#fromObject (readonly)

Returns the value of attribute from.



12
13
14
# File 'lib/oai/provider/resumption_token.rb', line 12

def from
  @from
end

#lastObject (readonly)

Returns the value of attribute last.



12
13
14
# File 'lib/oai/provider/resumption_token.rb', line 12

def last
  @last
end

#last_idObject (readonly)

Returns the value of attribute last_id.



12
13
14
# File 'lib/oai/provider/resumption_token.rb', line 12

def last_id
  @last_id
end

#prefixObject (readonly)

Returns the value of attribute prefix.



12
13
14
# File 'lib/oai/provider/resumption_token.rb', line 12

def prefix
  @prefix
end

#setObject (readonly)

Returns the value of attribute set.



12
13
14
# File 'lib/oai/provider/resumption_token.rb', line 12

def set
  @set
end

#totalObject

Returns the value of attribute total.



13
14
15
# File 'lib/oai/provider/resumption_token.rb', line 13

def total
  @total
end

#untilObject (readonly)

Returns the value of attribute until.



12
13
14
# File 'lib/oai/provider/resumption_token.rb', line 12

def until
  @until
end

Class Method Details

.extract_format(token_string) ⇒ Object

extracts the metadata prefix from a token string



47
48
49
# File 'lib/oai/provider/resumption_token.rb', line 47

def self.extract_format(token_string)
  return token_string.split('.')[0]
end

.parse(token_string) ⇒ Object

parses a token string and returns a ResumptionToken



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/oai/provider/resumption_token.rb', line 16

def self.parse(token_string)
  begin
    options = {}
    matches = /(.+):(\d+)$/.match(token_string)
    options[:last] = matches.captures[1].to_i
    last_id = nil
    total = nil
    
    parts = matches.captures[0].split('.')
    options[:metadata_prefix] = parts.shift
    parts.each do |part|
      case part
      when /^s/
        options[:set] = part.sub(/^s\(/, '').sub(/\)$/, '')
      when /^f/
        options[:from] = Time.parse(part.sub(/^f\(/, '').sub(/\)$/, ''))
      when /^u/
        options[:until] = Time.parse(part.sub(/^u\(/, '').sub(/\)$/, ''))
      when /^l/
        last_id = part.sub(/^l\(/, '').sub(/\)$/, '')
      when /^t/
        total = part.sub(/^t\(/, '').sub(/\)$/, '')
      end
    end
    self.new(last_id, options, nil, total)
  rescue => err
    raise OAI::ResumptionTokenException.new
  end
end

Instance Method Details

#==(other) ⇒ Object



68
69
70
71
72
# File 'lib/oai/provider/resumption_token.rb', line 68

def ==(other)
  prefix == other.prefix and set == other.set and from == other.from and
    self.until == other.until and last == other.last and 
    expiration == other.expiration and total == other.total
end

#next(last) ⇒ Object

convenience method for setting the offset of the next set of results



63
64
65
66
# File 'lib/oai/provider/resumption_token.rb', line 63

def next(last)
  @last = last
  self
end

#to_conditions_hashObject

return a hash containing just the model selection parameters



88
89
90
91
92
93
94
# File 'lib/oai/provider/resumption_token.rb', line 88

def to_conditions_hash
  conditions = {:metadata_prefix => self.prefix }
  conditions[:set] = self.set if self.set
  conditions[:from] = self.from if self.from
  conditions[:until] = self.until if self.until
  conditions
end

#to_sObject

return the a string representation of the token minus the offset



97
98
99
# File 'lib/oai/provider/resumption_token.rb', line 97

def to_s
  encode_conditions.gsub(/:\w+?$/, '')
end

#to_xmlObject

output an xml resumption token



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/oai/provider/resumption_token.rb', line 75

def to_xml
  xml = Builder::XmlMarkup.new
  token_content = if(last_id.to_s == last.to_s)
    [] # Empty token required on last page of results
  else
    [encode_conditions, hash_of_attributes]
  end
  
  xml.resumptionToken(*token_content)
  xml.target!
end