Sha256: c706f6ec9df0ca5a9c6828d0fd34c624d7cfab355a3a6dfd0ec0a5d853e99e34

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 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

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rentjuicer-0.4.0 lib/rentjuicer/listing.rb
rentjuicer-0.3.0 lib/rentjuicer/listing.rb
rentjuicer-0.2.1 lib/rentjuicer/listing.rb