quixoten-craigler 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION.yml +1 -1
- data/craigler.gemspec +1 -1
- data/lib/craigler.rb +2 -1
- data/lib/craigler/search.rb +27 -21
- data/test/craigler_search_test.rb +6 -6
- metadata +1 -1
data/VERSION.yml
CHANGED
data/craigler.gemspec
CHANGED
data/lib/craigler.rb
CHANGED
@@ -13,7 +13,8 @@ module Craigler
|
|
13
13
|
class << self
|
14
14
|
# Interface to Search that may or may not be more readable
|
15
15
|
def search(category, options = {})
|
16
|
-
|
16
|
+
options = { :only => category }.merge(options)
|
17
|
+
results = Search.new(options.delete(:for), options).results()
|
17
18
|
results.each {|result| yield(result) } if block_given?
|
18
19
|
results
|
19
20
|
end
|
data/lib/craigler/search.rb
CHANGED
@@ -4,7 +4,7 @@ module Craigler
|
|
4
4
|
class Search
|
5
5
|
include ERB::Util
|
6
6
|
|
7
|
-
attr_reader :search_term, :categories, :locations
|
7
|
+
attr_reader :search_term, :categories, :locations, :page_limit
|
8
8
|
|
9
9
|
# Creates a wrapper object for a craigslist search
|
10
10
|
#
|
@@ -13,32 +13,39 @@ module Craigler
|
|
13
13
|
# Specifies the location(s) to search in. Defaults to <tt>:anywhere</tt>.
|
14
14
|
# [:only]
|
15
15
|
# Specifies the category or categories to search in. Defaults to <tt>:all_for_sale_or_wanted</tt>
|
16
|
+
# [:page_limit]
|
17
|
+
# Maximum number of pages to fetch results from. Defaults to <tt>4</tt>.
|
18
|
+
# <b>Note:</b> A location may, and often does, have more than one searchable
|
19
|
+
# url assciated with it, e.g., {California}[http://geo.craigslist.org/iso/us/ca]. Because
|
20
|
+
# <tt>:page_limit</tt> is applied seperately to each url within the location, searching <tt>:in => :california</tt>
|
21
|
+
# with a <tt>:page_limit => 4</tt> could potentially make up to 100 page requests.</em>
|
16
22
|
def initialize(search_term, options = {})
|
17
23
|
raise InvalidSearchTerm if search_term.nil? || search_term == ''
|
18
24
|
|
19
|
-
|
20
|
-
|
21
|
-
|
25
|
+
options = {:in => :anywhere, :only => :all_for_sale_or_wanted, :page_limit => 4}.merge(options)
|
26
|
+
options[:in] = LOCATIONS.keys if options[:in] == :anywhere
|
27
|
+
@locations = (options[:in].is_a?(Array) ? options[:in] : [options[:in]]).collect(&:to_sym)
|
28
|
+
@categories = (options[:only].is_a?(Array) ? options[:only] : [options[:only]]).collect(&:to_sym)
|
29
|
+
@page_limit = options[:page_limit]
|
30
|
+
@search_term = search_term
|
31
|
+
@results = nil
|
32
|
+
|
33
|
+
_validate_locations()
|
34
|
+
_validate_categories()
|
22
35
|
end
|
23
36
|
|
24
37
|
# Returns the results of the search. If this is the first time
|
25
38
|
# calling #results then they will be fetched over the internet and cached in the search object.
|
26
39
|
#
|
27
40
|
# === Options
|
28
|
-
# [:page_limit]
|
29
|
-
# Maximum number of pages to fetch results from. Defaults to <tt>4</tt>.
|
30
|
-
# <b>Note:</b> A location may, and often does, have more than one searchable
|
31
|
-
# url assciated with it, e.g., {California}[http://geo.craigslist.org/iso/us/ca]. Because
|
32
|
-
# <tt>:page_limit</tt> is applied seperately to each url within the location, searching <tt>:in => :california</tt>
|
33
|
-
# with a <tt>:page_limit => 4</tt> could potentially make up to 100 page requests.</em>
|
34
41
|
# [:refresh]
|
35
42
|
# Set to <tt>true</tt> to force an update across the internet.
|
36
43
|
def results(options = {})
|
37
|
-
options = { :
|
44
|
+
options = { :refresh => false }.merge(options)
|
38
45
|
return @results unless @results.nil? || options[:refresh]
|
39
46
|
|
40
47
|
@results = []
|
41
|
-
last_page =
|
48
|
+
last_page = @page_limit - 1 # pages start at 0
|
42
49
|
|
43
50
|
_for_each_locations_search_url() do |location, url|
|
44
51
|
(0..last_page).each do |page|
|
@@ -51,18 +58,17 @@ module Craigler
|
|
51
58
|
results
|
52
59
|
end
|
53
60
|
|
54
|
-
|
55
|
-
def
|
56
|
-
options = {:in => LOCATIONS.keys, :only => :all_for_sale_or_wanted}.merge(options)
|
57
|
-
@locations = options[:in].is_a?(Array) ? options[:in] : [options[:in]]
|
58
|
-
@categories = options[:only].is_a?(Array) ? options[:only] : [options[:only]]
|
59
|
-
|
61
|
+
protected
|
62
|
+
def _validate_locations
|
60
63
|
@locations.each() do |location|
|
61
|
-
raise InvalidLocation
|
64
|
+
raise InvalidLocation.new(":anywhere not expected as part of an array") if location == :anywhere
|
65
|
+
raise InvalidLocation.new(":#{location} is not a valid location") unless LOCATIONS.key?(location)
|
62
66
|
end
|
63
|
-
|
67
|
+
end
|
68
|
+
|
69
|
+
def _validate_categories
|
64
70
|
@categories.each() do |category|
|
65
|
-
raise InvalidCategory unless
|
71
|
+
raise InvalidCategory unless CATEGORIES.key?(category)
|
66
72
|
end
|
67
73
|
end
|
68
74
|
|
@@ -47,6 +47,12 @@ class CraiglerSearchTest < Test::Unit::TestCase
|
|
47
47
|
Craigler::Search.new('Suzuki Boulevard M50', :in => [:utah, :nevada])
|
48
48
|
end
|
49
49
|
end
|
50
|
+
|
51
|
+
should "allow us to limit the number of pages searched" do
|
52
|
+
one_page_count = Craigler::Search.new('Honda', :in => :utah, :page_limit => 1).results.size
|
53
|
+
two_page_count = Craigler::Search.new('Honda', :in => :utah, :page_limit => 2).results.size
|
54
|
+
assert(one_page_count < two_page_count, "#{one_page_count} is not less than #{two_page_count}")
|
55
|
+
end
|
50
56
|
end
|
51
57
|
|
52
58
|
context "fetching search results" do
|
@@ -60,11 +66,5 @@ class CraiglerSearchTest < Test::Unit::TestCase
|
|
60
66
|
assert(results.size > 0, "No results were returned")
|
61
67
|
assert(results.inject(true) {|t,r| t && r.is_a?(Hash)})
|
62
68
|
end
|
63
|
-
|
64
|
-
should "allow us to limit the number of pages searched" do
|
65
|
-
one_page_count = @search.results(:page_limit => 1).size
|
66
|
-
two_page_count = @search.results(:page_limit => 2, :refresh => true).size
|
67
|
-
assert(one_page_count < two_page_count, "#{one_page_count} is not less than #{two_page_count}")
|
68
|
-
end
|
69
69
|
end
|
70
70
|
end
|