reso_api 0.4.6 → 0.4.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7e36e84a9836dcd15e9fd545c3b4eb4b42759771dc9593d410de6ed8d6558ff4
4
- data.tar.gz: ff23c4f1a94ee411f42a8d2f25e05e39d20a1e47a13ea2f5a0314b2a31a36b45
3
+ metadata.gz: 429940488232e917ca2fec8f1c5bbdd8d3bff37fb50f5b2d47d9ff817067abac
4
+ data.tar.gz: 7f0736cd242987fc19c0b6a0286b2ceef11f0dc363b31d8a161e02756d5456b1
5
5
  SHA512:
6
- metadata.gz: abd9c1f86ec78924c29b13a23c52b27e5f0ebfdcc888d52e3391a4d25ba606ccd3b49d4e4fba36339ebd14f22c5415244a81ab7201afbc9e5a9de5ec597c5a03
7
- data.tar.gz: a3f09fb8c92dd56d1b8f1d0c5e24d5321d33d2d218de2172f50999ea5be02e46264e335bbb33dde12a806ea3e2ebb51a1381f4448c1ca4f771c1de998a6b1f8c
6
+ metadata.gz: 3e5a50322a425cbde496039eaaa36bfdf255d08a49df1f148871fd01d3b809ff55e60bd070d74dc5c47bae5e80d45005ea7e0453c17fb981d665317d2804b23c
7
+ data.tar.gz: 89871becd21ea774f8763fb5e2e25e8b0c922a28bfe747709ad29c39b4e2d7e353fbfcc85dc85cf6d0474ff408ef380fab64caada98fa68613f01738e5d43cd4
data/README.md CHANGED
@@ -145,7 +145,7 @@ client.properties(expand: "Media")
145
145
 
146
146
  #### Automatically iterate over all results
147
147
 
148
- By passing a block to Media, Member, Office, and Property resource calls, subsequent paginated calls will automatically be made until the whole result set has been traversed. The block provided is executed for each returned object hash.
148
+ By passing a block to Media, Member, Office, and Property resource calls, subsequent paginated calls will automatically be made until the whole result set has been traversed. The block provided is executed for each returned object hash. The `batch` option can be used to return the full array of results.
149
149
 
150
150
  Here are a couple of examples of how that can be used:
151
151
 
@@ -157,6 +157,10 @@ end
157
157
  client.properties(filter: "StandardStatus eq 'Active'") do |hash|
158
158
  Listing.create(listing_key: hash['ListingKey'], data: hash)
159
159
  end
160
+
161
+ client.properties(filter: "StandardStatus eq 'Active'", batch: true) do |results|
162
+ Listing.insert_all(results) # Perform some batch operation
163
+ end
160
164
  ```
161
165
 
162
166
 
@@ -50,6 +50,7 @@ module RESO
50
50
  define_method method_name do |*args, &block|
51
51
  hash = args.first.is_a?(Hash) ? args.first : {}
52
52
  endpoint = FILTERABLE_ENDPOINTS[method_name]
53
+ response = {}
53
54
  params = {
54
55
  "$select": hash[:select],
55
56
  "$filter": hash[:filter],
@@ -63,10 +64,17 @@ module RESO
63
64
  }.compact
64
65
  if !block.nil?
65
66
  response = perform_call(endpoint, params)
66
- response["value"].each{|hash| block.call(hash)} if response["value"].class.eql?(Array)
67
+
68
+ if response["value"].class.eql?(Array)
69
+ hash[:batch] ? block.call(response["value"]) : response["value"].each{|hash| block.call(hash)}
70
+ end
71
+
67
72
  while (next_link = response["@odata.nextLink"]).present?
68
73
  response = perform_call(next_link, nil)
69
- response["value"].each{|hash| block.call(hash)} if response["value"].class.eql?(Array)
74
+
75
+ if response["value"].class.eql?(Array)
76
+ hash[:batch] ? block.call(response["value"]) : response["value"].each{|hash| block.call(hash)}
77
+ end
70
78
  end
71
79
  else
72
80
  return perform_call(endpoint, params)
@@ -106,12 +114,17 @@ module RESO
106
114
  if expiration > DateTime.now.utc
107
115
  return payload.token
108
116
  else
109
- @oauth2_payload = oauth2_client.client_credentials.get_token
110
- File.write(oauth2_token_path, @oauth2_payload.to_hash.to_json)
117
+ @oauth2_payload = fresh_oauth2_payload
111
118
  return @oauth2_payload.token
112
119
  end
113
120
  end
114
121
 
122
+ def fresh_oauth2_payload
123
+ @oauth2_payload = oauth2_client.client_credentials.get_token
124
+ File.write(oauth2_token_path, @oauth2_payload.to_hash.to_json)
125
+ return @oauth2_payload
126
+ end
127
+
115
128
  def oauth2_token_path
116
129
  File.join(Dir.tmpdir, [base_url.parameterize, "-oauth-token.json"].join)
117
130
  end
@@ -137,18 +150,36 @@ module RESO
137
150
 
138
151
  def perform_call(endpoint, params)
139
152
  uri = uri_for_endpoint(endpoint)
153
+ retries = 0
140
154
  if params.present?
141
155
  query = params.present? ? URI.encode_www_form(params).gsub("+", " ") : ""
142
156
  uri.query && uri.query.length > 0 ? uri.query += '&' + query : uri.query = query
143
157
  return URI::decode(uri.request_uri) if params.dig(:$debug).present?
144
158
  end
145
- request = Net::HTTP::Get.new(uri.request_uri)
146
- request['Authorization'] = "Bearer #{oauth2_token}"
147
- response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
148
- http.max_retries = 10
149
- http.request(request)
159
+ begin
160
+ req = Net::HTTP::Get.new(uri.request_uri)
161
+ req['Authorization'] = "Bearer #{oauth2_token}"
162
+ res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
163
+ http.request(req)
164
+ end
165
+ response = JSON(res.body) rescue res.body
166
+ if response.is_a?(String) && response.include?('Bad Gateway')
167
+ raise StandardError
168
+ elsif response.is_a?(String) && response.include?('Unauthorized')
169
+ fresh_oauth2_payload
170
+ raise StandardError
171
+ elsif response.is_a?(Hash) && response.has_key?("error")
172
+ raise StandardError
173
+ end
174
+ rescue Net::ReadTimeout, StandardError
175
+ if (retries += 1) <= 5
176
+ sleep 10
177
+ retry
178
+ else
179
+ raise
180
+ end
150
181
  end
151
- return JSON(response.body) rescue response.body
182
+ return response
152
183
  end
153
184
 
154
185
  end
@@ -1,3 +1,3 @@
1
1
  module ResoApi
2
- VERSION = "0.4.6"
2
+ VERSION = "0.4.9"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reso_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: 0.4.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Edlund
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-29 00:00:00.000000000 Z
11
+ date: 2022-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler