Class: PinkoiScraper::Filter

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

Overview

filter class basically uses xpath selectors to get attribs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFilter

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

Parameters:

  • value

    the value to set the attribute item_selector to.



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

Parameters:

  • value

    the value to set the attribute price_selector to.



12
13
14
# File 'lib/pinkoi/pinkoi_scraper.rb', line 12

def price_selector=(value)
  @price_selector = value
end

#resultObject (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

Parameters:

  • value

    the value to set the attribute site_url to.



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

Parameters:

  • value

    the value to set the attribute title_selector to.



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