Giter Site home page Giter Site logo

reso_transport's Introduction

Gitpod ready-to-code

ResoTransport

A Ruby gem for connecting to and interacting with RESO WebAPI services. Learn more about what that is by checking out the RESO WebAPI Documentation.

Installation

Add this line to your application's Gemfile:

gem 'reso_transport'

And then execute:

$ bundle

Or install it yourself as:

$ gem install reso_transport

Usage

Compatability

This gem has been tested to work with:

Logging

You can either set a global logger in an initializer file:

ResoTransport.configure do |c|
  c.logger = Logger.new("some_log_file")
  # OR
  c.logger = Rails.logger
end

Or you can set a logger for each specific instance of a client which can be useful for debugging:

@client = ResoTransport::Client.new(config.merge(logger: Logger.new("logfile")))

Getting Connected

There are 2 strategies for authentication.

Bearer Token

It's simple to use a static access token if your token never expires:

  @client = ResoTransport::Client.new({
    md_file: METADATA_CACHE,
    endpoint: ENDPOINT_URL
    authentication: {
      access_token: TOKEN,
      token_type: "Bearer" # this is the default and can be ommitted
    }
  })

Authorization Endpoint

If the connection requires requesting a new token periodically, it's easy to provide that information:

  @client = ResoTransport::Client.new({
    md_file: METADATA_CACHE,
    endpoint: ENDPOINT_URL
    authentication: {
      endpoint: AUTH_ENDPOINT,
      client_id: CLIENT_ID,
      client_secret: CLIENT_SECRET,
      grant_type: "client_credentials", # these are the default and can be ommitted
      scope: "api"
    }
  })

This will pre-fetch a token from the provided endpoint when the current token is either non-existent or has expired.

Caching Metadata

The metadata file itself is large and parsing it is slow, so ResoTransport has built in support for caching the metadata to your file system. In the example above you would replace METADATA_CACHE with a path to a file to store the metadata.

  md_file: "reso_md_cache/#{@mls.name}",

This will store the metadata to a file with @mls.name in a folder named reso_md_cache in the relative root of your app.

Customize your cache

If you don't have access to the file system, like on Heroku, or you just don't want to store the metadata on the file system, you can provide your down metadata cache class.

class MyCacheStore < ResoTransport::MetadataCache
  
  def read
    # read `name` from somewhere    
  end

  def write(data)
    # write `name` with `data` somewhere
    # return an IO instance
  end

end

The metadata parser expects to recieve an IO instance so just make sure your read and write methods return one.

And you can instruct the client to use that cache store like so:

  md_file: "reso_md_cache/#{@mls.name}",
  md_cache: MyCacheStore

Skip cache altogether

Caching the metadata is not actually required, just be aware that it will be much slower. To skip caching just omit the related keys when instantiating a new Client.

  @client = ResoTransport::Client.new({
    endpoint: ENDPOINT_URL
    authentication: {
      endpoint: AUTH_ENDPOINT,
      client_id: CLIENT_ID,
      client_secret: CLIENT_SECRET,
    }
  })

Resources

Once you have a successful connection you can explore what resources are available from the API:

  @client.resources
  #=> {"Property"=>#<ResoTransport::Resource entity_set="Property", schema="ODataService">, "Office"=>#<ResoTransport::Resource entity_set="Office", schema="ODataService">, "Member"=>#<ResoTransport::Resource entity_set="Member", schema="ODataService">}

  @client.resources["Property"]
  #=> #<ResoTransport::Resource entity_set="Property", schema="ODataService"> 

  @client.resources["Property"].query.limit(1).results
  #=> Results Array

Querying

ResoTransport provides powerful querying capabilities:

To get 10 listings in Los Angeles between 900K and 1M and at least 5 bedrooms:

  @resource.query.
    eq(City: "Los Angeles").
    le(ListPrice: 1_000_000).
    ge(ListPrice: 900_000, Bedrooms: 5).
    limit(10).
    results

To get 10 listings in Los Angeles OR Hollywood between 900K and 1M and at least 5 bedrooms:

  @resource.query.
  any {
    eq(City: "Los Angeles").eq(City: "Hollywood")
  }.
  le(ListPrice: 1_000_000).
  ge(ListPrice: 900_000, Bedrooms: 5).
  limit(10).
  results

Expanding Child Records

To see what child records can be expanded look at expandable:

  @resource.expandable
  #=> [#<struct ResoTransport::Property name="Media", data_type="Collection(RESO.Media)", attrs={"Name"=>"Media", "Type"=>"Collection(RESO.Media)"}, multi=true, enum=nil, complex_type=nil, entity_type=#<struct ResoTransport::EntityType name="Media", base_type=nil, primary_key="MediaKey", schema="CoreLogic.DataStandard.RESO.DD">> ...] 

Use expand to expand child records with the top level results.

  @resource.query.expand("Media").limit(10).results
  #=> Results Array

You have several options to expand multiple child record sets. Each of these will have the same result.

  @resource.query.expand("Media", "Office").limit(10).results
  
  @resource.query.expand(["Media", "Office"]).limit(10).results

  @resource.query.expand("Media").expand("Office").limit(10).results

Results Array

The results are parsed according to the metadata with some things worth mentioning:

  • Date fields are parsed into ruby DateTime objects
  • Enumeration fields are parsed into either the Name or Annotation -> String of the member that is represented.
  • Collections or Enumerations with is_flags=true will also be parsed into an Array.

Enumerations

Enumerations are essentially a mapping of system values and display values. To see a mapping:

  @resource.property("StandardStatus").enum.mapping

  => {
       "Active"=>"Active",
       "ActiveUnderContract"=>"Active Under Contract",
       "Canceled"=>"Canceled",
       "Closed"=>"Closed",
       "ComingSoon"=>"Coming Soon",
       "Delete"=>"Delete",
       "Expired"=>"Expired",
       "Hold"=>"Hold",
       "Incomplete"=>"Incomplete",
       "Pending"=>"Pending",
       "Withdrawn"=>"Withdrawn"
     }

Most Enumerations will ultimately be used to fill a dropdown with options to select from. Like so:

  @resource.property("StandardStatus").enum.mapping.values
  #=> ["Active", "Active Under Contract", "Canceled", "Closed", "Coming Soon", "Delete", "Expired", "Hold", "Incomplete", "Pending", "Withdrawn"]

When querying for an enumeration value, you can provide either the system name, or the display name and it will be converted to the correct value. This allows your programs to not worry too much about the system values.

  @resource.query.eq(StandardStatus: "Active Under Contract").limit(1).compile_params
  #=> {"$top"=>1, "$filter"=>"StandardStatus eq 'ActiveUnderContract'"} 

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/reso_transport. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the ResoTransport project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

reso_transport's People

Contributors

jondruse avatar coderberry avatar mixflame avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.