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

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

#unirate_rate(from, to) ⇒ Object

Numeric exchange rate from from to to (Float), or nil if unknown.



24
25
26
27
28
29
30
# File 'lib/jekyll/unirate/filters.rb', line 24

def unirate_rate(from, to)
  snapshot = Snapshot.current
  return nil if snapshot.nil?

  rate = snapshot.rate(from, to)
  rate&.to_f
end