The personal finance app for everyone (by everyone)
Ruby
fix(realie): support the v3 nested parcel record (#3627)
skitchbeatz195abc1822e2
diff --git a/app/models/provider/realie.rb b/app/models/provider/realie.rbindex addd676177..6a037a1072 100644--- a/app/models/provider/realie.rb+++ b/app/models/provider/realie.rb@@ -70,15 +70,15 @@ class Provider::Realie < Provider# Realie returns modelValue: 0 when it couldn't produce an AVM# estimate, so zero counts as absent and falls back to the assessed# total market value.- valuation = [ record["modelValue"], record["totalMarketValue"] ].find { |value| value.present? && value.to_d.positive? }+ valuation = [ model_value(record), total_market_value(record) ].find { |value| value.present? && value.to_d.positive? }raise Error.new(I18n.t("providers.realie.errors.no_valuation")) if valuation.nil?PropertyValuation.new(valuation: BigDecimal(valuation.to_s),currency: "USD",property_type: subtype_for_use_code(record["useCode"]),- year_built: record["yearBuilt"],- area_value: record["buildingArea"],+ year_built: year_built(record),+ area_value: building_area(record),area_unit: "sqft")end@@ -92,7 +92,7 @@ class Provider::Realie < Provider# street name can resolve to properties in other cities. A candidate# matches unless its returned city/ZIP contradict what the user entered.def location_match?(record, locality:, postal_code:)- returned_city = record["city"].to_s.strip+ returned_city = city(record).to_s.stripreturned_zip = record["zipCode"].to_s.strip.first(5)entered_city = locality.to_s.stripentered_zip = postal_code.to_s.strip.first(5)@@ -103,6 +103,48 @@ class Provider::Realie < Provider!(city_mismatch || zip_mismatch)end+ # Realie serves two response shapes for these endpoints. The shape is an+ # account-level setting that every API key on the account inherits: the+ # v2-compatible flat record, and the v3 nested parcel record, which is the+ # default for accounts created after the 2026-08-19 v3 release and a+ # one-way opt-in from the dashboard for older ones. The URL, the auth and+ # the top-level "property" key are identical in both, so the only way to+ # tell them apart is to look at the record itself.+ #+ # Read the flat key first and fall back to the nested path, which keeps+ # both shapes working without asking the user which one their account is+ # on. Mapping per https://docs.realie.ai/api-reference/v3/migration.+ def model_value(record)+ record["modelValue"] || record.dig("realieValuation", "ml", "value")+ end++ def total_market_value(record)+ record["totalMarketValue"] || record.dig("valuationInformation", "totalMarketValue")+ end++ def city(record)+ record["city"].presence || record.dig("propertyLocation", "city")+ end++ def year_built(record)+ record["yearBuilt"] || primary_building(record)&.dig("actualYearBuilt")+ end++ def building_area(record)+ record["buildingArea"] || primary_building(record)&.dig("buildingArea")+ end++ # The nested shape carries one entry per building on the parcel. Match the+ # flat shape, which describes the primary building only: buildingNumber 1+ # when the field is populated, otherwise the first entry.+ def primary_building(record)+ buildings = record.dig("buildingInformation", "buildings")+ return nil unless buildings.is_a?(Array)++ candidates = buildings.select { |building| building.is_a?(Hash) }+ candidates.find { |building| building["buildingNumber"].to_i == 1 } || candidates.first+ end+# Use codes arrive either as Realie's documented numeric codes (mapped# exactly above) or as free-form parcel descriptions, matched on# keywords. Unmatched codes leave the subtype unset rather than guessing.diff --git a/test/models/provider/realie_test.rb b/test/models/provider/realie_test.rbindex 7818e30f1c..0c8225f0fa 100644--- a/test/models/provider/realie_test.rb+++ b/test/models/provider/realie_test.rb@@ -23,6 +23,30 @@ class Provider::RealieTest < ActiveSupport::TestCase}.to_jsonend+ # The v3 nested parcel record, the shape served to accounts created after the+ # 2026-08-19 v3 release and to older accounts that opted in. Same URL, same+ # auth, same top-level "property" key -- only the field layout differs.+ # Trimmed to the fields this provider reads.+ def nested_address_lookup_body(overrides = {})+ {+ "property" => {+ "useCode" => "1001",+ "state" => "CA",+ "zipCode" => "90001",+ "propertyLocation" => { "city" => "LOS ANGELES" },+ "realieValuation" => {+ "ml" => { "value" => 420_000.0, "low" => 390_000.0, "high" => 450_000.0 }+ },+ "valuationInformation" => { "totalMarketValue" => 400_000 },+ "buildingInformation" => {+ "buildings" => [+ { "actualYearBuilt" => 1985, "buildingArea" => 1500.0, "livingArea" => 1400.0 }+ ]+ }+ }.merge(overrides)+ }.to_json+ end+test "fetches valuation and property attributes in a single request" dostub = stub_request(:get, "https://app.realie.ai/api/public/property/address/").with(@@ -171,6 +195,93 @@ class Provider::RealieTest < ActiveSupport::TestCaseendend+ test "reads the v3 nested parcel record" do+ stub = stub_request(:get, "https://app.realie.ai/api/public/property/address/")+ .with(+ query: { "address" => "123 Main Street", "state" => "CA" },+ headers: { "Authorization" => "test_api_key" }+ )+ .to_return(status: 200, body: nested_address_lookup_body)++ response = @provider.fetch_property_valuation(+ line1: "123 Main Street",+ locality: "Los Angeles",+ region: "CA",+ postal_code: "90001"+ )++ assert response.success?+ data = response.data+ assert_equal 420_000, data.valuation+ assert_equal "single_family_home", data.property_type+ assert_equal 1985, data.year_built+ assert_equal 1500, data.area_value+ assert_equal "sqft", data.area_unit+ assert_requested stub+ end++ test "falls back to the nested assessed market value when the nested model value is absent" do+ stub_request(:get, "https://app.realie.ai/api/public/property/address/")+ .with(query: hash_including("address" => "123 Main Street"))+ .to_return(status: 200, body: nested_address_lookup_body("realieValuation" => { "ml" => {} }))++ response = @provider.fetch_property_valuation(line1: "123 Main Street", region: "CA")++ assert response.success?+ assert_equal 400_000, response.data.valuation+ end++ test "matches the entered city against the nested property location" do+ stub_request(:get, "https://app.realie.ai/api/public/property/address/")+ .with(query: hash_including("address" => "123 Main Street"))+ .to_return(status: 200, body: nested_address_lookup_body)++ response = @provider.fetch_property_valuation(+ line1: "123 Main Street",+ locality: "San Diego",+ region: "CA",+ postal_code: "90001"+ )++ assert_not response.success?+ assert_equal I18n.t("providers.realie.errors.location_mismatch"), response.error.message+ end++ test "picks the primary building when the nested record lists several" do+ body = nested_address_lookup_body(+ "buildingInformation" => {+ "buildings" => [+ { "buildingNumber" => 2, "actualYearBuilt" => 1999, "buildingArea" => 400.0 },+ { "buildingNumber" => 1, "actualYearBuilt" => 1985, "buildingArea" => 1500.0 }+ ]+ }+ )++ stub_request(:get, "https://app.realie.ai/api/public/property/address/")+ .with(query: hash_including("address" => "123 Main Street"))+ .to_return(status: 200, body: body)++ response = @provider.fetch_property_valuation(line1: "123 Main Street", region: "CA")++ assert response.success?+ assert_equal 1985, response.data.year_built+ assert_equal 1500, response.data.area_value+ end++ test "raises no_valuation when neither shape carries a usable value" do+ stub_request(:get, "https://app.realie.ai/api/public/property/address/")+ .with(query: hash_including("address" => "123 Main Street"))+ .to_return(status: 200, body: nested_address_lookup_body(+ "realieValuation" => { "ml" => { "value" => 0 } },+ "valuationInformation" => { "totalMarketValue" => nil }+ ))++ response = @provider.fetch_property_valuation(line1: "123 Main Street", region: "CA")++ assert_not response.success?+ assert_equal I18n.t("providers.realie.errors.no_valuation"), response.error.message+ end+test "stops issuing requests once the monthly limit is reached" doProviderRequestCount.create!(provider_key: "realie", period: ProviderRequestCount.current_period, count: Provider::Realie::MAX_REQUESTS_PER_MONTH)