Class: PriceHubble::Client::Valuation

Inherits:
Base
  • Object
show all
Defined in:
lib/price_hubble/client/valuation.rb

Overview

A high level client library for the PriceHubble Valuation API.

Constant Summary

Constants included from Utils::Request

Utils::Request::CONTENT_TYPE

Instance Method Summary collapse

Methods inherited from Base

#configure, #connection

Methods included from Utils::Bangers

bangers

Methods included from Utils::Decision

#decision

Methods included from Utils::Response

#assign_entity, #bang_entity, #failed?, #status?

Methods included from Utils::Request

#use_authentication, #use_default_context

Instance Method Details

#assign_valuations(data, request) ⇒ Proc

Map and assign the valuation response to our local PriceHubble::Valuation representation. While taking care of the multi-dimensional array structure which is reflected from the request data. You get back a lambda which you need to call to get the results (for use in a decision). The return values is a Array<PriceHubble::Valuation>.

rubocop:disable Metrics/MethodLength because of the request

to response mapping

rubocop:disable Metrics/AbcSize dito

Parameters:

Returns:

  • (Proc)

    the valuation mapping code



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/price_hubble/client/valuation.rb', line 48

def assign_valuations(data, request)
  lambda do
    # valuations[i][j] contains the valuation for property i on date j
    data.each_with_index.map do |valuations, property_idx|
      valuations.each_with_index.map do |valuation, date_idx|
        # Fetch the request data for this valuation and
        # extend the raw data to build a local representation
        valuation.property = request.properties[property_idx]
        valuation.valuation_date = request.valuation_dates[date_idx]
        valuation.deal_type = request.deal_type
        valuation.country_code = request.country_code
        # Build the local representation from the raw data
        PriceHubble::Valuation.new(valuation)
      end
    end.flatten
  end
end

#property_value(request, **args) ⇒ Array<PriceHubble::Valuation>?

Perform a full-fledged valuation request.

rubocop:disable Metrics/MethodLength because of the request handling rubocop:disable Metrics/AbcSize dito

Parameters:

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/price_hubble/client/valuation.rb', line 16

def property_value(request, **args)
  data = request.attributes(sanitize: true)
  res = connection.post do |req|
    req.path = '/api/v1/valuation/property_value'
    req.body = data
    use_default_context(req, :property_value)
    use_authentication(req)
  end
  decision(bang: args.fetch(:bang, false)) do |result|
    result.bang(&bang_entity(request, res, data))
    result.good(&assign_valuations(res.body.valuations, request))
    successful?(res)
  end
end