Module: Sinatra::UniRate::Helpers
- Defined in:
- lib/sinatra/unirate.rb
Overview
Helpers mixed into every route/view when the extension is registered. Each helper builds a short-lived SinatraClient from the app's settings.
Instance Method Summary collapse
-
#unirate_client ⇒ Object
A SinatraClient built from the current app settings.
-
#unirate_convert(amount, from = nil, to = nil) ⇒ Object
Convert
amountfromfromtoto(a Float). -
#unirate_currencies ⇒ Object
GET /api/currencies.
-
#unirate_proxy ⇒ Object
Shared wrapper for the mounted JSON proxy routes.
-
#unirate_rate(from = nil, to = nil) ⇒ Object
GET /api/rates.
-
#unirate_vat(country = nil) ⇒ Object
GET /api/vat/rates.
Instance Method Details
#unirate_client ⇒ Object
A SinatraClient built from the current app settings. A fresh instance
is returned each call so set :unirate_* changes are always picked up.
50 51 52 |
# File 'lib/sinatra/unirate.rb', line 50 def unirate_client Sinatra::UniRate.client_for(settings) end |
#unirate_convert(amount, from = nil, to = nil) ⇒ Object
Convert amount from from to to (a Float). from defaults to the
app's :unirate_default_currency setting.
63 64 65 66 |
# File 'lib/sinatra/unirate.rb', line 63 def unirate_convert(amount, from = nil, to = nil) from ||= settings.unirate_default_currency unirate_client.convert(to: to, amount: amount, from: from) end |
#unirate_currencies ⇒ Object
GET /api/currencies. Returns an Array of currency-code strings.
69 70 71 |
# File 'lib/sinatra/unirate.rb', line 69 def unirate_currencies unirate_client.get_supported_currencies end |
#unirate_proxy ⇒ Object
Shared wrapper for the mounted JSON proxy routes. Falls through to a 404 unless :unirate_mount_routes is enabled, renders JSON on success, and maps a typed UniRate error to its HTTP status. Not intended for direct use in host-app routes.
83 84 85 86 87 88 89 |
# File 'lib/sinatra/unirate.rb', line 83 def unirate_proxy pass unless settings.unirate_mount_routes content_type :json yield.to_json rescue ::UniRate::UnirateError => e halt(e.status || 502, { error: e. }.to_json) end |
#unirate_rate(from = nil, to = nil) ⇒ Object
GET /api/rates. Returns a Float when to is given, else a code=>Float
map. from defaults to the app's :unirate_default_currency setting.
56 57 58 59 |
# File 'lib/sinatra/unirate.rb', line 56 def unirate_rate(from = nil, to = nil) from ||= settings.unirate_default_currency unirate_client.get_rate(from: from, to: to) end |
#unirate_vat(country = nil) ⇒ Object
GET /api/vat/rates. Returns the whole country map when country is nil,
otherwise the single-country vat_data hash.
75 76 77 |
# File 'lib/sinatra/unirate.rb', line 75 def unirate_vat(country = nil) unirate_client.get_vat_rates(country: country) end |