Skip to content

Commit

Permalink
Add best deals
Browse files Browse the repository at this point in the history
  • Loading branch information
Calyhre committed May 15, 2017
1 parent f7cd06f commit ccc2d71
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
20 changes: 19 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def prices
currency = Price.pluck(:currency).include?(params[:currency]) ? params[:currency] : nil

csv = CSV.generate(headers: true) do |rows|
rows << %w(title).concat(countries)
rows << %w(Title).concat(countries)
games.each do |code, games|
game = games.first
prices = games.flat_map(&:prices)
Expand All @@ -25,6 +25,24 @@ def prices
send_data csv, type: 'text/csv; charset=utf-8; header=present'
end

def best_deals
games = Game.all.includes(:prices).sort_by(&:title).group_by(&:parsed_game_code)
currencies = Price.pluck(:currency).uniq.sort

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

games.each do |code, games|
game = games.first
lowest = games.flat_map(&:prices).min_by { |price| price.value.exchange_to('USD') }
rates = currencies.map { |c| lowest.value.exchange_to(c) }
rows << [game.title, lowest.country].concat(rates)
end
end

send_data csv, type: 'text/csv; charset=utf-8; header=present'
end

def rates
currencies = Price.pluck(:currency).uniq.sort

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

get :prices, to: 'application#prices'
get :best_deals, to: 'application#best_deals'
get :rates, to: 'application#rates'
get :glossary, to: 'application#glossary'
end

0 comments on commit ccc2d71

Please sign in to comment.