Method: ExportChapterPdf#fetch_exchange_rate

Defined in:
lib/uktt/export_chapter_pdf.rb

#fetch_exchange_rate(currency = @currency) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/uktt/export_chapter_pdf.rb', line 121

def fetch_exchange_rate(currency = @currency)
  return 1.0 unless currency

  return 1.0 if currency === Uktt::PARENT_CURRENCY

  response = ENV.fetch("MX_RATE_EUR_#{currency}") do |_missing_name|
    if currency === 'GBP'
      Uktt::MonetaryExchangeRate.new(version: 'v2').latest(currency)
    else
      raise StandardError.new "Non-GBP currency exchange rates are not available via API and must be manually set with an environment variable, e.g., 'MX_RATE_EUR_#{currency}'"
    end
  end.to_f

  return response if response > 0.0

  raise StandardError.new "Currency error. response=#{response.inspect}"
end