Class: Craigler::Search

Inherits:
Object
  • Object
show all
Includes:
ERB::Util
Defined in:
lib/craigler/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Raises:



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, options = {})
  raise InvalidSearchTerm if search_term.nil? || search_term == ''
  
  options       = {:in => :anywhere, :only => :all_for_sale_or_wanted, :page_limit => 4}.merge(options)
  options[:in]  = LOCATIONS.keys if options[:in] == :anywhere
  @locations    = (options[:in].is_a?(Array) ? options[:in] : [options[:in]]).collect(&:to_sym)
  @categories   = (options[:only].is_a?(Array) ? options[:only] : [options[:only]]).collect(&:to_sym)
  @page_limit   = options[:page_limit]
  @search_term  = search_term
  @results      = nil
  
  _validate_locations()
  _validate_categories()
end

Instance Attribute Details

#categoriesObject (readonly)

Returns the value of attribute categories.



7
8
9
# File 'lib/craigler/search.rb', line 7

def categories
  @categories
end

#locationsObject (readonly)

Returns the value of attribute locations.



7
8
9
# File 'lib/craigler/search.rb', line 7

def locations
  @locations
end

#page_limitObject (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_termObject (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(options = {})
  options = { :refresh => false }.merge(options)
  return @results unless @results.nil? || options[: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