class FixerApiClient::CurrencyData
Public Class Methods
rates()
click to toggle source
# File lib/fixer_api_client/currency_data.rb, line 7 def self.rates new.rates end
rates_creation_date()
click to toggle source
# File lib/fixer_api_client/currency_data.rb, line 15 def self.rates_creation_date new.rates_creation_date end
symbols()
click to toggle source
# File lib/fixer_api_client/currency_data.rb, line 11 def self.symbols new.symbols end
Public Instance Methods
rates()
click to toggle source
# File lib/fixer_api_client/currency_data.rb, line 24 def rates @rates ||= parse(rates_filename, 'rates')['rates'] end
rates_creation_date()
click to toggle source
# File lib/fixer_api_client/currency_data.rb, line 19 def rates_creation_date timestamp = parse(rates_filename, 'rates')['timestamp'] timestamp && Time.strptime(timestamp.to_s,'%s').utc.to_s end
symbols()
click to toggle source
# File lib/fixer_api_client/currency_data.rb, line 28 def symbols @symbols ||= parse(currencies_filename, 'symbols')['symbols'] end
Private Instance Methods
api_key()
click to toggle source
# File lib/fixer_api_client/currency_data.rb, line 92 def api_key raise 'You need an api key' unless ENV['FIXER_IO_API_KEY'] ENV['FIXER_IO_API_KEY'] end
connection()
click to toggle source
# File lib/fixer_api_client/currency_data.rb, line 88 def connection @connection ||= FixerApiClient::Connection.connection end
currencies_filename()
click to toggle source
# File lib/fixer_api_client/currency_data.rb, line 83 def currencies_filename @currencies_filename ||= File.join(File.dirname(__dir__), 'data', env, 'currency_symbols.json') end
env()
click to toggle source
# File lib/fixer_api_client/currency_data.rb, line 79 def env tests? ? 'test' : 'production' end
parse(filename, data_path)
click to toggle source
# File lib/fixer_api_client/currency_data.rb, line 34 def parse(filename, data_path) data = if update?(filename) results = connection.get("#{url_for(data_path)}?access_key=#{api_key}") if results.status == 200 file = File.open(filename, 'w') file.puts(results.body.to_json) file.close results.body.to_json else File.read(filename) end else File.read(filename) end JSON.parse(data) end
rates_filename()
click to toggle source
# File lib/fixer_api_client/currency_data.rb, line 74 def rates_filename @rates_filename ||= File.join(File.dirname(__dir__), 'data', env,'rates_data.json') end
tests?()
click to toggle source
# File lib/fixer_api_client/currency_data.rb, line 70 def tests? ENV['fixer_api_client_tests'].to_s == 'true' end
update?(filename)
click to toggle source
# File lib/fixer_api_client/currency_data.rb, line 60 def update?(filename) return false if tests? return false unless File.exists?(filename) && !File.zero?(filename) return false if filename =~ /currency_symbols/ # currency symbols file doesn't have timestamp... data = File.read(filename) json = JSON.parse(data) ((Time.now.utc - Time.at(json['timestamp']).utc) / 60 / 60) > 24 end
url_for(path)
click to toggle source
# File lib/fixer_api_client/currency_data.rb, line 53 def url_for(path) case path when 'rates' then '/latest' when 'symbols' then '/symbols' end end