Module: Jekyll::Unirate::Filters
- Defined in:
- lib/jekyll/unirate/filters.rb
Overview
Liquid filters for use in any Jekyll template. All are prefixed ‘unirate_` to avoid clashing with site- or theme-defined filters.
{{ 100 | unirate_convert: "USD", "EUR" }} => 92.0
{{ "USD" | unirate_rate: "EUR" }} => 0.92
{{ 92.5 | unirate_money: "EUR" }} => "€92.50"
{{ 100 | unirate_price: "USD", "EUR" }} => "€92.00"
Every filter degrades gracefully when no snapshot is available or a pair is missing: ‘unirate_convert`/`unirate_price` fall back to the input amount (so a price still renders), `unirate_rate` returns nil. This keeps a transient API problem from breaking the rendered page.
Instance Method Summary collapse
-
#unirate_convert(amount, from, to) ⇒ Object
Convert
amountfromfromtoto(Float). -
#unirate_money(amount, currency) ⇒ Object
Format a bare numeric
amountincurrency(e.g. “€92.50”). -
#unirate_price(amount, from, to) ⇒ Object
Convert
amountfromfromtotoand format it into. -
#unirate_rate(from, to) ⇒ Object
Numeric exchange rate from
fromtoto(Float), or nil if unknown.
Instance Method Details
#unirate_convert(amount, from, to) ⇒ Object
Convert amount from from to to (Float). Falls back to the input amount unchanged when the rate is unavailable.
34 35 36 37 38 39 40 |
# File 'lib/jekyll/unirate/filters.rb', line 34 def unirate_convert(amount, from, to) snapshot = Snapshot.current return to_number(amount) if snapshot.nil? converted = snapshot.convert(amount, from, to) converted ? converted.to_f : to_number(amount) end |
#unirate_money(amount, currency) ⇒ Object
Format a bare numeric amount in currency (e.g. “€92.50”).
43 44 45 |
# File 'lib/jekyll/unirate/filters.rb', line 43 def unirate_money(amount, currency) Formatter.format(amount, currency) end |
#unirate_price(amount, from, to) ⇒ Object
Convert amount from from to to and format it in to. Falls back to formatting the input amount in to when no rate is available.
49 50 51 |
# File 'lib/jekyll/unirate/filters.rb', line 49 def unirate_price(amount, from, to) Formatter.format(unirate_convert(amount, from, to), to) end |