Class: Craigslister
- Inherits:
-
Object
- Object
- Craigslister
- Defined in:
- lib/craigslister.rb
Instance Attribute Summary collapse
-
#area ⇒ Object
readonly
Returns the value of attribute area.
-
#high ⇒ Object
readonly
Returns the value of attribute high.
-
#item ⇒ Object
readonly
Returns the value of attribute item.
-
#low ⇒ Object
readonly
Returns the value of attribute low.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Instance Method Summary collapse
-
#initialize(args) ⇒ Craigslister
constructor
A new instance of Craigslister.
- #links ⇒ Object
- #scrape! ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(args) ⇒ Craigslister
Returns a new instance of Craigslister.
10 11 12 13 14 15 16 17 |
# File 'lib/craigslister.rb', line 10 def initialize args @results = [] @area = args.fetch(:area, 'sfbay') @item = args[:item] @high = args.fetch(:high, nil) @low = args.fetch(:low, nil) validate_price_range end |
Instance Attribute Details
#area ⇒ Object (readonly)
Returns the value of attribute area.
8 9 10 |
# File 'lib/craigslister.rb', line 8 def area @area end |
#high ⇒ Object (readonly)
Returns the value of attribute high.
8 9 10 |
# File 'lib/craigslister.rb', line 8 def high @high end |
#item ⇒ Object (readonly)
Returns the value of attribute item.
8 9 10 |
# File 'lib/craigslister.rb', line 8 def item @item end |
#low ⇒ Object (readonly)
Returns the value of attribute low.
8 9 10 |
# File 'lib/craigslister.rb', line 8 def low @low end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
8 9 10 |
# File 'lib/craigslister.rb', line 8 def results @results end |
Instance Method Details
#links ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/craigslister.rb', line 31 def links page = Nokogiri::HTML(open(url)) page.css('.hdrlnk').map do |link| # formats out of town links, otherwise use base_url + link link['href'] =~ /\w+\.craig/ ? "https:" + link['href'] : base_url + link['href'] end end |
#scrape! ⇒ Object
19 20 21 22 |
# File 'lib/craigslister.rb', line 19 def scrape! links.each_with_index {|link, index| get_data_from(link, index)} results end |
#url ⇒ Object
24 25 26 27 28 29 |
# File 'lib/craigslister.rb', line 24 def url "#{base_url}/"\ "search/sss?sort=rel&"\ "#{price_query}"\ "query=#{item.downcase.split(' ') * '+'}" end |