module YouGotListed class Listings < Resource def search(params = {}) params[:page_count] ||= 20 params[:page_index] ||= 1 params[:sort_name] ||= "rent" params[:sort_dir] ||= "asc" params[:detail_level] ||= 2 SearchResponse.new(self.client.perform_request(:get, '/rentals/search.php', params), self.client, params[:page_count]) end def featured(params = {}, featured_tag = 'Featured Rentals') if params[:tags].blank? params[:tags] = featured_tag else params[:tags] += (params[:tags].ends_with?(',') ? featured_tag : ",#{featured_tag}") end search(params) end def find_by_id(listing_id) params = {:listing_id => listing_id, :detail_level => 2} response = SearchResponse.new(self.client.perform_request(:get, '/rentals/search.php', params), self.client, 20) (response.success? && response.properties.size > 0) ? response.properties.first : nil end def find_all(params = {}) params[:page_count] ||= 20 all_listings = [] response = search(params) if response.success? all_listings << response.properties total_pages = (response.ygl_response.total.to_i/params[:page_count].to_f).ceil if total_pages > 1 (2..total_pages).each do |page_num| resp = search(params.merge(:page => page_num)) if resp.success? all_listings << resp.properties end end end end all_listings.flatten end def find_all_by_ids(listing_ids, include_off_market = true) listing_ids = listing_ids.split(',') if listing_ids.is_a?(String) mls_ids = [] ygl_ids = [] all_listings = [] listing_ids.each do |list_id| if list_id =~ /[A-Z]{3}-[0-9]{3}-[0-9]{3}/ ygl_ids << list_id else mls_ids << list_id end end ygl_ids.in_groups_of(500, false).each_with_index do |group, index| search_params = {:listing_ids => group.join(','), :page_count => 500, :page_index => (index + 1)} search_params[:include_off_market] = 1 if include_off_market group.delete_if{|x| x.nil?} all_listings << find_all({:listing_ids => group.join(','), :page_count => 500, :page_index => (index + 1)}) end mls_ids.each do |mls_id| all_listings << find_by_id(mls_id) end all_listings.compact.flatten end class SearchResponse < YouGotListed::Response attr_accessor :limit, :paginator_cache, :client def initialize(response, client, limit = 20, raise_error = false) super(response, raise_error) self.limit = limit self.client = client end def properties return [] if self.ygl_response.listings.blank? props = [] if self.ygl_response.listings.listing.is_a?(Array) self.ygl_response.listings.listing.each do |listing| props << YouGotListed::Listing.new(listing, self.client) end else props << YouGotListed::Listing.new(self.ygl_response.listings.listing, self.client) end props end def mls_results? @has_mls_properties ||= properties.any?{|property| property.mls_listing?} end def paginator paginator_cache if paginator_cache self.paginator_cache = WillPaginate::Collection.create( (self.ygl_response.page_index ? self.ygl_response.page_index : 1), self.limit, (self.ygl_response.total ? self.ygl_response.total : properties.size)) do |pager| pager.replace properties end end end end end