Class: CurrencyUpdater::Updater

Inherits:
Object
  • Object
show all
Defined in:
lib/currency_updater/updater.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(currency = nil) ⇒ Updater

———————————————————————-# instance methods ———————————————————————-#



37
38
39
40
# File 'lib/currency_updater/updater.rb', line 37

def initialize(currency = nil)
  @currencies = Currencies.new
  Updater.select_country_codes(currency)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/currency_updater/updater.rb', line 42

def method_missing(method,*args,&block)
  if @currencies.respond_to?(method)
    @currencies.send(method,*args,&block)
  else
    super
  end
end

Class Method Details

.initialize_currenciesObject



14
15
16
17
18
19
# File 'lib/currency_updater/updater.rb', line 14

def initialize_currencies
  Currencies.clear
  @@currency_codes.each do |currency|
    Currencies.add(currency)
  end
end

.search_rate_for(currency) ⇒ Object



29
30
31
32
# File 'lib/currency_updater/updater.rb', line 29

def search_rate_for(currency)
  raise "invalid currency" unless currency
  CurrencyUpdater.rate_finder.call(currency)
end

.select_country_codes(currency) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/currency_updater/updater.rb', line 4

def select_country_codes(currency)
  @@currency_codes = if currency.nil?
                       Currencies.all_codes
                     else
                       Array.new([currency]).flatten
                     end
  initialize_currencies
  update_currencies
end

.update_currenciesObject



21
22
23
24
25
26
27
# File 'lib/currency_updater/updater.rb', line 21

def update_currencies
  currencies = Currencies.all
  currencies.each do |currency|
    rate = search_rate_for(currency.code)
    currency.rate = rate
  end
end

Instance Method Details

#to_jsonObject



50
51
52
# File 'lib/currency_updater/updater.rb', line 50

def to_json
  @currencies.to_json
end

#to_xmlObject



54
55
56
# File 'lib/currency_updater/updater.rb', line 54

def to_xml
  @currencies.to_xml
end

#to_yamlObject



58
59
60
# File 'lib/currency_updater/updater.rb', line 58

def to_yaml
  @currencies.to_yaml
end