Class: Craigslister

Inherits:
Object
  • Object
show all
Defined in:
lib/craigslister.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#areaObject (readonly)

Returns the value of attribute area.



8
9
10
# File 'lib/craigslister.rb', line 8

def area
  @area
end

#highObject (readonly)

Returns the value of attribute high.



8
9
10
# File 'lib/craigslister.rb', line 8

def high
  @high
end

#itemObject (readonly)

Returns the value of attribute item.



8
9
10
# File 'lib/craigslister.rb', line 8

def item
  @item
end

#lowObject (readonly)

Returns the value of attribute low.



8
9
10
# File 'lib/craigslister.rb', line 8

def low
  @low
end

#resultsObject (readonly)

Returns the value of attribute results.



8
9
10
# File 'lib/craigslister.rb', line 8

def results
  @results
end

Instance Method Details



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

#urlObject



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