Class: Realogy::Entity

Inherits:
Object
  • Object
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
17
18
19
20
21
22
23
# File 'lib/realogy/app/models/realogy/entity.rb', line 12

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

  if data_provided_in_hash?(hash)
    @object.data = extract_entity_data(hash)
    @object.save if @object.changed?
  elsif @object.needs_updating?
    @object.populate
  end
end

Instance Method Details

#dig_for_array(*path) ⇒ Object



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

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

#dig_for_boolean(*path) ⇒ Object



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

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

#dig_for_datetime(*path) ⇒ Object



64
65
66
# File 'lib/realogy/app/models/realogy/entity.rb', line 64

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

#dig_for_decimal(*path) ⇒ Object



68
69
70
71
# File 'lib/realogy/app/models/realogy/entity.rb', line 68

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

#dig_for_hash(*path) ⇒ Object



73
74
75
76
# File 'lib/realogy/app/models/realogy/entity.rb', line 73

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

#dig_for_integer(*path) ⇒ Object



78
79
80
81
# File 'lib/realogy/app/models/realogy/entity.rb', line 78

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

#dig_for_string(*path) ⇒ Object



83
84
85
86
# File 'lib/realogy/app/models/realogy/entity.rb', line 83

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

#get_dataObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/realogy/app/models/realogy/entity.rb', line 25

def get_data
  retries = 0
  max_retries = 3

  begin
    call = ["get_", self.class.to_s.downcase.split("::").last, "_by_id"].join.to_sym
    Realogy::DataSync.client.__send__(call, entity_id)
  rescue OpenSSL::SSL::SSLError, Net::OpenTimeout, Net::ReadTimeout, Errno::ECONNRESET => e
    retries += 1
    if retries <= max_retries
      sleep_time = 2**retries  # 2s, 4s, 8s
      Rails.logger.warn "[Realogy::Entity] Retry #{retries}/#{max_retries} for #{entity_id} after 
#{sleep_time}s (#{e.class})"
      sleep(sleep_time)
      retry
    else
      Rails.logger.error "[Realogy::Entity] Failed to fetch #{entity_id} after #{max_retries} retries:
 #{e.message}"
      raise
    end
  end
end

#needs_updating?Boolean

Returns:

  • (Boolean)


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

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

#populateObject



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

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