Class: Stash::Harvester::HarvestTask

Inherits:
Object
  • Object
show all
Defined in:
lib/stash/harvester/harvest_task.rb

Overview

Generic superclass of protocol-specific harvest tasks. Subclasses should override #harvest_records, in conjunction with an appropriate SourceConfig, to perform the actual harvest.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, from_time: nil, until_time: nil) ⇒ HarvestTask

Constructs a new HarvestTask with the specified datetime range. Note that the datetime range must be in UTC.

Parameters:

  • config (HarvestConfig)

    the configuration to be used by this task.

  • from_time (Time, nil) (defaults to: nil)

    the start (inclusive) of the datestamp range for selective harvesting. If from_time is omitted, harvesting will extend back to the earliest datestamp in the repository. (Optional)

  • until_time (Time, nil) (defaults to: nil)

    the end (inclusive) of the datestamp range for selective harvesting. If until_time is omitted, harvesting will extend forward to the latest datestamp in the repository. (Optional)

Raises:

  • (RangeError)

    if from_time is later than until_time.

  • (ArgumentError)

    if from_time or until_time is not in UTC.


38
39
40
41
# File 'lib/stash/harvester/harvest_task.rb', line 38

def initialize(config:, from_time: nil, until_time: nil)
  @config = config
  @from_time, @until_time = valid_range(from_time, until_time)
end

Instance Attribute Details

#configObject (readonly)


Accessors


14
15
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/stash/harvester/harvest_task.rb', line 14

class HarvestTask

  # ------------------------------------------------------------
  # Accessors

  attr_reader :config
  attr_reader :from_time
  attr_reader :until_time

  # ------------------------------------------------------------
  # Initializer

  # Constructs a new +HarvestTask+ with the specified datetime range.
  # Note that the datetime range must be in UTC.
  #
  # @param config [HarvestConfig] the configuration to be used by this task.
  # @param from_time [Time, nil] the start (inclusive) of the datestamp range for selective harvesting.
  #   If +from_time+ is omitted, harvesting will extend back to the earliest datestamp in the
  #   repository. (Optional)
  # @param until_time [Time, nil] the end (inclusive) of the datestamp range for selective harvesting.
  #   If +until_time+ is omitted, harvesting will extend forward to the latest datestamp in the
  #   repository. (Optional)
  # @raise [RangeError] if +from_time+ is later than +until_time+.
  # @raise [ArgumentError] if +from_time+ or +until_time+ is not in UTC.
  def initialize(config:, from_time: nil, until_time: nil)
    @config = config
    @from_time, @until_time = valid_range(from_time, until_time)
  end

  # Provides access to the harvested records as a lazy enumeration. Implementations should make only
  # the minimum network requests needed to satisfy the needs of the client. For instance, in a protocol
  # that requires record-by-record downloading, if the client requests only the first record from the
  # enumeration, the implementation should only download that record. In a protocol that provides
  # 1000-record 'pages', the implementation should not download the second page unless the client asks
  # for more than 1000 records.
  #
  # @return [Enumerator::Lazy<HarvestedRecord>]
  #   A lazy enumerator of the harvested records
  def harvest_records
    fail "#{self.class} should override #harvest_records to harvest records, but it doesn't"
  end

  # ------------------------------
  # Private methods

  private

  # ------------------------------
  # Parameter validators

  def utc_or_nil(time)
    if time && !time.utc?
      fail ArgumentError, "time #{time}| must be in UTC"
    else
      time
    end
  end

  def valid_range(from_time, until_time)
    if from_time && until_time && from_time.to_i > until_time.to_i
      fail RangeError, "from_time #{from_time} must be <= until_time #{until_time}"
    else
      [utc_or_nil(from_time), utc_or_nil(until_time)]
    end
  end

end

#from_timeTime? (readonly)

Returns the start (inclusive) of the datestamp range for selective harvesting.

Returns:

  • (Time, nil)

    the start (inclusive) of the datestamp range for selective harvesting.


14
15
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/stash/harvester/harvest_task.rb', line 14

