Class: Rentjuicer::Listings
- Inherits:
-
Object
- Object
- Rentjuicer::Listings
- Defined in:
- lib/rentjuicer/listings.rb
Defined Under Namespace
Classes: SearchResponse
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#resource ⇒ Object
Returns the value of attribute resource.
Instance Method Summary collapse
- #featured(params = {}) ⇒ Object
- #find_all(params = {}) ⇒ Object
- #find_all_by_ids(listing_ids, params = {}) ⇒ Object
- #find_by_id(listing_id, params = {}) ⇒ Object
-
#initialize(client) ⇒ Listings
constructor
A new instance of Listings.
- #search(params = {}) ⇒ Object
Constructor Details
#initialize(client) ⇒ Listings
Returns a new instance of Listings.
6 7 8 9 |
# File 'lib/rentjuicer/listings.rb', line 6 def initialize(client) self.client = client self.resource = "/listings.json" end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
4 5 6 |
# File 'lib/rentjuicer/listings.rb', line 4 def client @client end |
#resource ⇒ Object
Returns the value of attribute resource.
4 5 6 |
# File 'lib/rentjuicer/listings.rb', line 4 def resource @resource end |
Instance Method Details
#featured(params = {}) ⇒ Object
18 19 20 21 |
# File 'lib/rentjuicer/listings.rb', line 18 def featured(params = {}) params.merge!(:featured => 1) search(params) end |
#find_all(params = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rentjuicer/listings.rb', line 28 def find_all(params = {}) per_page = params[:limit] || 20 all_listings = [] response = search(params) if response.success? all_listings << response.properties total_pages = (response.total_results/per_page.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 |
#find_all_by_ids(listing_ids, params = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/rentjuicer/listings.rb', line 47 def find_all_by_ids(listing_ids, params = {}) listing_ids = listing_ids.split(',') if listing_ids.is_a?(String) all_listings = [] listing_ids.in_groups_of(500, false).each do |group| group.delete_if{|x| x.nil?} all_listings << find_all(params.merge(:rentjuice_id => group.join(','))) end all_listings.flatten end |
#find_by_id(listing_id, params = {}) ⇒ Object
23 24 25 26 |
# File 'lib/rentjuicer/listings.rb', line 23 def find_by_id(listing_id, params = {}) response = SearchResponse.new(self.client.process_get(resource, params.merge(:rentjuice_id => listing_id)), self.client) (response.success? && response.properties.size > 0) ? response.properties.first : nil end |
#search(params = {}) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/rentjuicer/listings.rb', line 11 def search(params = {}) limit = params[:limit] || 20 params[:order_by] ||= "rent" params[:order_direction] ||= "asc" SearchResponse.new(self.client.process_get(resource, params), self.client, limit) end |