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

Note:

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

Parameters:

  • search_term (String)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :in (Symbol, Array) — default: :anywhere

    location(s) to search

  • :only (Symbol, Array) — default: :all_for_sale_or_wanted

    category or categories to search

  • :page_limit (Fixnum) — default: 4

    maximum number of pages to read per url

Raises:



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, 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.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :refresh (Boolean) — default: false

    force the results to be updated before they are returned



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(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