Sha256: 2991e04f7eca05bfbf1bdc90581fa9684e1e16e3e16068223d9aa9482921cb0d

Contents?: true

Size: 1.81 KB

Versions: 9

Compression:

Stored size: 1.81 KB

Contents

require 'real_savvy/connection'
require 'real_savvy/concern'
require 'real_savvy/adapter'

class RealSavvy::Client
  ADAPTER_LOOKUP = {
    agent_profiles: RealSavvy::Adapter::AgentProfile,
    agents: RealSavvy::Adapter::Agent,
    broker_offices: RealSavvy::Adapter::BrokerOffice,
    collections: RealSavvy::Adapter::Collection,
    markets: RealSavvy::Adapter::Market,
    open_houses: RealSavvy::Adapter::OpenHouse,
    properties: RealSavvy::Adapter::Property,
    listings: RealSavvy::Adapter::Property,
    saved_searches: RealSavvy::Adapter::SavedSearch,
    site: RealSavvy::Adapter::Site,
    sites: RealSavvy::Adapter::Site,
    users: RealSavvy::Adapter::User,
    user: RealSavvy::Adapter::User,
  }.freeze

  def initialize(token:, api_url: 'https://api.realsavvy.com', logger: nil, impersonated_user_id: nil)
    @token = token
    @api_url = api_url
    @logger = logger
    @impersonated_user_id = impersonated_user_id
  end

  def connection
    @connection ||= RealSavvy::Connection.new(client: self)
  end

  def logger
    @logger
  end

  def logging?
    !!@logging
  end

  def token
    @token
  end

  def api_url
    @api_url
  end

  def impersonated_user_id
    @impersonated_user_id
  end

  class AdapterProxy
    attr_reader :adapter

    def initialize(adapter)
      @adapter = adapter
    end

    private

    def method_missing(method, *args, &block)
      response = @adapter.send(method, *args, &block)
      RealSavvy::Document.new(response.body, status: response.status)
    end
  end

  def get_adapter(name)
    resource_adapaters[name] ||= AdapterProxy.new(ADAPTER_LOOKUP[name].new(connection: connection))
  end

  private

  def resource_adapaters
    @resource_adapaters ||= {}
  end

  def method_missing(m, *args, &block)
    if ADAPTER_LOOKUP[m]
      get_adapter(m)
    else
      super
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
real_savvy-0.0.12 lib/real_savvy/client.rb
real_savvy-0.0.10 lib/real_savvy/client.rb
real_savvy-0.0.9 lib/real_savvy/client.rb
real_savvy-0.0.8 lib/real_savvy/client.rb
real_savvy-0.0.7 lib/real_savvy/client.rb
real_savvy-0.0.6 lib/real_savvy/client.rb
real_savvy-0.0.5 lib/real_savvy/client.rb
real_savvy-0.0.4 lib/real_savvy/client.rb
real_savvy-0.0.3 lib/real_savvy/client.rb