Class: Scraper
- Inherits:
-
Object
- Object
- Scraper
- Defined in:
- lib/scraper.rb
Instance Attribute Summary collapse
-
#encoded_url ⇒ Object
Returns the value of attribute encoded_url.
-
#product_rows ⇒ Object
Returns the value of attribute product_rows.
-
#query ⇒ Object
Returns the value of attribute query.
Instance Method Summary collapse
- #htmlify(string) ⇒ Object
-
#initialize(query_object) ⇒ Scraper
constructor
A new instance of Scraper.
- #number_extraction ⇒ Object
-
#open_url ⇒ Object
in development, not implemented.
- #scrape ⇒ Object
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_url ⇒ Object
Returns the value of attribute encoded_url.
3 4 5 |
# File 'lib/scraper.rb', line 3 def encoded_url @encoded_url end |
#product_rows ⇒ Object
Returns the value of attribute product_rows.
3 4 5 |
# File 'lib/scraper.rb', line 3 def product_rows @product_rows end |
#query ⇒ Object
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_extraction ⇒ Object
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_url ⇒ Object
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 |
#scrape ⇒ Object
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 |