Skip to content

Commit 1393b53

Browse files
v1.3.0
1 parent 839315e commit 1393b53

File tree

6 files changed

+144
-41
lines changed

6 files changed

+144
-41
lines changed

README.md

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,28 @@ gem 'metalpriceapi'
1515

1616
api_key = 'SET_YOUR_API_KEY_HERE'
1717
client = MetalpriceAPI::Client.new(api_key: api_key)
18+
19+
# Or use EU server:
20+
# client = MetalpriceAPI::Client.new(api_key: api_key, server: 'eu')
1821
```
22+
---
23+
## Server Regions
24+
25+
MetalpriceAPI provides two regional endpoints. Choose the one closest to your servers for optimal performance.
26+
27+
| Region | Base URL |
28+
|--------|----------|
29+
| United States (default) | `https://api.metalpriceapi.com/v1` |
30+
| Europe | `https://api-eu.metalpriceapi.com/v1` |
31+
32+
```ruby
33+
# Default (US)
34+
client = MetalpriceAPI::Client.new(api_key: 'SET_YOUR_API_KEY_HERE')
35+
36+
# Europe
37+
client = MetalpriceAPI::Client.new(api_key: 'SET_YOUR_API_KEY_HERE', server: 'eu')
38+
```
39+
1940
---
2041
## Documentation
2142

