realogy 0.6.9 → 0.6.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4cd27d1efeb5f696a0c1e78300083b7ead01f7f380bd1ebf8c1d420b25dd870
4
- data.tar.gz: 5c275d47d826831f3fda7ec142e907069b7de334f2afd1d31bb84380caf5b40e
3
+ metadata.gz: 16b4f3e20df302badddd3af150745656f53fc1cbc3ea6457e82bd7ca4eed055c
4
+ data.tar.gz: 4a93d40d4c122ad052c2122083aaf2878d1ad466f8e2c68d90da171116e321ac
5
5
  SHA512:
6
- metadata.gz: c4f05a3939ca6c449d9b357cfb1581d8ba6f2dc031104b203c25f6bd056a5099c271dcfb26a0db087778dead92d7e5446efb954cdc03e850ff8d6d8aced19f6f
7
- data.tar.gz: a81cdbf6e7709007e584300014b1c09e65f617ac028ba9394493779aabf78463cac56542bd6988a51e4bd725efc2d4238cd5af28d700a2644ccc14a29eb03961
6
+ metadata.gz: 6de6ebb0b2ef83481f5d983c0028bb3b0918e450b6099476e863e0b22c63d0b858a7b18abec54d2a7b82c520beffe5218f3b84992070423574e832909300bd42
7
+ data.tar.gz: 10c3201b1dd9e24e0bc54ac2d1034266b18c9260278dc227ed3d94ea127cc7e789bb8d648b8a396449bb697028d596a87a57de9e8df67b769667aed2e95ec35c
@@ -152,6 +152,10 @@ module Realogy
152
152
  request['Ocp-Apim-Subscription-Key'] = Rails.application.credentials.dig(:realogy, :subscription_key)
153
153
  request['Authorization'] = "Bearer #{auth_token}"
154
154
  response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
155
+ http.open_timeout = 10 # seconds to wait for connection
156
+ http.read_timeout = 30 # seconds to wait for response
157
+ http.write_timeout = 10
158
+ http.ssl_timeout = 10
155
159
  http.max_retries = 10
156
160
  http.request(request)
157
161
  end
@@ -6,58 +6,97 @@ class Realogy::Entity < ApplicationRecord
6
6
  validates :last_update_on, presence: true
7
7
 
8
8
  def needs_updating?
9
- self.new_record? || self.last_update_on_changed? || self.data.blank?
9
+ new_record? || last_update_on_changed? || data.blank?
10
10
  end
11
11
 
12
- def self.triage hash
13
- @object = self.find_or_initialize_by(entity_id: [hash["entityId"], hash["id"]].compact.first)
12
+ def self.triage(hash)
13
+ entity_id = [hash["entityId"], hash["id"]].compact.first
14
+ @object = find_or_initialize_by(entity_id: entity_id)
14
15
  @object.last_update_on = hash["lastUpdateOn"].to_s.to_datetime
15
- @object.populate if @object.needs_updating?
16
+
17
+ if data_provided_in_hash?(hash)
18
+ @object.data = extract_entity_data(hash)
19
+ @object.save if @object.changed?
20
+ elsif @object.needs_updating?
21
+ @object.populate
22
+ end
16
23
  end
17
24
 
18
25
  def get_data
19
- call = ["get_", self.class.to_s.downcase.split("::").last, "_by_id"].join.to_sym
20
- Realogy::DataSync.client.__send__(call, self.entity_id)
26
+ retries = 0
27
+ max_retries = 3
28
+
29
+ begin
30
+ call = ["get_", self.class.to_s.downcase.split("::").last, "_by_id"].join.to_sym
31
+ Realogy::DataSync.client.__send__(call, entity_id)
32
+ rescue OpenSSL::SSL::SSLError, Net::OpenTimeout, Net::ReadTimeout, Errno::ECONNRESET => e
33
+ retries += 1
34
+ if retries <= max_retries
35
+ sleep_time = 2**retries # 2s, 4s, 8s
36
+ Rails.logger.warn "[Realogy::Entity] Retry #{retries}/#{max_retries} for #{entity_id} after
37
+ #{sleep_time}s (#{e.class})"
38
+ sleep(sleep_time)
39
+ retry
40
+ else
41
+ Rails.logger.error "[Realogy::Entity] Failed to fetch #{entity_id} after #{max_retries} retries:
42
+ #{e.message}"
43
+ raise
44
+ end
45
+ end
21
46
  end
22
47
 
23
48
  def populate
24
49
  result = get_data
25
50
  self.data = result unless result.blank?
26
- self.save if self.changed?
51
+ save if changed?
27
52
  end
28
53
 
29
54
  def dig_for_array(*path)
30
- return nil unless (json = self.data).is_a?(Hash)
55
+ return nil unless (json = data).is_a?(Hash)
31
56
  (v = json.dig(*path)).is_a?(Array) ? v : nil
32
57
  end
33
58
 
34
59
  def dig_for_boolean(*path)
35
- return nil unless (json = self.data).is_a?(Hash)
60
+ return nil unless (json = data).is_a?(Hash)
36
61
  (v = json.dig(*path)).to_s.upcase.eql?("TRUE") ? true : nil
37
62
  end
38
63
 
39
64
  def dig_for_datetime(*path)
40
- self.data.dig(*path).to_datetime rescue nil
65
+ data.dig(*path).to_datetime rescue nil
41
66
  end
42
67
 
43
68
  def dig_for_decimal(*path)
44
- return nil unless (json = self.data).is_a?(Hash)
69
+ return nil unless (json = data).is_a?(Hash)
45
70
  (v = json.dig(*path).to_f) != 0.0 ? v : nil
46
71
  end
47
72
 
48
73
  def dig_for_hash(*path)
49
- return nil unless (json = self.data).is_a?(Hash)
74
+ return nil unless (json = data).is_a?(Hash)
50
75
  (v = json.dig(*path)).is_a?(Hash) ? v : nil
51
76
  end
52
77
 
53
78
  def dig_for_integer(*path)
54
- return nil unless (json = self.data).is_a?(Hash)
79
+ return nil unless (json = data).is_a?(Hash)
55
80
  (v = json.dig(*path).to_i) != 0 ? v : nil
56
81
  end
57
82
 
58
83
  def dig_for_string(*path)
59
- return nil unless (json = self.data).is_a?(Hash)
84
+ return nil unless (json = data).is_a?(Hash)
60
85
  (v = json.dig(*path)).to_s.present? ? (v.eql?("0") ? nil : v) : nil
61
86
  end
62
87
 
88
+ class << self
89
+ private
90
+
91
+ def data_provided_in_hash?(hash)
92
+ # Check if hash contains entity data beyond just metadata fields
93
+ metadata_keys = %w[entityId id lastUpdateOn class action]
94
+ (hash.keys - metadata_keys).any?
95
+ end
96
+
97
+ def extract_entity_data(hash)
98
+ # Remove metadata fields and return just the entity data
99
+ hash.except("class", "action")
100
+ end
101
+ end
63
102
  end
@@ -1,3 +1,3 @@
1
1
  module Realogy
2
- VERSION = "0.6.9"
2
+ VERSION = "0.6.11"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: realogy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.9
4
+ version: 0.6.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Edlund