Class: PinkoiScraper::Filter
- Inherits:
-
Object
- Object
- PinkoiScraper::Filter
- Defined in:
- lib/pinkoi/pinkoi_scraper.rb
Overview
filter class basically uses xpath selectors to get attribs
Instance Attribute Summary collapse
-
#item_selector ⇒ Object
writeonly
Sets the attribute item_selector.
-
#price_selector ⇒ Object
writeonly
Sets the attribute price_selector.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#site_url ⇒ Object
writeonly
Sets the attribute site_url.
-
#title_selector ⇒ Object
writeonly
Sets the attribute title_selector.
Instance Method Summary collapse
- #fetch_result(uri = 'category=1') ⇒ Object
-
#initialize ⇒ Filter
constructor
A new instance of Filter.
Constructor Details
#initialize ⇒ Filter
Returns a new instance of Filter.
26 27 28 29 30 31 32 33 |
# File 'lib/pinkoi/pinkoi_scraper.rb', line 26 def initialize @result = [] # xml selectors that will be used to scrape data @item_selector = "//div[contains(@class,\'items\')]/div" @title_selector = "div[contains(@class,\'title\')]" @price_selector = "div[@class=\'info\']/div[@class=\'price\']" @site_url = 'http://www.pinkoi.com/browse?' end |
Instance Attribute Details
#item_selector=(value) ⇒ Object (writeonly)
Sets the attribute item_selector
10 11 12 |
# File 'lib/pinkoi/pinkoi_scraper.rb', line 10 def item_selector=(value) @item_selector = value end |
#price_selector=(value) ⇒ Object (writeonly)
Sets the attribute price_selector
12 13 14 |
# File 'lib/pinkoi/pinkoi_scraper.rb', line 12 def price_selector=(value) @price_selector = value end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
9 10 11 |
# File 'lib/pinkoi/pinkoi_scraper.rb', line 9 def result @result end |
#site_url=(value) ⇒ Object (writeonly)
Sets the attribute site_url
13 14 15 |
# File 'lib/pinkoi/pinkoi_scraper.rb', line 13 def site_url=(value) @site_url = value end |
#title_selector=(value) ⇒ Object (writeonly)
Sets the attribute title_selector
11 12 13 |
# File 'lib/pinkoi/pinkoi_scraper.rb', line 11 def title_selector=(value) @title_selector = value end |
Instance Method Details
#fetch_result(uri = 'category=1') ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/pinkoi/pinkoi_scraper.rb', line 35 def fetch_result(uri = 'category=1') url = @site_url + uri # try to open the url document = get_xmldata(url) # hard return on an error return [] unless document != 'error' items = document.xpath(@item_selector) # loop through the items and get the title and price items.map do |item| title = item.xpath(@title_selector).text price = item.xpath(@price_selector).text @result << { title: "#{title}", price: "#{price}" } unless title.empty? end result end |