@@ -27,32 +48,62 @@ client.fetchSymbols()
2748
[Link](https://metalpriceapi.com/documentation#api_symbol)
2849

2950
---
30-
#### fetchLive(base, currencies)
51+
#### setServer(server)
52+
53+
- `server` <[string]> Pass `'eu'` to use the EU server (`api-eu.metalpriceapi.com`), or `'us'` for the US server. Defaults to US if not specified.
54+
55+
```ruby
56+
client.setServer('eu')
57+
```
58+
59+
---
60+
#### fetchLive(base, currencies, unit, purity, math)
3161

3262
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
3363
- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.
64+
- `unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
65+
- `purity` <[string]> Optional. Pass in a purity level for metal prices.
66+
- `math` <[string]> Optional. Pass in a math expression to apply to the rates.
3467

3568
```ruby
36-
client.fetchLive(base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'])
69+
client.fetchLive('USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz')
3770
```
3871

3972
[Link](https://metalpriceapi.com/documentation#api_realtime)
4073

4174
---
42-
#### fetchHistorical(date, base, currencies)
75+
#### fetchHistorical(date, base, currencies, unit)
4376

4477
- `date` <[string]> Required. Pass in a string with format `YYYY-MM-DD`
4578
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
4679
- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.
80+
- `unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
4781

4882
```ruby
49-
client.fetchHistorical(date='2021-04-05', base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'])
83+
client.fetchHistorical('2024-02-05', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz')
5084
```
5185

5286
[Link](https://metalpriceapi.com/documentation#api_historical)
5387

5488
---
55-
#### fetchOHLC(base, currency, date, unit, dateType)
89+
#### hourly(base, currency, unit, start_date, end_date, math, date_type)
90+
91+
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
92+
- `currency` <[string]> Required. Specify currency you would like to get hourly rates for.
93+
- `unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
94+
- `start_date` <[string]> Required. Specify the start date using the format `YYYY-MM-DD`.
95+
- `end_date` <[string]> Required. Specify the end date using the format `YYYY-MM-DD`.
96+
- `math` <[string]> Optional. Pass in a math expression to apply to the rates.
97+
- `date_type` <[string]> Optional. Pass in a date type, overrides date parameters if passed in.
98+
99+
```ruby
100+
client.hourly('USD', 'XAU', 'troy_oz', '2025-11-03', '2025-11-03')
101+
```
102+
103+
[Link](https://metalpriceapi.com/documentation#api_hourly)
104+
105+
---
106+
#### fetchOHLC(base, currency, date, unit, date_type)
56107

57108
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
58109
- `currency` <[string]> Required. Specify currency you would like to get OHLC for.
@@ -61,60 +112,65 @@ client.fetchHistorical(date='2021-04-05', base='USD', currencies=['XAU', 'XAG',
61112
- `date_type` <[string]> Optional. Pass in a date type, overrides date parameter if passed in.
62113

63114
```ruby
64-
client.fetchOHLC(base='USD', currency='XAU', date='2024-02-05', unit='troy_oz', date_type=nil)
115+
client.fetchOHLC('USD', 'XAU', '2024-02-06', 'troy_oz')
65116
```
66117

67118
[Link](https://metalpriceapi.com/documentation#api_ohlc)
68119

69120
---
70-
#### convert(from_currency, to_currency, amount, date)
121+
#### convert(from_currency, to_currency, amount, date, unit)
71122

72123
- `from_currency` <[string]> Optional. Pass in a base currency, defaults to USD.
73124
- `to_currency` <[string]> Required. Specify currency you would like to convert to.
74125
- `amount` <[number]> Required. The amount to convert.
75126
- `date` <[string]> Optional. Specify date to use historical midpoint value for conversion with format `YYYY-MM-DD`. Otherwise, it will use live exchange rate date if value not passed in.
127+
- `unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
76128

77129
```ruby
78-
client.convert(from_currency='USD', to_currency='EUR', amount=100, date='2021-04-05')
130+
client.convert('USD', 'EUR', 100, '2024-02-05')
79131
```
80132

81133
[Link](https://metalpriceapi.com/documentation#api_convert)
82134

83135
---
84-
#### timeframe(start_date, end_date, base, currencies)
136+
#### timeframe(start_date, end_date, base, currencies, unit)
85137

86138
- `start_date` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
87139
- `end_date` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
88140
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
89141
- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.
142+
- `unit` <[string]> Optional. Pass in a unit for metal prices (e.g. `troy_oz`, `gram`, `kilogram`).
90143

91144
```ruby
92-
client.timeframe(start_date='2021-04-05', end_date='2021-04-06', base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'])
145+
client.timeframe('2024-02-05', '2024-02-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz')
93146
```
94147

95148
[Link](https://metalpriceapi.com/documentation#api_timeframe)
96149

97150
---
98-
#### change(start_date, end_date, base, currencies)
151+
#### change(start_date, end_date, base, currencies, date_type)
99152

100153
- `start_date` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
101154
- `end_date` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
102155
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
103156
- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.
157+
- `date_type` <[string]> Optional. Pass in a date type, overrides date parameters if passed in.
104158

105159
```ruby
106-
client.change(start_date='2021-04-05', end_date='2021-04-06', base='USD', currencies=['XAU', 'XAG', 'XPD', 'XPT'])
160+
client.change('2024-02-05', '2024-02-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'])
107161
```
108162

109163
[Link](https://metalpriceapi.com/documentation#api_change)
164+
110165
---
111-
#### carat(start_date, end_date, base, currencies)
166+
#### carat(base, currency, date)
112167

113168
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
169+
- `currency` <[string]> Optional. Pass in a metal code to get carat prices for (defaults to XAU).
114170
- `date` <[string]> Optional. Specify date to get Carat for specific date using format `YYYY-MM-DD`. If not specified, uses live rates.
115171

116172
```ruby
117-
client.carat(base='USD', date='2021-04-06')
173+
client.carat('USD', 'XAU', '2024-02-06')
118174
```
119175

120176
[Link](https://metalpriceapi.com/documentation#api_carat)
@@ -128,6 +184,9 @@ client.usage()
128184

129185
[Link](https://metalpriceapi.com/documentation#api_usage)
130186

187+
---
188+
**[Official documentation](https://metalpriceapi.com/documentation)**
189+
131190
---
132191
## FAQ
133192

example/index.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,36 @@
22

33
API_KEY = 'REPLACE_ME'
44

5-
MetalpriceAPI::VERSION
5+
client = MetalpriceAPI::Client.new(api_key: API_KEY)
66

7-
client = MetalpriceAPI::Client.new(
8-
api_key: API_KEY
9-
)
7+
# Or use EU server:
8+
# client = MetalpriceAPI::Client.new(api_key: API_KEY, server: 'eu')
109

1110
data = client.fetchSymbols()
1211
puts data
1312

14-
data = client.fetchLive()
13+
data = client.fetchLive('USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz')
1514
puts data
1615

17-
data = client.fetchHistorical('2021-04-05', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'])
16+
data = client.fetchHistorical('2024-02-05', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz')
1817
puts data
1918

20-
data = client.fetchOHLC('USD', 'XAU', '2024-02-06', 'troy_oz', nil)
19+
data = client.hourly('USD', 'XAU', 'troy_oz', '2025-11-03', '2025-11-03')
2120
puts data
2221

23-
data = client.convert('USD', 'EUR', 100, '2021-04-05')
22+
data = client.fetchOHLC('USD', 'XAU', '2024-02-06', 'troy_oz')
2423
puts data
2524

26-
data = client.timeframe('2021-04-05', '2021-04-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'])
25+
data = client.convert('USD', 'EUR', 100, '2024-02-05')
2726
puts data
2827

29-
data = client.change('2021-04-05', '2021-04-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'])
28+
data = client.timeframe('2024-02-05', '2024-02-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'], 'troy_oz')
3029
puts data
3130

32-
data = client.carat('USD', '2021-04-06')
31+
data = client.change('2024-02-05', '2024-02-06', 'USD', ['XAU', 'XAG', 'XPD', 'XPT'])
32+
puts data
33+
34+
data = client.carat('USD', 'XAU', '2024-02-06')
3335
puts data
3436

3537
data = client.usage()

lib/metalpriceapi/client.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ def initialize(options = {})
1313
send("#{key}=", options[key] || MetalpriceAPI.config.send(key))
1414
end
1515
@logger ||= MetalpriceAPI::Logger.logger
16+
# If server was explicitly passed, update endpoint accordingly
17+
if options.key?(:server)
18+
self.endpoint = MetalpriceAPI::SERVERS.fetch(options[:server], MetalpriceAPI::SERVERS['us'])
19+
end
20+
end
21+
22+
def setServer(server)
23+
self.endpoint = MetalpriceAPI::SERVERS.fetch(server, MetalpriceAPI::SERVERS['us'])
24+
@connection = nil # reset memoized connection
1625
end
1726

1827
class << self

lib/metalpriceapi/config.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
# frozen_string_literal: true
22

33
module MetalpriceAPI
4+
SERVERS = {
5+
'us' => 'https://api.metalpriceapi.com/v1',
6+
'eu' => 'https://api-eu.metalpriceapi.com/v1',
7+
}.freeze
8+
49
module Config
510
extend self
611

712
ATTRIBUTES = %i[
813
endpoint
914
api_key
15+
server
1016
proxy
1117
user_agent
1218
ca_path
@@ -19,7 +25,8 @@ module Config
1925
attr_accessor(*Config::ATTRIBUTES)
2026

2127
def reset
22-
self.endpoint = 'https://api.metalpriceapi.com/v1'
28+
self.server = 'us'
29+
self.endpoint = MetalpriceAPI::SERVERS['us']
2330
self.api_key = nil
2431
self.user_agent = "MetalpriceAPI Ruby Client/#{MetalpriceAPI::VERSION}"
2532
self.ca_path = defined?(OpenSSL) ? OpenSSL::X509::DEFAULT_CERT_DIR : nil
@@ -29,6 +36,11 @@ def reset
2936
self.timeout = nil
3037
self.open_timeout = nil
3138
end
39+
40+
def server=(value)
41+
@server = value
42+
self.endpoint = MetalpriceAPI::SERVERS.fetch(value, MetalpriceAPI::SERVERS['us'])
43+
end
3244
end
3345

3446
class << self

lib/metalpriceapi/endpoints/index.rb

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,88 @@ def fetchSymbols
66
get('symbols')
77
end
88

9-
def fetchLive(base = nil, currencies = nil)
9+
def fetchLive(base = nil, currencies = nil, unit = nil, purity = nil, math = nil)
1010
options = removeEmpty({
1111
base: base,
12-
currencies: (currencies || []).join(',')
12+
currencies: (currencies || []).join(','),
13+
unit: unit,
14+
purity: purity,
15+
math: math,
1316
})
1417
get('latest', options)
1518
end
1619

17-
def fetchHistorical(date, base = nil, currencies = nil)
20+
def fetchHistorical(date, base = nil, currencies = nil, unit = nil)
1821
options = removeEmpty({
1922
base: base,
20-
currencies: (currencies || []).join(',')
23+
currencies: (currencies || []).join(','),
24+
unit: unit,
2125
})
2226
get(date, options)
2327
end
2428

29+
def hourly(base = nil, currency = nil, unit = nil, start_date = nil, end_date = nil, math = nil, date_type = nil)
30+
options = removeEmpty({
31+
base: base,
32+
currency: currency,
33+
unit: unit,
34+
start_date: start_date,
35+
end_date: end_date,
36+
math: math,
37+
date_type: date_type,
38+
})
39+
get('hourly', options)
40+
end
41+
2542
def fetchOHLC(base = nil, currency = nil, date = nil, unit = nil, date_type = nil)
2643
options = removeEmpty({
2744
base: base,
2845
currency: currency,
2946
date: date,
3047
unit: unit,
31-
date_type: dateType
48+
date_type: date_type,
3249
})
3350
get('ohlc', options)
3451
end
3552

36-
def convert(from_currency = nil, to_currency = nil, amount = nil, date = nil)
53+
def convert(from_currency = nil, to_currency = nil, amount = nil, date = nil, unit = nil)
3754
options = removeEmpty({
3855
'from': from_currency,
3956
'to': to_currency,
4057
'amount': amount,
41-
'date': date
58+
'date': date,
59+
'unit': unit,
4260
})
4361
get('convert', options)
4462
end
4563

46-
def timeframe(start_date, end_date, base = nil, currencies = nil)
64+
def timeframe(start_date, end_date, base = nil, currencies = nil, unit = nil)
4765
options = removeEmpty({
4866
'start_date': start_date,
4967
'end_date': end_date,
5068
'base': base,
51-
'currencies': (currencies || []).join(',')
69+
'currencies': (currencies || []).join(','),
70+
'unit': unit,
5271
})
5372
get('timeframe', options)
5473
end
5574

56-
def change(start_date, end_date, base = '', currencies = nil)
75+
def change(start_date, end_date, base = nil, currencies = nil, date_type = nil)
5776
options = removeEmpty({
5877
'start_date': start_date,
5978
'end_date': end_date,
6079
'base': base,
61-
'currencies': (currencies || []).join(',')
80+
'currencies': (currencies || []).join(','),
81+
'date_type': date_type,
6282
})
6383
get('change', options)
6484
end
6585

66-
def carat(base = '', date = nil)
86+
def carat(base = nil, currency = nil, date = nil)
6787
options = removeEmpty({
6888
'base': base,
69-
'date': date
89+
'currency': currency,
90+
'date': date,
7091
})
7192
get('carat', options)
7293
end
@@ -79,7 +100,7 @@ def usage
79100

80101
def removeEmpty(options)
81102
options.each do |key, value|
82-
if (!value || value == '')
103+
if (value.nil? || value == '')
83104
options.delete(key)
84105
end
85106
end

0 commit comments

Comments
 (0)