Class: Tiler::DataIngestionService

Inherits:
Object
  • Object
show all
Defined in:
app/services/tiler/data_ingestion_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_source, records, via:, user: nil) ⇒ DataIngestionService

Returns a new instance of DataIngestionService.



5
6
7
8
9
10
11
12
# File 'app/services/tiler/data_ingestion_service.rb', line 5

def initialize(data_source, records, via:, user: nil)
  @data_source = data_source
  @records     = records.is_a?(Hash) ? [ records ] : Array(records)
  @via         = via
  @user        = user
  @accepted    = 0
  @errors      = []
end

Instance Attribute Details

#acceptedObject (readonly)

Returns the value of attribute accepted.



3
4
5
# File 'app/services/tiler/data_ingestion_service.rb', line 3

def accepted
  @accepted
end

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'app/services/tiler/data_ingestion_service.rb', line 3

def errors
  @errors
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/tiler/data_ingestion_service.rb', line 14

def call
  ActiveRecord::Base.transaction do
    @records.each_with_index do |raw, i|
      payload = coerce_payload(raw)
      record  = @data_source.data_records.build(
        payload:      payload.to_json,
        recorded_at:  extract_recorded_at(raw) || Time.current,
        source_ref:   raw["source_ref"] || raw[:source_ref],
        ingested_via: @via
      )
      if record.save
        @accepted += 1
      else
        @errors << { index: i, messages: record.errors.full_messages }
      end
    end
  end
  self
end

#success?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'app/services/tiler/data_ingestion_service.rb', line 34

def success?
  @errors.empty?
end