apmex-price 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/apmex-price.rb +60 -0
- data/test/test-apmex-price.rb +80 -0
- metadata +52 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6ae60dec8d6baec7159656a4d01a68a2b6d7efca
|
4
|
+
data.tar.gz: ea4aacbb141ae9665fbef74cc0860c6390089539
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 68ecada31c593ab14c9d4d79b7a56c19a875e22e7c82e713971e13c4a8d53ae7bad9d42618253cb08c2195171ba4865157ca134cadbae78e6b1b85c1e03fda5d
|
7
|
+
data.tar.gz: 71b6e993996aa5fcd2dec5f6780ea64581d3e8029b056877e090063d4ce826bc615a9eba6a4848b8dce21e5c5a1ed97f30ae91ae8f13e247dc23dd822db61a73
|
data/lib/apmex-price.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'open-uri'
|
4
|
+
|
5
|
+
class ApmexPrice
|
6
|
+
attr_accessor :url, :page, :spot, :ounces_spot, :buy, :sell, :current_value
|
7
|
+
|
8
|
+
DOLLARS_REGEX = /(\d*,*\d+\.\d+)/
|
9
|
+
OUNCES_REGEX = /(\d*,*\d+\.?\d*)/
|
10
|
+
OUNCES_CSS = 'div.product-specs > table > tbody > tr:nth-child(7) > td'
|
11
|
+
SPOT_CSS = 'span.item-bid'
|
12
|
+
BUY_CSS = 'div.product-buy-price > h4 > a'
|
13
|
+
SELL_CSS = 'table.table-volume-pricing > tbody > tr:nth-child(1) > td:nth-child(2)'
|
14
|
+
|
15
|
+
def initialize(url)
|
16
|
+
@url = url
|
17
|
+
match()
|
18
|
+
calculate()
|
19
|
+
end
|
20
|
+
|
21
|
+
def match
|
22
|
+
@page = Nokogiri::HTML(open(@url))
|
23
|
+
|
24
|
+
# Get ounces
|
25
|
+
ounces_text = @page.css(OUNCES_CSS).text
|
26
|
+
ounces_match = OUNCES_REGEX.match(ounces_text)
|
27
|
+
@ounces = ounces_match[0].to_f
|
28
|
+
|
29
|
+
# Get the dollar amounts
|
30
|
+
@spot = match_dollars(SPOT_CSS)
|
31
|
+
@buy = match_dollars(BUY_CSS)
|
32
|
+
@sell = match_dollars(SELL_CSS)
|
33
|
+
end
|
34
|
+
|
35
|
+
def calculate
|
36
|
+
@ounces_spot = @spot * @ounces
|
37
|
+
|
38
|
+
if @buy != 0.0
|
39
|
+
@current_value = @buy
|
40
|
+
else
|
41
|
+
@current_value = @ounces_spot
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def match_dollars(dollars_css)
|
46
|
+
dollars_text = @page.css(dollars_css).text
|
47
|
+
dollars_match = DOLLARS_REGEX.match(dollars_text)
|
48
|
+
|
49
|
+
if dollars_match != nil
|
50
|
+
dollars_match[1].gsub(',', '').to_f
|
51
|
+
else
|
52
|
+
0.0
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Aid in creating CSV files
|
57
|
+
def to_s
|
58
|
+
@current_value.to_s + ',' + @spot.to_s + ',' + @buy.to_s + ',' + @sell.to_s
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'apmex-price'
|
3
|
+
|
4
|
+
class ApmexPriceTest < Minitest::Unit::TestCase
|
5
|
+
def test
|
6
|
+
apmex_urls = [
|
7
|
+
'http://www.apmex.com/product/3/1-4-oz-gold-american-eagle-random-year',
|
8
|
+
'http://www.apmex.com/product/3/1-4-oz-gold-american-eagle-random-year',
|
9
|
+
'http://www.apmex.com/product/3/1-4-oz-gold-american-eagle-random-year',
|
10
|
+
'http://www.apmex.com/product/1/1-oz-gold-american-eagle-random-year',
|
11
|
+
'http://www.apmex.com/product/23331/1-oz-silver-american-eagle-random-year',
|
12
|
+
'http://www.apmex.com/product/23331/1-oz-silver-american-eagle-random-year',
|
13
|
+
'http://www.apmex.com/product/23331/1-oz-silver-american-eagle-random-year',
|
14
|
+
'http://www.apmex.com/product/23331/1-oz-silver-american-eagle-random-year',
|
15
|
+
'http://www.apmex.com/product/79019/2014-1-oz-silver-american-eagle-bu',
|
16
|
+
'http://www.apmex.com/product/79019/2014-1-oz-silver-american-eagle-bu',
|
17
|
+
'http://www.apmex.com/product/79019/2014-1-oz-silver-american-eagle-bu',
|
18
|
+
'http://www.apmex.com/product/79019/2014-1-oz-silver-american-eagle-bu',
|
19
|
+
'http://www.apmex.com/product/79019/2014-1-oz-silver-american-eagle-bu',
|
20
|
+
'http://www.apmex.com/product/79019/2014-1-oz-silver-american-eagle-bu',
|
21
|
+
'http://www.apmex.com/product/79019/2014-1-oz-silver-american-eagle-bu',
|
22
|
+
'http://www.apmex.com/product/54875/2010-australia-1-oz-silver-kookaburra-bu',
|
23
|
+
'http://www.apmex.com/product/54875/2010-australia-1-oz-silver-kookaburra-bu',
|
24
|
+
'http://www.apmex.com/product/54875/2010-australia-1-oz-silver-kookaburra-bu',
|
25
|
+
'http://www.apmex.com/product/84445/2015-australia-1-oz-silver-kookaburra-bu',
|
26
|
+
'http://www.apmex.com/product/84445/2015-australia-1-oz-silver-kookaburra-bu',
|
27
|
+
'http://www.apmex.com/product/84445/2015-australia-1-oz-silver-kookaburra-bu',
|
28
|
+
'http://www.apmex.com/product/84445/2015-australia-1-oz-silver-kookaburra-bu',
|
29
|
+
'http://www.apmex.com/product/84445/2015-australia-1-oz-silver-kookaburra-bu',
|
30
|
+
'http://www.apmex.com/product/84445/2015-australia-1-oz-silver-kookaburra-bu',
|
31
|
+
'http://www.apmex.com/product/84445/2015-australia-1-oz-silver-kookaburra-bu',
|
32
|
+
'http://www.apmex.com/product/79047/2014-1-10-oz-gold-austrian-philharmonic',
|
33
|
+
'http://www.apmex.com/product/79021/2014-austria-1-oz-silver-philharmonic',
|
34
|
+
'http://www.apmex.com/product/79021/2014-austria-1-oz-silver-philharmonic',
|
35
|
+
'http://www.apmex.com/product/79021/2014-austria-1-oz-silver-philharmonic',
|
36
|
+
'http://www.apmex.com/product/79021/2014-austria-1-oz-silver-philharmonic',
|
37
|
+
'http://www.apmex.com/product/79021/2014-austria-1-oz-silver-philharmonic',
|
38
|
+
'http://www.apmex.com/product/79021/2014-austria-1-oz-silver-philharmonic',
|
39
|
+
'http://www.apmex.com/product/79021/2014-austria-1-oz-silver-philharmonic',
|
40
|
+
'http://www.apmex.com/product/79021/2014-austria-1-oz-silver-philharmonic',
|
41
|
+
'http://www.apmex.com/product/79021/2014-austria-1-oz-silver-philharmonic',
|
42
|
+
'http://www.apmex.com/product/79021/2014-austria-1-oz-silver-philharmonic',
|
43
|
+
'http://www.apmex.com/product/79573/2014-1-oz-silver-britannia-brilliant-uncirculated',
|
44
|
+
'http://www.apmex.com/product/12/1-10-oz-gold-canadian-maple-leaf-random-year',
|
45
|
+
'http://www.apmex.com/product/79041/2014-canada-1-4-oz-gold-maple-leaf-bu',
|
46
|
+
'http://www.apmex.com/product/79041/2014-canada-1-4-oz-gold-maple-leaf-bu',
|
47
|
+
'http://www.apmex.com/product/79041/2014-canada-1-4-oz-gold-maple-leaf-bu',
|
48
|
+
'http://www.apmex.com/product/1090/1-oz-silver-canadian-maple-leaf-random-year',
|
49
|
+
'http://www.apmex.com/product/79056/2014-1-10-oz-gold-chinese-panda-sealed',
|
50
|
+
'http://www.apmex.com/product/72458/2013-china-1-oz-silver-panda-bu-in-capsule',
|
51
|
+
'http://www.apmex.com/product/72458/2013-china-1-oz-silver-panda-bu-in-capsule',
|
52
|
+
'http://www.apmex.com/product/1114/1-oz-silver-mexican-libertad-random-year',
|
53
|
+
'http://www.apmex.com/product/1114/1-oz-silver-mexican-libertad-random-year',
|
54
|
+
'http://www.apmex.com/product/1114/1-oz-silver-mexican-libertad-random-year',
|
55
|
+
'http://www.apmex.com/product/1114/1-oz-silver-mexican-libertad-random-year',
|
56
|
+
'http://www.apmex.com/product/11951/1-oz-pamp-suisse-gold-bar-9999-fine-in-assay',
|
57
|
+
'http://www.apmex.com/product/57159/1-oz-perth-mint-gold-bar-9999-fine-in-assay',
|
58
|
+
'http://www.apmex.com/product/21/10-oz-silver-bar-secondary-market-999-fine',
|
59
|
+
'http://www.apmex.com/product/21/10-oz-silver-bar-secondary-market-999-fine',
|
60
|
+
'http://www.apmex.com/product/21/10-oz-silver-bar-secondary-market-999-fine',
|
61
|
+
'http://www.apmex.com/product/21/10-oz-silver-bar-secondary-market-999-fine',
|
62
|
+
'http://www.apmex.com/product/72281/10-oz-sunshine-silver-bar-999-fine-v2',
|
63
|
+
'http://www.apmex.com/product/72281/10-oz-sunshine-silver-bar-999-fine-v2',
|
64
|
+
'http://www.apmex.com/product/72281/10-oz-sunshine-silver-bar-999-fine-v2',
|
65
|
+
'http://www.apmex.com/product/72281/10-oz-sunshine-silver-bar-999-fine-v2',
|
66
|
+
'http://www.apmex.com/product/72281/10-oz-sunshine-silver-bar-999-fine-v2'
|
67
|
+
]
|
68
|
+
|
69
|
+
puts 'Current Value,Spot,Buy,Sell'
|
70
|
+
|
71
|
+
apmex_urls.each do |u|
|
72
|
+
a = ApmexPrice.new(u)
|
73
|
+
puts a.to_s
|
74
|
+
assert a.current_value.is_a? Float
|
75
|
+
assert a.spot.is_a? Float
|
76
|
+
assert a.buy.is_a? Float
|
77
|
+
assert a.sell.is_a? Float
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: apmex-price
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- cscribn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-30 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Do you own precious metals? Are you interested in getting the current
|
14
|
+
value of your collection? If so, this gem is for you. Simply find the URL of a product
|
15
|
+
you are interested in, pass it as an argument, and you will get back the spot, buy,
|
16
|
+
sell, and current value price. The current price is either the APMEX buy price or
|
17
|
+
the spot price if the buy price is not available. This program relies on CSS paths
|
18
|
+
of the apmex.com website, and will therefore need to be updated whenever the site
|
19
|
+
design changes.
|
20
|
+
email:
|
21
|
+
executables: []
|
22
|
+
extensions: []
|
23
|
+
extra_rdoc_files: []
|
24
|
+
files:
|
25
|
+
- lib/apmex-price.rb
|
26
|
+
- test/test-apmex-price.rb
|
27
|
+
homepage: http://rubygems.org/gems/apmex-price
|
28
|
+
licenses:
|
29
|
+
- MIT
|
30
|
+
metadata: {}
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 2.0.15
|
48
|
+
signing_key:
|
49
|
+
specification_version: 4
|
50
|
+
summary: Get spot, buy, sell, and current value prices of precious metals using apmex.com
|
51
|
+
URLs.
|
52
|
+
test_files: []
|