#!/usr/bin/env ruby # This script demonstrates how to use the RESO Web API with the Ruby client by pulling listings # and replacing encoded field values with their corresponding human-readable values, which are # pulled from the XML returned by the RESO metadata endpoint. require "spark_api" require "nokogiri" # set up session and RESO Web API middleware SparkApi.configure do |config| config.authentication_mode = SparkApi::Authentication::OAuth2 config.middleware = :reso_api end SparkApi.client.session = SparkApi::Authentication::OAuthSession.new({ :access_token => "OAUTH2_ACCESS_TOKEN" }) # pull metadata from RESO Web API metadata_res = (SparkApi.client.get("/$metadata", {:$ApiUser => "FLEXMLS_TECH_ID"}) ) metadata_xml = Nokogiri::XML(metadata_res).remove_namespaces! # make an array of fields which need to be checked for readable values fields_to_lookup = [] metadata_xml.xpath('//Schema/EnumType/@Name').each do |el| fields_to_lookup << el.to_str end # get 25 listings listings = (SparkApi.client.get("/Property", {:$top => 25, :$ApiUser => "FLEXMLS_TECH_ID"} )) listings['value'].each do |listing| # for each listing, fields_to_lookup.each do |field| # go through the array of fields to be checked. if !!listing[field] # when one of the fields that needs to be checked exists in a listing, if listing[field].is_a? String readable = metadata_xml.xpath( # check for readable value to be swapped in "//Schema/ EnumType[@Name=\"#{field}\"]/ Member[@Name=\"#{listing[field]}\"]/ Annotation" ).attr("String") # if there is a readable value, swap it in if !!readable listing[field] = readable.to_str end elsif listing[field].is_a? Array readable_arr = [] listing[field].each do |el| readable = metadata_xml.xpath( # check for readable value to be swapped in "//Schema/ EnumType[@Name=\"#{field}\"]/ Member[@Name=\"#{el}\"]/ Annotation" ).attr("String") # assemble a new array with readable values and swap it in if !!readable readable_arr << readable.to_str else readable_arr << el end listing[field] = readable_arr end end end end end puts listings