Class: Merritt::TIND::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/merritt/tind/record.rb

Constant Summary collapse

IDENTIFIER =
'identifier'.freeze
DATESTAMP =
'datestamp'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier:, datestamp:, oai_metadata: nil) ⇒ Record

Returns a new instance of Record.



14
15
16
17
18
# File 'lib/merritt/tind/record.rb', line 14

def initialize(identifier:, datestamp:, oai_metadata: nil)
  @identifier = identifier
  @datestamp = datestamp
  @metadata = 
end

Instance Attribute Details

#datestampObject (readonly)

Returns the value of attribute datestamp.



11
12
13
# File 'lib/merritt/tind/record.rb', line 11

def datestamp
  @datestamp
end

#identifierObject (readonly)

Returns the value of attribute identifier.



10
11
12
# File 'lib/merritt/tind/record.rb', line 10

def identifier
  @identifier
end

#metadataObject (readonly)

Returns the value of attribute metadata.



12
13
14
# File 'lib/merritt/tind/record.rb', line 12

def 
  @metadata
end

Class Method Details

.earlier(r1, r2) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/merritt/tind/record.rb', line 80

def earlier(r1, r2)
  return r1 if r2.nil?
  return r2 if r1.nil?
  return r1 if (r1.datestamp <=> r2.datestamp) < 0

  r2
end

.from_hash(h) ⇒ Object



88
89
90
91
92
# File 'lib/merritt/tind/record.rb', line 88

def from_hash(h)
  return unless h

  Record.new(identifier: h[IDENTIFIER], datestamp: h[DATESTAMP])
end

.from_oai(oai_record) ⇒ Object

Constructs a new Merritt::TIND::Record wrapping the specified record.

Parameters:

  • oai_record (OAI::Record)

    An OAI record as returned by ‘OAI::Client`

Raises:

  • (ArgumentError)


97
98
99
100
101
102
103
104
# File 'lib/merritt/tind/record.rb', line 97

def from_oai(oai_record)
  raise ArgumentError, "can't parse nil record" unless oai_record

  header = oai_record.header
  identifier = header.identifier
  datestamp = header.datestamp && Time.parse(header.datestamp)
  Record.new(identifier: identifier, datestamp: datestamp, oai_metadata: oai_record.)
end

.later(r1, r2) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/merritt/tind/record.rb', line 72

def later(r1, r2)
  return r1 if r2.nil?
  return r2 if r1.nil?
  return r1 if (r1.datestamp <=> r2.datestamp) > 0

  r2
end

Instance Method Details

#content_uriObject



51
52
53
54
55
56
57
58
59
# File 'lib/merritt/tind/record.rb', line 51

def content_uri
  @content_uri ||= begin
    # TODO: something smarter when we know the real requirements
    content_url = dc_identifiers.find do |dc_id|
      dc_id.start_with?('http') && dc_id.end_with?('jpg')
    end
    content_url && URI.parse(content_url)
  end
end

#dc_creatorsObject



47
48
49
# File 'lib/merritt/tind/record.rb', line 47

def dc_creators
  @dc_creators ||= REXML::XPath.match(, './/dc:creator').map(&:text)
end

#dc_datesObject



35
36
37
38
39
40
41
# File 'lib/merritt/tind/record.rb', line 35

def dc_dates
  @dc_dates ||= begin
    REXML::XPath.match(, './/dc:date')
      .map(&:text)
      .map { |t| Time.parse(t) }
  end
end

#dc_identifiersObject



31
32
33
# File 'lib/merritt/tind/record.rb', line 31

def dc_identifiers
  @dc_identifiers ||= REXML::XPath.match(, './/dc:identifier').map(&:text)
end

#dc_titlesObject



43
44
45
# File 'lib/merritt/tind/record.rb', line 43

def dc_titles
  @dc_titles ||= REXML::XPath.match(, './/dc:title').map(&:text)
end

#ercObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/merritt/tind/record.rb', line 20

def erc
  # TODO: something smarter when we know the real requirements
  {
    'what' => identifier,
    'where' => local_id,
    'when' => dc_dates.first || datestamp,
    'when/created' => dc_dates.first || datestamp,
    'when/modified' => datestamp
  }
end

#local_idObject



61
62
63
64
# File 'lib/merritt/tind/record.rb', line 61

def local_id
  # TODO: something smarter when we know the real requirements
  dc_identifiers.first || identifier
end

#to_hObject



66
67
68
# File 'lib/merritt/tind/record.rb', line 66

def to_h
  { IDENTIFIER => identifier, DATESTAMP => datestamp }
end