Class: Scraper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_object) ⇒ Scraper

Returns a new instance of Scraper.



5
6
7
8
# File 'lib/scraper.rb', line 5

def initialize(query_object)
  self.query = query_object
  self.product_rows = []
end

Instance Attribute Details

#encoded_urlObject

Returns the value of attribute encoded_url.



3
4
5
# File 'lib/scraper.rb', line 3

def encoded_url
  @encoded_url
end

#product_rowsObject

Returns the value of attribute product_rows.



3
4
5
# File 'lib/scraper.rb', line 3

def product_rows
  @product_rows
end

#queryObject

Returns the value of attribute query.



3
4
5
# File 'lib/scraper.rb', line 3

def query
  @query
end

Instance Method Details

#htmlify(string) ⇒ Object



10
11
12
# File 'lib/scraper.rb', line 10

def htmlify(string)
  string.gsub(" ", "+")
end

#number_extractionObject



23
24
25
26
27
# File 'lib/scraper.rb', line 23

def number_extraction
    self.product_rows.map do |product_row|
      product_row.css('span.l2 span.price').text[1..-1].to_i
  end
end

#open_urlObject

in development, not implemented



29
30
31
# File 'lib/scraper.rb', line 29

def open_url # in development, not implemented
  `open #{self.encoded_url}`
end

#scrapeObject



14
15
16
17
18
19
20
21
# File 'lib/scraper.rb', line 14

def scrape
  self.encoded_url = URI.encode("https://newyork.craigslist.org/search/sss#{query.keyword_to_url}?zoomToPosting=&catAbb=sss&query=#{htmlify(query.search_query)}&minAsk=#{query.min_price}&maxAsk=#{query.max_price}&sort=rel&excats=")
  html = open(URI.parse(self.encoded_url)) # /sss?zo
  cl_page = Nokogiri::HTML(html.read)
  cl_page.css('div.content p.row').map do |product_row|
    self.product_rows << product_row
  end
end