joyceshop 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a3acee65aadff3a04affd99a74cdf5504632c695
4
- data.tar.gz: 2709281e4846fe17d1a70649554aa87ddbea2b81
3
+ metadata.gz: f8dbce4f8ea05d23d63f3d9c49bc5819afeb7bd7
4
+ data.tar.gz: ddc747fbfb484c3f4dc4f8d3e513153a243bd01c
5
5
  SHA512:
6
- metadata.gz: 2f512e054122cfe33a784207d417de2539687646cf23127dbefe5719270c78c6e7f8740c7103c4242a1ab506b1af2f934a733389c48ba05624243e3cfc3d0d5f
7
- data.tar.gz: af79d4953a8ad049e69f7074ca7bbc575ecc5e3df6518a9c6b53b08b0006f6b67ab46a94a625de4be3cc608f47999e7dc00bc9d2ec6f74912dd44dc249953632
6
+ metadata.gz: ed50d49f299ae7bf30e8f86f3e9284d4d9dd0eaebe325b0a9f7f3b01664e652b57faf20b57b05b17ab1b26ba6ac0149dd43693927e49890ab1b8db57e0ef1bf4
7
+ data.tar.gz: c7cd6da017475ef2f3b34d92774407f3fc7916f97c416ab9b0094488f605a302d38b554096f798dd6fda6f88aac53dc2ed82f53fffc0a546fa569caddfac6740
@@ -3,4 +3,4 @@
3
3
  require_relative '../lib/joyceshop.rb' # for testing
4
4
 
5
5
  scraper = JoyceShop::Scraper.new()
6
- puts scraper.accessories(1)
6
+ puts scraper.search('紗針織衫', {price_boundary: [100, 443]})
@@ -24,34 +24,40 @@ module JoyceShop
24
24
  # Regular
25
25
  @@TITLE_REGEX = /([.\p{Han}[a-zA-Z]]+)/
26
26
 
27
- def latest(page)
27
+ def latest(page, options={})
28
28
  uri = uri_with_page(@@LATEST_URI, page)
29
29
  body = fetch_data(uri)
30
- filter(body)
30
+ data = parse_html(body)
31
+ filter(data, options)
31
32
  end
32
33
 
33
- def popular(page)
34
+ def popular(page, options={})
34
35
  uri = uri_with_page(@@POPULAR_URI, page)
35
36
  body = fetch_data(uri)
36
- filter(body)
37
+ data = parse_html(body)
38
+ filter(data, options)
37
39
  end
38
40
 
39
- def tops(page)
41
+ def tops(page, options={})
40
42
  uri = uri_with_page(@@TOPS_URI, page)
41
43
  body = fetch_data(uri)
42
- filter(body)
44
+ data = parse_html(body)
45
+ filter(data, options)
43
46
  end
44
47
 
45
- def pants(page)
48
+ def pants(page, options={})
46
49
  uri = uri_with_page(@@PANTS_URI, page)
47
50
  body = fetch_data(uri)
48
- filter(body)
51
+ data = parse_html(body)
52
+ filter(data, options)
49
53
  end
50
54
 
51
- def accessories(page)
55
+ def accessories(page, options={})
52
56
  uri = uri_with_page(@@ACCESSORIES_URI, page)
53
57
  body = fetch_data(uri)
54
- filter(body)
58
+ data = parse_html(body)
59
+ filter(data, options)
60
+ end
55
61
  end
56
62
 
57
63
  private
@@ -60,10 +66,31 @@ module JoyceShop
60
66
  end
61
67
 
62
68
  def fetch_data(uri)
63
- open(uri) {|file| file.read}
69
+ open(uri) { |file| file.read }
70
+ end
71
+
72
+ # Filter
73
+ # ------------------------------------------------------------
74
+ def filter(data, options)
75
+ results = data
76
+
77
+ unless options.empty?
78
+ results = match_price(results, options[:price_boundary]) if options[:price_boundary]
79
+ end
80
+
81
+ results
82
+ end
83
+
84
+ def match_price(data, boundary)
85
+ lower_bound = boundary.first || 0
86
+ upper_bound = boundary.last || Float::INFINITY
87
+
88
+ data.select { |item| lower_bound <= item[:price] && item[:price] <= upper_bound }
64
89
  end
65
90
 
66
- def filter(raw)
91
+ # Parser
92
+ # ------------------------------------------------------------
93
+ def parse_html(raw)
67
94
  Oga.parse_html(raw)
68
95
  .xpath(@@ITEM_SELECTOR)
69
96
  .map { |item| parse(item) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: joyceshop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Even Chang
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2015-12-14 00:00:00.000000000 Z
14
+ date: 2016-01-03 00:00:00.000000000 Z
15
15
  dependencies: []
16
16
  description: This is a gem scraping joyceshop's website and returns the popular/latest
17
17
  items