Class: Pho::OAI::Records

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

Overview

Collection of records

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(responseDate, from, to, records = [], token = nil, list_size = nil, cursor = nil) ⇒ Records

Returns a new instance of Records.



48
49
50
51
52
53
54
55
56
# File 'lib/pho/oai.rb', line 48

def initialize(responseDate, from, to, records=[], token=nil, list_size=nil, cursor=nil)
  @response_date = responseDate
  @from = from
  @to = to
  @records = records
  @resumption_token = token
  @list_size = list_size
  @cursor = cursor
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



46
47
48
# File 'lib/pho/oai.rb', line 46

def cursor
  @cursor
end

#fromObject (readonly)

Returns the value of attribute from.



46
47
48
# File 'lib/pho/oai.rb', line 46

def from
  @from
end

#list_sizeObject (readonly)

Returns the value of attribute list_size.



46
47
48
# File 'lib/pho/oai.rb', line 46

def list_size
  @list_size
end

#recordsObject (readonly)

Returns the value of attribute records.



46
47
48
# File 'lib/pho/oai.rb', line 46

def records
  @records
end

#response_dateObject (readonly)

Returns the value of attribute response_date.



46
47
48
# File 'lib/pho/oai.rb', line 46

def response_date
  @response_date
end

#resumption_tokenObject (readonly)

Returns the value of attribute resumption_token.



46
47
48
# File 'lib/pho/oai.rb', line 46

def resumption_token
  @resumption_token
end

#toObject (readonly)

Returns the value of attribute to.



46
47
48
# File 'lib/pho/oai.rb', line 46

def to
  @to
end

Class Method Details

.parse(response) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pho/oai.rb', line 58

def Records.parse(response)
  doc = REXML::Document.new(response)
  if doc.root.get_elements("error")[0] != nil
    code = doc.root.get_elements("error")[0].attributes["code"]
    if code == "noRecordsMatch"
      return nil
    else
      raise "Unable to list records: #{code}, #{doc.root.get_elements("error")[0].text}"
    end          
  end
  records = []        
  responseDate = doc.root.get_elements("responseDate")[0].text
  from = doc.root.get_elements("request")[0].attributes["from"]
  from = DateTime.parse( from ) unless from == nil          
  to = doc.root.get_elements("request")[0].attributes["until"]
  to = DateTime.parse( to ) unless to == nil 
  REXML::XPath.each( doc.root,  "//oai:header", {"oai" => "http://www.openarchives.org/OAI/2.0/"} ) do |header|
   uri = header.get_elements("identifier")[0].text
   datestamp = header.get_elements("datestamp")[0].text
   records << Record.new(uri, DateTime.parse( datestamp ) )
  end
  resumption_token = doc.root.get_elements("//resumptionToken")[0]
  if resumption_token != nil
      token = resumption_token.text
      list_size = resumption_token.attributes["completeListSize"].to_i if resumption_token.attributes["completeListSize"] != nil 
      cursor = resumption_token.attributes["cursor"].to_i if resumption_token.attributes["cursor"] != nil
  end
  return Records.new(DateTime.parse(responseDate), from, to, records, token, list_size, cursor)   
end

.read_from_store(store, from = nil, to = nil, resumption_token = nil) ⇒ Object

List records from a store



89
90
91
92
93
94
95
# File 'lib/pho/oai.rb', line 89

def Records.read_from_store(store, from=nil, to=nil, resumption_token=nil)
  resp = store.list_records(from, to, resumption_token)
  if resp.status != 200
    raise "Unable to list records"
  end
  return parse(resp.content)
end

.read_next_records(store, resumption_token) ⇒ Object

Fetch the next list of records from a store, using a resumption token

Returns nil if there are no records to retrieve. Can be used as a simple iterator, e.g:

records = store.list_records while records != nil

#do something with retrieved records
records = Records.read_next_records(store, records.resumption_token)

end

store

the store

resumption_token

previously retrieved resumption_token



109
110
111
112
113
114
115
# File 'lib/pho/oai.rb', line 109

def Records.read_next_records(store, resumption_token)
  #list is already complete
  if resumption_token == nil
    return nil
  end
  return Records.read_from_store(store, nil, nil, token)
end