quixoten-craigler 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 1
3
- :minor: 1
3
+ :minor: 2
4
4
  :patch: 0
data/craigler.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{craigler}
8
- s.version = "1.1.0"
8
+ s.version = "1.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Devin Christensen"]
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
- results = Search.new(options[:for], :in => (options[:in] || :anywhere), :only => category).results()
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
@@ -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
- @search_term = search_term
20
- @results = nil
21
- _parse_options(options)
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 = { :page_limit => 4, :refresh => false }.merge(options)
44
+ options = { :refresh => false }.merge(options)
38
45
  return @results unless @results.nil? || options[:refresh]
39
46
 
40
47
  @results = []
41
- last_page = options[:page_limit] - 1 # pages start at 0
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
- private
55
- def _parse_options(options)
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 unless location == :anywhere || LOCATIONS.key?(location)
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 category == :all_for_sale_or_wanted || CATEGORIES.key?(category)
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quixoten-craigler
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Devin Christensen