Module: UniRateRails::Helper

Defined in:
lib/unirate_rails/helper.rb

Overview

View helpers mixed into ActionView::Base by Engine so templates can look up rates and conversions inline. Each helper builds a short-lived Client from the process configuration.

<%= unirate_rate("USD", "EUR") %>          => 0.92
<%= unirate_convert(100, "USD", "EUR") %>  => 92.5

Instance Method Summary collapse

Instance Method Details

#unirate_convert(amount, from = nil, to = nil) ⇒ Object

Convert amount from from to to (a Float), or nil on any UniRate error.



22
23
24
25
26
27
# File 'lib/unirate_rails/helper.rb', line 22

def unirate_convert(amount, from = nil, to = nil)
  from ||= UniRateRails.configuration.default_currency
  UniRateRails.client.convert(to: to, amount: amount, from: from)
rescue UniRateRails::UnirateError
  nil
end

#unirate_rate(from = nil, to = nil) ⇒ Object

Current rate from from to to (a Float), or nil on any UniRate error so a rate hiccup never raises inside a view render.



13
14
15
16
17
18
# File 'lib/unirate_rails/helper.rb', line 13

def unirate_rate(from = nil, to = nil)
  from ||= UniRateRails.configuration.default_currency
  UniRateRails.client.get_rate(from: from, to: to)
rescue UniRateRails::UnirateError
  nil
end