Class: MiniEtl::Source

Inherits:
Object
  • Object
show all
Includes:
Status
Defined in:
lib/mini_etl/source.rb

Overview

Source data from a give type and location

Constant Summary collapse

VALID_STATES =
{
  initialized: 0,
  validated: 1,
  sourced: 2,
  failed: 3
}.freeze
ACCEPTED_PARAMS =
%i[type location data].freeze

Constants included from Status

MiniEtl::Status::DEFAULT_STATES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Status

included

Constructor Details

#initialize(params = {}) ⇒ Source

Returns a new instance of Source.



19
20
21
22
23
24
25
# File 'lib/mini_etl/source.rb', line 19

def initialize(params = {})
  ACCEPTED_PARAMS.each do |param|
    instance_variable_set "@#{param}".to_sym, params[param]
  end
  @payload = []
  initialized!
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



15
16
17
# File 'lib/mini_etl/source.rb', line 15

def data
  @data
end

#locationObject

Returns the value of attribute location.



15
16
17
# File 'lib/mini_etl/source.rb', line 15

def location
  @location
end

#payloadObject (readonly)

Returns the value of attribute payload.



16
17
18
# File 'lib/mini_etl/source.rb', line 16

def payload
  @payload
end

#typeObject

Returns the value of attribute type.



15
16
17
# File 'lib/mini_etl/source.rb', line 15

def type
  @type
end

Instance Method Details

#fetchObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/mini_etl/source.rb', line 34

def fetch
  strategy = MiniEtl::Strategy.for(@type)

  if strategy && validated?
    @payload = strategy.fetch(self)
    sourced!
  else
    failed!
  end
end

#validateObject



27
28
29
30
31
32
# File 'lib/mini_etl/source.rb', line 27

def validate
  strategy = MiniEtl::Strategy.for(@type)
  return false && failed! if strategy.nil?

  strategy.validate(self).tap { |x| x ? validated! : failed! }
end