class HarvestTask

  # ------------------------------------------------------------
  # Accessors

  attr_reader :config
  attr_reader :from_time
  attr_reader :until_time

  # ------------------------------------------------------------
  # Initializer

  # Constructs a new +HarvestTask+ with the specified datetime range.
  # Note that the datetime range must be in UTC.
  #
  # @param config [HarvestConfig] the configuration to be used by this task.
  # @param from_time [Time, nil] the start (inclusive) of the datestamp range for selective harvesting.
  #   If +from_time+ is omitted, harvesting will extend back to the earliest datestamp in the
  #   repository. (Optional)
  # @param until_time [Time, nil] the end (inclusive) of the datestamp range for selective harvesting.
  #   If +until_time+ is omitted, harvesting will extend forward to the latest datestamp in the
  #   repository. (Optional)
  # @raise [RangeError] if +from_time+ is later than +until_time+.
  # @raise [ArgumentError] if +from_time+ or +until_time+ is not in UTC.
  def initialize(config:, from_time: nil, until_time: nil)
    @config = config
    @from_time, @until_time = valid_range(from_time, until_time)
  end

  # Provides access to the harvested records as a lazy enumeration. Implementations should make only
  # the minimum network requests needed to satisfy the needs of the client. For instance, in a protocol
  # that requires record-by-record downloading, if the client requests only the first record from the
  # enumeration, the implementation should only download that record. In a protocol that provides
  # 1000-record 'pages', the implementation should not download the second page unless the client asks
  # for more than 1000 records.
  #
  # @return [Enumerator::Lazy<HarvestedRecord>]
  #   A lazy enumerator of the harvested records
  def harvest_records
    fail "#{self.class} should override #harvest_records to harvest records, but it doesn't"
  end

  # ------------------------------
  # Private methods

  private

  # ------------------------------
  # Parameter validators

  def utc_or_nil(time)
    if time && !time.utc?
      fail ArgumentError, "time #{time}| must be in UTC"
    else
      time
    end
  end

  def valid_range(from_time, until_time)
    if from_time && until_time && from_time.to_i > until_time.to_i
      fail RangeError, "from_time #{from_time} must be <= until_time #{until_time}"
    else
      [utc_or_nil(from_time), utc_or_nil(until_time)]
    end
  end

end

#until_timeTime? (readonly)

Returns the end (inclusive) of the datestamp range for selective harvesting.

Returns:

  • (Time, nil)

    the end (inclusive) of the datestamp range for selective harvesting.


14
15
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/stash/harvester/harvest_task.rb', line 14

class HarvestTask

  # ------------------------------------------------------------
  # Accessors

  attr_reader :config
  attr_reader :from_time
  attr_reader :until_time

  # ------------------------------------------------------------
  # Initializer

  # Constructs a new +HarvestTask+ with the specified datetime range.
  # Note that the datetime range must be in UTC.
  #
  # @param config [HarvestConfig] the configuration to be used by this task.
  # @param from_time [Time, nil] the start (inclusive) of the datestamp range for selective harvesting.
  #   If +from_time+ is omitted, harvesting will extend back to the earliest datestamp in the
  #   repository. (Optional)
  # @param until_time [Time, nil] the end (inclusive) of the datestamp range for selective harvesting.
  #   If +until_time+ is omitted, harvesting will extend forward to the latest datestamp in the
  #   repository. (Optional)
  # @raise [RangeError] if +from_time+ is later than +until_time+.
  # @raise [ArgumentError] if +from_time+ or +until_time+ is not in UTC.
  def initialize(config:, from_time: nil, until_time: nil)
    @config = config
    @from_time, @until_time = valid_range(from_time, until_time)
  end

  # Provides access to the harvested records as a lazy enumeration. Implementations should make only
  # the minimum network requests needed to satisfy the needs of the client. For instance, in a protocol
  # that requires record-by-record downloading, if the client requests only the first record from the
  # enumeration, the implementation should only download that record. In a protocol that provides
  # 1000-record 'pages', the implementation should not download the second page unless the client asks
  # for more than 1000 records.
  #
  # @return [Enumerator::Lazy<HarvestedRecord>]
  #   A lazy enumerator of the harvested records
  def harvest_records
    fail "#{self.class} should override #harvest_records to harvest records, but it doesn't"
  end

  # ------------------------------
  # Private methods

  private

  # ------------------------------
  # Parameter validators

  def utc_or_nil(time)
    if time && !time.utc?
      fail ArgumentError, "time #{time}| must be in UTC"
    else
      time
    end
  end

  def valid_range(from_time, until_time)
    if from_time && until_time && from_time.to_i > until_time.to_i
      fail RangeError, "from_time #{from_time} must be <= until_time #{until_time}"
    else
      [utc_or_nil(from_time), utc_or_nil(until_time)]
    end
  end

end

Instance Method Details

#harvest_recordsEnumerator::Lazy<HarvestedRecord>

Provides access to the harvested records as a lazy enumeration. Implementations should make only the minimum network requests needed to satisfy the needs of the client. For instance, in a protocol that requires record-by-record downloading, if the client requests only the first record from the enumeration, the implementation should only download that record. In a protocol that provides 1000-record 'pages', the implementation should not download the second page unless the client asks for more than 1000 records.

Returns:

  • (Enumerator::Lazy<HarvestedRecord>)

    A lazy enumerator of the harvested records


52
53
54
# File 'lib/stash/harvester/harvest_task.rb', line 52

def harvest_records
  fail "#{self.class} should override #harvest_records to harvest records, but it doesn't"
end