Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done recruitment task #4

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion recruitment_task/converter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
# frozen_string_literal: true

require_relative 'price'

class Converter
# TODO: Implement me
class InvalidPriceObject < StandardError
def message
'Argument must be instance of Price class'
end
end

CURRENCIES = {
eur: { pln: 4.32, usd: 1.13 },
usd: { pln: 3.81, eur: 0.88 },
pln: { eur: 0.23, usd: 0.26 }
}.freeze

def initialize(price)
@price = price
validate_price
end

def convert_to(currency)
raise Price::InvalidCurrency unless Price::SUPPORTED_CURRENCIES.include?(currency)

converted_price = if currency == @price.currency
@price.amount
else
@price.amount * CURRENCIES[@price.currency][currency]
end
converted_price.round 2
end

private

def validate_price
raise InvalidPriceObject unless @price.instance_of? Price
end
end