Class: UniRateRails::RatesController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/unirate_rails/rates_controller.rb

Overview

JSON proxy endpoints for the UniRate API. Mount the engine to expose them:

mount UniRateRails::Engine => "/unirate"

then:

GET /unirate/rate?from=USD&to=EUR
GET /unirate/convert?from=USD&to=EUR&amount=100
GET /unirate/currencies

The API key never leaves the server — it lives in the engine configuration, not in the browser. Typed UniRate errors are mapped back to sensible HTTP statuses.

Instance Method Summary collapse

Instance Method Details

#convertObject



23
24
25
26
27
28
29
30
# File 'app/controllers/unirate_rails/rates_controller.rb', line 23

def convert
  result = client.convert(
    to: params[:to],
    amount: Float(params[:amount] || 1),
    from: params[:from] || "USD"
  )
  render json: { result: result }
end

#currenciesObject



32
33
34
# File 'app/controllers/unirate_rails/rates_controller.rb', line 32

def currencies
  render json: { currencies: client.get_supported_currencies }
end

#rateObject



19
20
21
# File 'app/controllers/unirate_rails/rates_controller.rb', line 19

def rate
  render json: { rate: client.get_rate(from: params[:from] || "USD", to: params[:to]) }
end