Class: Realogy::Entity

Inherits:
ApplicationRecord show all
Defined in:
lib/realogy/app/models/realogy/entity.rb

Direct Known Subclasses

Agent, Company, Listing, Office, Team

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.triage(hash) ⇒ Object



12
13
14
15
16
# File 'lib/realogy/app/models/realogy/entity.rb', line 12

def self.triage hash
  @object = self.find_or_initialize_by(entity_id: [hash["entityId"], hash["id"]].compact.first)
  @object.last_update_on = hash["lastUpdateOn"].to_s.to_datetime
  @object.populate if @object.needs_updating?
end

Instance Method Details

#dig_for_array(*path) ⇒ Object



29
30
31
32
# File 'lib/realogy/app/models/realogy/entity.rb', line 29

def dig_for_array(*path)
  return nil unless (json = self.data).is_a?(Hash)
  (v = json.dig(*path)).is_a?(Array) ? v : nil
end

#dig_for_boolean(*path) ⇒ Object



34
35
36
37
# File 'lib/realogy/app/models/realogy/entity.rb', line 34

def dig_for_boolean(*path)
  return nil unless (json = self.data).is_a?(Hash)
  (v = json.dig(*path)).to_s.upcase.eql?("TRUE") ? true : nil
end

#dig_for_datetime(*path) ⇒ Object



39
40
41
# File 'lib/realogy/app/models/realogy/entity.rb', line 39

def dig_for_datetime(*path)
  self.data.dig(*path).to_datetime rescue nil
end

#dig_for_decimal(*path) ⇒ Object



43
44
45
46
# File 'lib/realogy/app/models/realogy/entity.rb', line 43

def dig_for_decimal(*path)
  return nil unless (json = self.data).is_a?(Hash)
  (v = json.dig(*path).to_f) != 0.0 ? v : nil
end

#dig_for_hash(*path) ⇒ Object



48
49
50
51
# File 'lib/realogy/app/models/realogy/entity.rb', line 48

def dig_for_hash(*path)
  return nil unless (json = self.data).is_a?(Hash)
  (v = json.dig(*path)).is_a?(Hash) ? v : nil
end

#dig_for_integer(*path) ⇒ Object



53
54
55
56
# File 'lib/realogy/app/models/realogy/entity.rb', line 53

def dig_for_integer(*path)
  return nil unless (json = self.data).is_a?(Hash)
  (v = json.dig(*path).to_i) != 0 ? v : nil
end

#dig_for_string(*path) ⇒ Object



58
59
60
61
# File 'lib/realogy/app/models/realogy/entity.rb', line 58

def dig_for_string(*path)
  return nil unless (json = self.data).is_a?(Hash)
  (v = json.dig(*path)).to_s.present? ? (v.eql?("0") ? nil : v) : nil
end

#get_dataObject



18
19
20
21
# File 'lib/realogy/app/models/realogy/entity.rb', line 18

def get_data
  call = ["get_", self.class.to_s.downcase.split("::").last, "_by_id"].join.to_sym
  Realogy::DataSync.client.__send__(call, self.entity_id)
end

#needs_updating?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/realogy/app/models/realogy/entity.rb', line 8

def needs_updating?
  self.new_record? || self.last_update_on_changed? || self.data.blank?
end

#populateObject



23
24
25
26
27
# File 'lib/realogy/app/models/realogy/entity.rb', line 23

def populate
  result = get_data
  self.data = result unless result.blank?
  self.save if self.changed?
end