Skip to content

Commit

Permalink
Add exchange rates endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Calyhre committed May 15, 2017
1 parent ccdaa3e commit 903fe98
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
24 changes: 23 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base
request.format = :csv
end

def index
def prices
games = Game.all.includes(:prices).sort_by(&:title).group_by(&:parsed_game_code)
countries = Price.pluck(:country).uniq.sort
currency = Price.pluck(:currency).include?(params[:currency]) ? params[:currency] : nil
Expand All @@ -30,4 +30,26 @@ def index
format.csv { send_data csv }
end
end

def rates
exchange_rates = ExchangeRate.all
currencies = exchange_rates.pluck(:from).uniq.sort

csv = CSV.generate(headers: true) do |rows|
rows << [''].concat(currencies)

currencies.each do |from|
row = [from]
currencies.each do |to|
exchange_rate = exchange_rates.find_by(from: from, to: to)
row << (exchange_rate.present? ? exchange_rate.rate : 1)
end
rows << row
end
end

respond_to do |format|
format.csv { send_data csv }
end
end
end
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

get :prices, to: 'application#index'
get :prices, to: 'application#prices'
get :rates, to: 'application#rates'
end

0 comments on commit 903fe98

Please sign in to comment.