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(options, expiration = nil, total = nil) ⇒ ResumptionToken

Returns a new instance of ResumptionToken.



44
45
46
47
48
49
50
51
52
# File 'lib/oai/provider/resumption_token.rb', line 44

def initialize(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
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

#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 (readonly)

Returns the value of attribute total.



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

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



40
41
42
# File 'lib/oai/provider/resumption_token.rb', line 40

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

.parse(token_string) ⇒ Object

parses a token string and returns a ResumptionToken



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

def self.parse(token_string)
  begin
    options = {}
    matches = /(.+):(\d+)$/.match(token_string)
    options[:last] = matches.captures[1].to_i
    
    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(/\)$/, '')).localtime
      when /^u/
        options[:until] = Time.parse(part.sub(/^u\(/, '').sub(/\)$/, '')).localtime
      end
    end
    self.new(options)
  rescue => err
    raise OAI::ResumptionTokenException.new
  end
end

Instance Method Details

#==(other) ⇒ Object



60
61
62
63
64
# File 'lib/oai/provider/resumption_token.rb', line 60

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



55
56
57
58
# File 'lib/oai/provider/resumption_token.rb', line 55

def next(last)
  @last = last
  self
end

#to_conditions_hashObject

return a hash containing just the model selection parameters



74
75
76
77
78
79
80
# File 'lib/oai/provider/resumption_token.rb', line 74

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



83
84
85
# File 'lib/oai/provider/resumption_token.rb', line 83

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

#to_xmlObject

output an xml resumption token



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

def to_xml
  xml = Builder::XmlMarkup.new
  xml.resumptionToken(encode_conditions, hash_of_attributes)
  xml.target!
end