apmex-price 1.0.2 → 1.1.0
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 +4 -4
- data/lib/apmex-price.rb +9 -45
- data/test/test-apmex-price.rb +26 -31
- metadata +8 -10
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 181fe4d5c0d581a0ca72957f4896d5c1e29e3a5c
|
|
4
|
+
data.tar.gz: 6bf68ac2f74c09d8320749993a54b24cda6ecf43
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b5f3442b13c4afbea91e7699703ffe5c65680042c299faa04b84e7dd07bcb59b2fd8ee07647d42a579299f006ac80893aa2117bb6c4b2eeda053741bd91ac37d
|
|
7
|
+
data.tar.gz: 7cc982bd3e1cc9208a453e6951e9c5bcd5c39c72241c5f3fec790a4fcdb09899794188c2d68ae21e018ea0c19ee0a7bb8fda62da4b8f47461fa5a2b0bb83088a
|
data/lib/apmex-price.rb
CHANGED
|
@@ -2,65 +2,29 @@ require 'nokogiri'
|
|
|
2
2
|
require 'open-uri'
|
|
3
3
|
|
|
4
4
|
class ApmexPrice
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
OUNCES_LABEL_CSS = 'div.product-specs > table > tbody > tr:nth-child(7) > th'
|
|
10
|
-
OUNCES_CSS = 'div.product-specs > table > tbody > tr:nth-child(7) > td'
|
|
11
|
-
OUNCES2_CSS = 'div.product-specs > table > tbody > tr:nth-child(6) > td'
|
|
12
|
-
SPOT_CSS = 'span.item-bid'
|
|
13
|
-
BUY_CSS = 'div.product-buy-price > h4 > a'
|
|
14
|
-
SELL_CSS = 'table.table-volume-pricing > tbody > tr:nth-child(1) > td:nth-child(2)'
|
|
5
|
+
attr_accessor :url, :page, :buy
|
|
6
|
+
|
|
7
|
+
DOLLARS_REGEX = /(\d*,*\d+\.\d+)/
|
|
8
|
+
BUY_CSS = 'div.page-content-main > div > div > strong'
|
|
15
9
|
|
|
16
10
|
def initialize(url)
|
|
17
11
|
@url = url
|
|
18
12
|
match()
|
|
19
|
-
calculate()
|
|
20
13
|
end
|
|
21
|
-
|
|
22
|
-
|
|
14
|
+
|
|
15
|
+
def match
|
|
23
16
|
@page = Nokogiri::HTML(open(@url))
|
|
24
|
-
|
|
25
|
-
# Get ounces. They are usually the 7th child, but there is at least one case for which they are the 6th.
|
|
26
|
-
if @page.css(OUNCES_LABEL_CSS).text == 'Metal Content:'
|
|
27
|
-
ounces_text = @page.css(OUNCES_CSS).text
|
|
28
|
-
else
|
|
29
|
-
ounces_text = @page.css(OUNCES2_CSS).text
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
ounces_match = OUNCES_REGEX.match(ounces_text)
|
|
33
|
-
@ounces = ounces_match[0].to_f
|
|
34
|
-
|
|
35
|
-
# Get the dollar amounts
|
|
36
|
-
@spot = match_dollars(SPOT_CSS)
|
|
37
17
|
@buy = match_dollars(BUY_CSS)
|
|
38
|
-
@sell = match_dollars(SELL_CSS)
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def calculate
|
|
42
|
-
@ounces_spot = (@spot * @ounces).round(2)
|
|
43
|
-
|
|
44
|
-
if @buy > @ounces_spot
|
|
45
|
-
@value = @buy
|
|
46
|
-
else
|
|
47
|
-
@value = @ounces_spot
|
|
48
|
-
end
|
|
49
18
|
end
|
|
50
|
-
|
|
19
|
+
|
|
51
20
|
def match_dollars(dollars_css)
|
|
52
21
|
dollars_text = @page.css(dollars_css).text
|
|
53
22
|
dollars_match = DOLLARS_REGEX.match(dollars_text)
|
|
54
|
-
|
|
23
|
+
|
|
55
24
|
if dollars_match != nil
|
|
56
25
|
dollars_match[1].gsub(',', '').to_f
|
|
57
26
|
else
|
|
58
27
|
0.0
|
|
59
|
-
end
|
|
28
|
+
end
|
|
60
29
|
end
|
|
61
|
-
|
|
62
|
-
# Aid in creating CSV files
|
|
63
|
-
def to_s
|
|
64
|
-
@value.to_s + ',' + @ounces_spot.to_s + ',' + @buy.to_s + ',' + @sell.to_s
|
|
65
|
-
end
|
|
66
30
|
end
|
data/test/test-apmex-price.rb
CHANGED
|
@@ -4,40 +4,35 @@ require_relative '../lib/apmex-price'
|
|
|
4
4
|
class ApmexPriceTest < Minitest::Test
|
|
5
5
|
def test_
|
|
6
6
|
apmex_urls = [
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
7
|
+
'http://www.apmex.com/sell-to-us/71274',
|
|
8
|
+
'http://www.apmex.com/sell-to-us/59146',
|
|
9
|
+
'http://www.apmex.com/sell-to-us/12082',
|
|
10
|
+
'http://www.apmex.com/sell-to-us/57010',
|
|
11
|
+
'http://www.apmex.com/sell-to-us/59157',
|
|
12
|
+
'http://www.apmex.com/sell-to-us/79019',
|
|
13
|
+
'http://www.apmex.com/sell-to-us/54875',
|
|
14
|
+
'http://www.apmex.com/sell-to-us/54875',
|
|
15
|
+
'http://www.apmex.com/sell-to-us/84445',
|
|
16
|
+
'http://www.apmex.com/sell-to-us/79047',
|
|
17
|
+
'http://www.apmex.com/sell-to-us/79021',
|
|
18
|
+
'http://www.apmex.com/sell-to-us/79573',
|
|
19
|
+
'http://www.apmex.com/sell-to-us/71264',
|
|
20
|
+
'http://www.apmex.com/sell-to-us/79041',
|
|
21
|
+
'http://www.apmex.com/sell-to-us/56011',
|
|
22
|
+
'http://www.apmex.com/sell-to-us/72454',
|
|
23
|
+
'http://www.apmex.com/sell-to-us/72458',
|
|
24
|
+
'http://www.apmex.com/sell-to-us/69514',
|
|
25
|
+
'http://www.apmex.com/sell-to-us/11951',
|
|
26
|
+
'http://www.apmex.com/sell-to-us/57159',
|
|
27
|
+
'http://www.apmex.com/sell-to-us/43309',
|
|
28
|
+
'http://www.apmex.com/sell-to-us/72281',
|
|
29
|
+
'http://www.apmex.com/sell-to-us/72281'
|
|
30
30
|
]
|
|
31
|
-
|
|
32
|
-
puts 'Value,Spot,Buy,Sell'
|
|
33
|
-
|
|
31
|
+
|
|
34
32
|
apmex_urls.each do |u|
|
|
35
33
|
a = ApmexPrice.new(u)
|
|
36
|
-
puts a.to_s
|
|
37
|
-
assert a.value.is_a? Float
|
|
38
|
-
assert a.ounces_spot.is_a? Float
|
|
34
|
+
puts a.buy.to_s
|
|
39
35
|
assert a.buy.is_a? Float
|
|
40
|
-
|
|
41
|
-
end
|
|
36
|
+
end
|
|
42
37
|
end
|
|
43
38
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: apmex-price
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- cscribn
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2016-02-01 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: minitest
|
|
@@ -38,12 +38,11 @@ dependencies:
|
|
|
38
38
|
- - "~>"
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
40
|
version: '0'
|
|
41
|
-
description: Do you own precious metals?
|
|
42
|
-
value of your collection?
|
|
43
|
-
you are interested in, pass it as an argument, and you will get back the
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
website, and will therefore need to be updated whenever the site design changes.
|
|
41
|
+
description: Do you own precious metals? Are you interested in getting the current
|
|
42
|
+
value of your collection? If so, this gem is for you. Simply find the URL of a
|
|
43
|
+
product you are interested in, pass it as an argument, and you will get back the
|
|
44
|
+
Apmex buy price. This program relies on CSS paths of the apmex.com website, and
|
|
45
|
+
will therefore need to be updated whenever the site design changes.
|
|
47
46
|
email:
|
|
48
47
|
executables: []
|
|
49
48
|
extensions: []
|
|
@@ -74,6 +73,5 @@ rubyforge_project:
|
|
|
74
73
|
rubygems_version: 2.2.3
|
|
75
74
|
signing_key:
|
|
76
75
|
specification_version: 4
|
|
77
|
-
summary: Get
|
|
78
|
-
URLs.
|
|
76
|
+
summary: Get buy price of precious metals using apmex.com URLs.
|
|
79
77
|
test_files: []
|