class SparkApi::Models::Listing

Constants

DATA_MASK
WRITEABLE_FIELDS

Attributes

constraints[RW]
documents[RW]
floplans[RW]
open_houses[RW]
photos[RW]
rental_calendars[RW]
tour_of_homes[RW]
videos[RW]
virtual_tours[RW]

Public Class Methods

company(arguments={}) click to toggle source
# File lib/spark_api/models/listing.rb, line 90
def self.company(arguments={})
  collect(connection.get("/company/listings", arguments))
end
find_by_cart_id(cart_id, options={}) click to toggle source
# File lib/spark_api/models/listing.rb, line 77
def self.find_by_cart_id(cart_id, options={}) 
  query = {:_filter => "ListingCart Eq '#{cart_id}'"}
  find(:all, options.merge(query)) 
end
my(arguments={}) click to toggle source
# File lib/spark_api/models/listing.rb, line 82
def self.my(arguments={})
  collect(connection.get("/my/listings", arguments))
end
nearby(latitude, longitude, arguments={}) click to toggle source
# File lib/spark_api/models/listing.rb, line 94
def self.nearby(latitude, longitude, arguments={})
  nearby_args = {:_lat => latitude, :_lon => longitude}.merge(arguments)
  collect(connection.get("/listings/nearby", nearby_args))
end
new(attributes={}) click to toggle source
Calls superclass method SparkApi::Models::Base::new
# File lib/spark_api/models/listing.rb, line 11
def initialize(attributes={})
  @photos = []
  @videos = []
  @virtual_tours = []
  @rental_calendars = []
  @documents = []
  @constraints = []
  @tour_of_homes = []
  @open_houses = []
  @floplans = []

  if attributes.has_key?('StandardFields')
    pics, vids, tours, docs, ohouses, tourhomes = attributes['StandardFields'].values_at('Photos','Videos', 'VirtualTours', 'Documents', 'OpenHouses', 'TourOfHomes')
  end

  if attributes.has_key?('RentalCalendar')
    rentalcalendars = attributes['RentalCalendar']
  end

  if attributes.has_key?('FloPlans')
    floplans = attributes['FloPlans']
  end

  if pics != nil
    setup_attribute(@photos, pics, Photo)
    attributes['StandardFields'].delete('Photos')
  end

  if vids != nil
    setup_attribute(@videos, vids, Video)
    attributes['StandardFields'].delete('Videos')
  end

  if tours != nil
    setup_attribute(@virtual_tours, tours, VirtualTour)
    attributes['StandardFields'].delete('VirtualTours')
  end

  if docs != nil
    setup_attribute(@documents, docs, Document)
    attributes['StandardFields'].delete('Documents')
  end

  if ohouses != nil
    setup_attribute(@open_houses, ohouses, OpenHouse)
    attributes['StandardFields'].delete('OpenHouses')
  end

  if tourhomes != nil
    setup_attribute(@tour_of_homes, tourhomes, TourOfHome)
    attributes['StandardFields'].delete('TourOfHomes')
  end

  if rentalcalendars != nil
    setup_attribute(@rental_calendars, rentalcalendars, RentalCalendar)
    attributes.delete('RentalCalendar')
  end

  if floplans != nil
    setup_attribute(@floplans, floplans, FloPlan)
    attributes.delete('FloPlans')
  end

  super(attributes)
end
office(arguments={}) click to toggle source
# File lib/spark_api/models/listing.rb, line 86
def self.office(arguments={})
  collect(connection.get("/office/listings", arguments))
end
tour_of_homes(arguments={}) click to toggle source
# File lib/spark_api/models/listing.rb, line 99
def self.tour_of_homes(arguments={})
  collect(connection.get("/listings/tourofhomes", arguments))
end

Public Instance Methods

ExpirationDate() click to toggle source
# File lib/spark_api/models/listing.rb, line 233
def ExpirationDate
  attributes["ExpirationDate"]
end
ExpirationDate=(value) click to toggle source
# File lib/spark_api/models/listing.rb, line 236
def ExpirationDate=(value)
  write_attribute("ExpirationDate", value)
end
delete_photos(photoIds, args={}) click to toggle source
# File lib/spark_api/models/listing.rb, line 252
def delete_photos(photoIds, args={})
  unless photoIds.is_a? String
    raise ArgumentError, "Batch photo delete failed. '#{photoIds}' is not a string."
  end

  begin
    return delete_photos!(photoIds, args)
  rescue BadResourceRequest => e
    SparkApi.logger.warn { "Failed to delete photos from resource #{self}: #{e.message}" }
  rescue NotFound => e
    SparkApi.logger.error { "Failed to delete photos from resource #{self}: #{e.message}" }
  end
  false
end
delete_photos!(photoIds, args={}) click to toggle source
# File lib/spark_api/models/listing.rb, line 248
def delete_photos!(photoIds, args={})
  connection.delete subresource_path("photos") + "/#{photoIds}", args
end
editable?(editable_settings = []) click to toggle source
# File lib/spark_api/models/listing.rb, line 224
def editable?(editable_settings = [])
  settings = Array(editable_settings)
  editable = attributes.include?("Permissions") && self.Permissions["Editable"] == true
  if editable
    settings.each{ |setting| editable = false unless self.Permissions["EditableSettings"][setting.to_s] == true }
  end
  editable
end
full_address() click to toggle source
# File lib/spark_api/models/listing.rb, line 149
def full_address
  "#{self.street_address}, #{self.region_address}".strip().gsub(/^,\s/, '').gsub(/,$/, '')
end
my_notes() click to toggle source
# File lib/spark_api/models/listing.rb, line 119
def my_notes
  Note.build_subclass.tap do |note|
    note.prefix = "/listings/#{self.ListingKey}"
    note.element_name = "/my/notes"
  end
