reso_api 0.4.4 → 0.4.7

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: cd4c435432b3f47213fb549d9568c9cdc64ad8c1129154d268aa78c30ab1bca1
4
- data.tar.gz: 8a1227afa9dd827e231fd02a1df9c205bb1995b7cb54bec36e4e993638bfe19b
3
+ metadata.gz: 9b02d03482a2216a50877b15d2f8ff6951294625470fb09d9fa1753ece52e4f6
4
+ data.tar.gz: 3a7d7c7b2dc6af22e9af6575e2556a35b117af5e0839edf004d8f04d7f68ceb5
5
5
  SHA512:
6
- metadata.gz: de3f5246183a14b62a0ddbe9697a8c67127a9e3a6add49f2a5ae32957406746fc49af9a8f2337b3fa7d87183e0eca6e4033bad89c327376dda076baee7150db6
7
- data.tar.gz: 510ee54a5972125a86ebbc141272a6f6793e3dafb0deb63d41ac970a8ff9df0f93106994b03e646dc6a7f117a3ac9f8ae6ff5a4c6e9fa910e160208bd52b0fb4
6
+ metadata.gz: db880d0367a2a249bb80bca2fddde68fd6aa70a8ab7a54d636a3629c88f2a1538b7adc0c89b9750af68154837c0b5782c1eeb839e2152c9915532ad52b80c70c
7
+ data.tar.gz: 2451fdda3931aad5aa2e25d2af334d262988ad1aaef0484fe8f96c368a5e39bb6cb0f1a37ae9a38b5dc93c5e369f0b61e86be23b7716befcf65f622a597308ee
@@ -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],
@@ -61,6 +62,7 @@ module RESO
61
62
  "$count": hash[:count].to_s.presence,
62
63
  "$debug": hash[:debug]
63
64
  }.compact
65
+ File.open("timespans", 'a') { |file| file.write("Timestamp\tDuration\tCode\tMessage\tPath\n") }
64
66
  if !block.nil?
65
67
  response = perform_call(endpoint, params)
66
68
  response["value"].each{|hash| block.call(hash)} if response["value"].class.eql?(Array)
@@ -106,12 +108,17 @@ module RESO
106
108
  if expiration > DateTime.now.utc
107
109
  return payload.token
108
110
  else
109
- @oauth2_payload = oauth2_client.client_credentials.get_token
110
- File.write(oauth2_token_path, @oauth2_payload.to_hash.to_json)
111
+ @oauth2_payload = fresh_oauth2_payload
111
112
  return @oauth2_payload.token
112
113
  end
113
114
  end
114
115
 
116
+ def fresh_oauth2_payload
117
+ @oauth2_payload = oauth2_client.client_credentials.get_token
118
+ File.write(oauth2_token_path, @oauth2_payload.to_hash.to_json)
119
+ return @oauth2_payload
120
+ end
121
+
115
122
  def oauth2_token_path
116
123
  File.join(Dir.tmpdir, [base_url.parameterize, "-oauth-token.json"].join)
117
124
  end
@@ -137,17 +144,34 @@ module RESO
137
144
 
138
145
  def perform_call(endpoint, params)
139
146
  uri = uri_for_endpoint(endpoint)
147
+ retries = 0
140
148
  if params.present?
141
149
  query = params.present? ? URI.encode_www_form(params).gsub("+", " ") : ""
142
150
  uri.query && uri.query.length > 0 ? uri.query += '&' + query : uri.query = query
143
151
  return URI::decode(uri.request_uri) if params.dig(:$debug).present?
144
152
  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.request(request)
153
+ begin
154
+ req = Net::HTTP::Get.new(uri.request_uri)
155
+ req['Authorization'] = "Bearer #{oauth2_token}"
156
+ res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
157
+ http.request(req)
158
+ end
159
+ response = JSON(res.body) rescue res.body
160
+ if response.is_a?(String) && response.include?('Bad Gateway')
161
+ raise StandardError
162
+ elsif response.is_a?(String) && response.include?('Unauthorized')
163
+ fresh_oauth2_payload
164
+ raise StandardError
165
+ elsif response.is_a?(Hash) && response.has_key?("error")
166
+ raise StandardError
167
+ end
168
+ rescue Net::ReadTimeout, StandardError
169
+ if (retries += 1) <= 5
170
+ sleep 10
171
+ retry
172
+ end
149
173
  end
150
- return JSON(response.body) rescue response.body
174
+ return response
151
175
  end
152
176
 
153
177
  end
@@ -1,2 +1,5 @@
1
1
  module RESO
2
+ def self.table_name_prefix
3
+ "reso_"
4
+ end
2
5
  end
@@ -1,3 +1,3 @@
1
1
  module ResoApi
2
- VERSION = "0.4.4"
2
+ VERSION = "0.4.7"
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.4
4
+ version: 0.4.7
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-25 00:00:00.000000000 Z
11
+ date: 2022-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler