Class: Craigler::Search
- Inherits:
-
Object
- Object
- Craigler::Search
- Includes:
- ERB::Util
- Defined in:
- lib/craigler/search.rb
Instance Attribute Summary collapse
-
#categories ⇒ Object
readonly
Returns the value of attribute categories.
-
#locations ⇒ Object
readonly
Returns the value of attribute locations.
-
#page_limit ⇒ Object
readonly
Returns the value of attribute page_limit.
-
#search_term ⇒ Object
readonly
Returns the value of attribute search_term.
Instance Method Summary collapse
-
#initialize(search_term, options = {}) ⇒ Search
constructor
Creates a wrapper object for a craigslist search.
-
#results(options = {}) ⇒ Object
Returns the results of the search.
Constructor Details
#initialize(search_term, options = {}) ⇒ Search
A location often has more than one url associated with it. For example, :california
has 29 associated urls as of this writing. In this case, up to 116 pages could potentially be searched (:page_limit * num_urls = 4 * 29 = 116
).
Creates a wrapper object for a craigslist search
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/craigler/search.rb', line 16 def initialize(search_term, = {}) raise InvalidSearchTerm if search_term.nil? || search_term == '' = {:in => :anywhere, :only => :all_for_sale_or_wanted, :page_limit => 4}.merge() [:in] = LOCATIONS.keys if [:in] == :anywhere @locations = ([:in].is_a?(Array) ? [:in] : [[:in]]).collect(&:to_sym) @categories = ([:only].is_a?(Array) ? [:only] : [[:only]]).collect(&:to_sym) @page_limit = [:page_limit] @search_term = search_term @results = nil _validate_locations() _validate_categories() end |
Instance Attribute Details
#categories ⇒ Object (readonly)
Returns the value of attribute categories.
7 8 9 |
# File 'lib/craigler/search.rb', line 7 def categories @categories end |
#locations ⇒ Object (readonly)
Returns the value of attribute locations.
7 8 9 |
# File 'lib/craigler/search.rb', line 7 def locations @locations end |
#page_limit ⇒ Object (readonly)
Returns the value of attribute page_limit.
7 8 9 |
# File 'lib/craigler/search.rb', line 7 def page_limit @page_limit end |
#search_term ⇒ Object (readonly)
Returns the value of attribute search_term.
7 8 9 |
# File 'lib/craigler/search.rb', line 7 def search_term @search_term end |
Instance Method Details
#results(options = {}) ⇒ Object
Returns the results of the search. If this is the first time calling #results then they will be fetched over the internet and cached in the search object.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/craigler/search.rb', line 35 def results( = {}) = { :refresh => false }.merge() return @results unless @results.nil? || [:refresh] @results = [] last_page = @page_limit - 1 # pages start at 0 _for_each_locations_search_url() do |location, url| (0..last_page).each do |page| results = _extract_items_from_url(location, "#{url}&s=#{page*25}") @results.push(*results) break if results.size < RESULTS_PER_PAGE end end results end |