end
region_address() click to toggle source
# File lib/spark_api/models/listing.rb, line 145
def region_address
  "#{self.City}, #{self.StateOrProvince} #{self.PostalCode}".delete(DATA_MASK).strip().gsub(/^,\s/, '').gsub(/,$/, '')
end
reorder_photo(photo_id, index) click to toggle source
# File lib/spark_api/models/listing.rb, line 205
def reorder_photo(photo_id, index)
  unless Integer(index)
    raise ArgumentError, "Photo reorder failed. '#{index}' is not a number."
  end

  begin
    return reorder_photo!(photo_id, index)
  rescue BadResourceRequest => e
    SparkApi.logger.warn { "Failed to save resource #{self}: #{e.message}" }
  rescue NotFound => e
    SparkApi.logger.error { "Failed to save resource #{self}: #{e.message}" }
  end
  false
end
reorder_photo!(photo_id, index) click to toggle source
# File lib/spark_api/models/listing.rb, line 219
def reorder_photo!(photo_id, index)
  connection.put subresource_path("photos") + "/#{photo_id}", "Photos" => [{"Order"=>index}]
  true
end
reorder_photos(arguments={}) click to toggle source
# File lib/spark_api/models/listing.rb, line 190
def reorder_photos(arguments={})
  begin
    return reorder_photos!(arguments)
  rescue BadResourceRequest => e
    SparkApi.logger.warn { "Failed to save resource #{self}: #{e.message}" }
  rescue NotFound => e
    SparkApi.logger.error { "Failed to save resource #{self}: #{e.message}" }
  end
  false
end
reorder_photos!(arguments={}) click to toggle source
# File lib/spark_api/models/listing.rb, line 200
def reorder_photos!(arguments={})
  results = connection.put subresource_path("photos"), arguments
  true
end
respond_to?(method_symbol, include_all=false) click to toggle source
Calls superclass method SparkApi::Models::Base#respond_to?
# File lib/spark_api/models/listing.rb, line 240
def respond_to?(method_symbol, include_all=false)
  if super
    true
  else
    attributes['StandardFields'].include?(method_symbol.to_s) rescue false
  end
end
save(arguments={}) click to toggle source
# File lib/spark_api/models/listing.rb, line 153
def save(arguments={})
  self.errors = []
  begin
    return save!(arguments)
  rescue BadResourceRequest => e
    self.errors << {:code => e.code, :message => e.message}
    if e.code == 1053
      @constraints = []
      e.details.each do |detail|
        detail.each_pair do |k,v|
          v.each { |constraint| @constraints << Constraint.new(constraint)}
        end
      end
    end
    SparkApi.logger.warn { "Failed to save resource #{self}: #{e.message}" }
  rescue NotFound => e
    SparkApi.logger.error { "Failed to save resource #{self}: #{e.message}" }
  end
  false
end
save!(arguments={}) click to toggle source
# File lib/spark_api/models/listing.rb, line 174
def save!(arguments={})
  writable_changed_keys = changed & WRITEABLE_FIELDS
  if writable_changed_keys.empty?
    SparkApi.logger.warn { "No supported listing change detected" }
  else
    results = connection.put "/flexmls#{self.class.path}/#{self.Id}", build_hash(writable_changed_keys), arguments
    @contstraints = []
    results.details.each do |detail|
      detail.each_pair do |k,v|
        v.each { |constraint| @constraints << Constraint.new(constraint)}
      end
    end
  end
  true
end
shared_notes(fore=nil) click to toggle source

'fore' is required when accessing an agent's shared notes for a specific contact. If the ApiUser /is/ the contact, then it can be inferred by the api, so it's unecessary

# File lib/spark_api/models/listing.rb, line 130
def shared_notes(fore=nil)
  Note.build_subclass.tap do |note|
    note.prefix = "/listings/#{self.ListingKey}"
    if fore.nil?
      note.element_name = "/shared/notes"
    else
      note.element_name = "/shared/notes/contacts/#{fore}"
    end
  end
end
street_address() click to toggle source
# File lib/spark_api/models/listing.rb, line 141
def street_address        
  (self.UnparsedFirstLineAddress || '').delete(DATA_MASK).strip().gsub(/\s{2,}/, ' ')
end

Private Instance Methods

build_hash(keys) click to toggle source
# File lib/spark_api/models/listing.rb, line 288
def build_hash(keys)
  hash = {}
  keys.each do |key|
    hash[key] = attributes[key]
  end
  hash
end
method_missing(method_symbol, *arguments) click to toggle source

TODO trim this down so we're only overriding the StandardFields access

Calls superclass method SparkApi::Models::Base#method_missing
# File lib/spark_api/models/listing.rb, line 270
def method_missing(method_symbol, *arguments)
  method_name = method_symbol.to_s

  if method_name =~ /(=|\?)$/
    case $1
    when "="
      write_attribute($`,arguments.first)
      # TODO figure out a nice way to present setters for the standard fields
    when "?"
      attributes[$`]
    end  
  else 
    return attributes[method_name] if attributes.include?(method_name)
    return @attributes['StandardFields'][method_name] if attributes['StandardFields'].include?(method_name)
    super
  end
end
setup_attribute(attributes, collection, klass) click to toggle source

Determine if passed a model or hash and push instance of Klass onto attributes array

# File lib/spark_api/models/listing.rb, line 297
def setup_attribute(attributes, collection, klass)
  collection.collect do |c|
    attribute = (c.instance_of? klass) ? c : klass.new(c)
    attributes.push(attribute)
  end
end
subresource_path(subresource) click to toggle source
# File lib/spark_api/models/listing.rb, line 304
def subresource_path(subresource)
  "#{self.class.path}/#{self.Id}/#{subresource}"
end