Class: Pwb::ImportMapper

Inherits:
Object
  • Object
show all
Defined in:
app/services/pwb/import_mapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mls_name) ⇒ ImportMapper

Returns a new instance of ImportMapper.



6
7
8
9
# File 'app/services/pwb/import_mapper.rb', line 6

def initialize(mls_name)
  mls_mapping = Pwb::ImportMapping.find_by_name(mls_name)
  self.mls_mapping = mls_mapping
end

Instance Attribute Details

#mls_mappingObject

Returns the value of attribute mls_mapping.



4
5
6
# File 'app/services/pwb/import_mapper.rb', line 4

def mls_mapping
  @mls_mapping
end

Instance Method Details

#map_property(mls_property) ⇒ Object



11
12
13
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
# File 'app/services/pwb/import_mapper.rb', line 11

def map_property(mls_property)
  mapped_property = {}

  # direct_mappings is a hash of MLS fieldnames and the equivalent fieldname in pwb
  # eg /pwb/config/import_mappings/mls_olr.json
  direct_mappings = mls_mapping.mappings

  # mapped_property_old = mls_property.to_hash.map {|k, v| [direct_mappings[k], v] }.to_h
  # return mapped_property_old.except(nil)

  direct_mappings.each do |mapping|
    origin_field_key = mapping[0]
    target_field = mapping[1]
    field_value = mls_property[origin_field_key].blank? ? target_field["default"] : mls_property[origin_field_key]
    mapped_property[target_field["fieldName"]] = field_value
  end

  # nested_mappings is a hash of MLS fieldnames and the equivalent fieldname in pwb
  nested_mappings = mls_mapping.nested_mappings


  if nested_mappings
    nested_key = nested_mappings["key"]
    nested_mappings["mappings"].each do |mapping|
      origin_field_key = mapping[0]
      target_field = mapping[1]
      if mls_property[nested_key].present? && mls_property[nested_key][origin_field_key].present?
        field_value = mls_property[nested_key][origin_field_key]
      else
        field_value = target_field["default"]
      end
      mapped_property[target_field["fieldName"]] = field_value
    end
  end


  # TODO: - figure out way of importing extras
  mapped_property
end