Sha256: 6c83967054d5bc6fa6c34b3d708c7ad75570e6f12be08894b581ee5758ba9143

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

module Rentjuicer
  class Listing
    
    def initialize(listing)
      listing.keys.each do |key, value|
        self.instance_variable_set('@'+key, listing.send(key))
        self.class.send(:define_method, key, proc{self.instance_variable_get("@#{key}")})
      end
    end

    def similar_listings(rj, limit = 6)
      search_params = {
        :limit => limit + 1,
        :min_rent => self.rent.to_i * 0.9,
        :max_rent => self.rent.to_i * 1.1,
        :min_beds => ((self.bedrooms.to_i - 1) <= 0 ? 0 : (self.bedrooms.to_i - 1)),
        :max_beds => self.bedrooms.to_i + 1,
        :min_baths => ((self.bathrooms.to_i - 1) <= 0 ? 0 : (self.bathrooms.to_i - 1)),
        :max_baths => self.bathrooms.to_i + 1,
        :neighborhoods => self.neighborhood_name
      }

      similar = []
      listings = Rentjuicer::Listings.new(rj)
      listings.search(search_params).properties.each do |prop|
        similar << prop unless prop.id == self.id
        break if similar.size == limit
      end
      similar
    end

    def id
      rentjuice_id
    end

    def thumb_pic
      main_pic[:thumbnail] if sorted_photos
    end

    def first_pic
      main_pic[:fullsize] if sorted_photos
    end

    def main_pic
      sorted_photos.detect(lambda {return sorted_photos.first}) { |photo| photo[:main_photo] }
    end

    def sorted_photos
      @sorted_pictures ||= self.photos.sort_by{|photo| photo[:sort_order].to_i} if photos
    end

    def neighborhood_name
      self.neighborhoods.first unless neighborhoods.blank?
    end

    def mls_listing?
      source_type && source_type == "mls"
    end

    def mls_disclaimer
      attribution_split[1].gsub('<br />', '') if mls_listing? && attribution_split && attribution_split[1]
    end

    def courtesy_of
      attribution_split[0] if mls_listing? && attribution_split && attribution_split[0]
    end

    private

    def attribution_split
      @attribution_parts ||= attribution.split('<br />', 2) unless attribution.blank?
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rentjuicer-0.4.3 lib/rentjuicer/listing.rb