Class: Tiler::DataIngestionService
- Inherits:
-
Object
- Object
- Tiler::DataIngestionService
- Defined in:
- app/services/tiler/data_ingestion_service.rb
Instance Attribute Summary collapse
-
#accepted ⇒ Object
readonly
Returns the value of attribute accepted.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(data_source, records, via:, user: nil) ⇒ DataIngestionService
constructor
A new instance of DataIngestionService.
- #success? ⇒ Boolean
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
#accepted ⇒ Object (readonly)
Returns the value of attribute accepted.
3 4 5 |
# File 'app/services/tiler/data_ingestion_service.rb', line 3 def accepted @accepted end |
#errors ⇒ Object (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
#call ⇒ Object
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. } end end end self end |
#success? ⇒ Boolean
34 35 36 |
# File 'app/services/tiler/data_ingestion_service.rb', line 34 def success? @errors.empty? end |