Class: OAI::Provider::ResumptionToken
- Inherits:
-
Object
- Object
- OAI::Provider::ResumptionToken
- 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
-
#expiration ⇒ Object
readonly
Returns the value of attribute expiration.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#last ⇒ Object
readonly
Returns the value of attribute last.
-
#last_id ⇒ Object
readonly
Returns the value of attribute last_id.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#set ⇒ Object
readonly
Returns the value of attribute set.
-
#total ⇒ Object
Returns the value of attribute total.
-
#until ⇒ Object
readonly
Returns the value of attribute until.
Class Method Summary collapse
-
.extract_format(token_string) ⇒ Object
extracts the metadata prefix from a token string.
-
.parse(token_string) ⇒ Object
parses a token string and returns a ResumptionToken.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(last_id, options, expiration = nil, total = nil) ⇒ ResumptionToken
constructor
A new instance of ResumptionToken.
-
#next(last) ⇒ Object
convenience method for setting the offset of the next set of results.
-
#to_conditions_hash ⇒ Object
return a hash containing just the model selection parameters.
-
#to_s ⇒ Object
return the a string representation of the token minus the offset.
-
#to_xml ⇒ Object
output an xml resumption token.
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, , expiration = nil, total = nil) @prefix = [:metadata_prefix] @set = [:set] @last = [:last] @from = [:from] if [:from] @until = [:until] if [:until] @expiration = expiration if expiration @total = total if total @last_id = last_id end |
Instance Attribute Details
#expiration ⇒ Object (readonly)
Returns the value of attribute expiration.
12 13 14 |
# File 'lib/oai/provider/resumption_token.rb', line 12 def expiration @expiration end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
12 13 14 |
# File 'lib/oai/provider/resumption_token.rb', line 12 def from @from end |
#last ⇒ Object (readonly)
Returns the value of attribute last.
12 13 14 |
# File 'lib/oai/provider/resumption_token.rb', line 12 def last @last end |
#last_id ⇒ Object (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 |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
12 13 14 |
# File 'lib/oai/provider/resumption_token.rb', line 12 def prefix @prefix end |
#set ⇒ Object (readonly)
Returns the value of attribute set.
12 13 14 |
# File 'lib/oai/provider/resumption_token.rb', line 12 def set @set end |
#total ⇒ Object
Returns the value of attribute total.
13 14 15 |
# File 'lib/oai/provider/resumption_token.rb', line 13 def total @total end |
#until ⇒ Object (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 = {} matches = /(.+):(\d+)$/.match(token_string) [:last] = matches.captures[1].to_i last_id = nil total = nil parts = matches.captures[0].split('.') [:metadata_prefix] = parts.shift parts.each do |part| case part when /^s/ [:set] = part.sub(/^s\(/, '').sub(/\)$/, '') when /^f/ [:from] = Time.parse(part.sub(/^f\(/, '').sub(/\)$/, '')) when /^u/ [: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, , 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_hash ⇒ Object
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_s ⇒ Object
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_xml ⇒ Object
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 |