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
Creates a wrapper object for a craigslist search
Options
- :in
-
Specifies the location(s) to search in. Defaults to
:anywhere
. - :only
-
Specifies the category or categories to search in. Defaults to
:all_for_sale_or_wanted
- :page_limit
-
Maximum number of pages to fetch results from. Defaults to
4
. Note: A location may, and often does, have more than one searchable url assciated with it, e.g., California. Because:page_limit
is applied seperately to each url within the location, searching:in => :california
with a:page_limit => 4
could potentially make up to 100 page requests.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/craigler/search.rb', line 22 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.
Options
- :refresh
-
Set to
true
to force an update across the internet.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/craigler/search.rb', line 43 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 |