Class: Estated::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/estated/client.rb

Class Method Summary collapse

Class Method Details

.get_property(params, skip_cache: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/estated/client.rb', line 9

def self.get_property(params, skip_cache: false)
  with_cache('/property', params, skip_cache: skip_cache) do
    query = params.merge(token: Estated.config.api_key)
    response = self.get('/property', query: query)

    if response.parsed_response.is_a?(Hash)
      json = response.parsed_response

      # Don't cache if there's no data
      [json, !!json['data']]
    else
      [{ 'response' => response }, false]
    end
  end
end

.with_cache(path, params, skip_cache:) ⇒ Object



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

def self.with_cache(path, params, skip_cache:)
  if !skip_cache && Estated.config.cache_enabled?
    json = RequestCache.read(path, params)
    return json if json
  end

  result, result_is_cacheable = yield

  if Estated.config.cache_enabled? && result_is_cacheable
    # Cache original request
    RequestCache.write(path, params, result)

    # Also cache fips/apn request format
    fips = result.dig('data', 'parcel', 'fips_code')
    apn = result.dig('data', 'parcel', 'apn_unformatted')

    if fips && apn
      RequestCache.write(path, { fips: fips, apn: apn }, result)
    end
  end

  result
end