Class: Currency::Exchange::Rate::Source::Failover

Inherits:
Base
  • Object
show all
Defined in:
lib/currency/exchange/rate/source/failover.rb

Overview

Gets Rates from primary source, if primary fails, attempts secondary source.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#primaryObject

Primary rate source.



11
12
13
# File 'lib/currency/exchange/rate/source/failover.rb', line 11

def primary
  @primary
end

#secondaryObject

Secondary rate source if primary fails.



14
15
16
# File 'lib/currency/exchange/rate/source/failover.rb', line 14

def secondary
  @secondary
end

Instance Method Details

#clear_ratesObject



21
22
23
24
25
# File 'lib/currency/exchange/rate/source/failover.rb', line 21

def clear_rates
  @primary.clear_rates
  @secondary.clear_rates
  super
end

#get_rate(c1, c2, time) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/currency/exchange/rate/source/failover.rb', line 28

def get_rate(c1, c2, time)
  rate = nil

  # Try primary.
  err = nil
  begin
    rate = @primary.get_rate(c1, c2, time)
  rescue Object => e
    err = e
  end


  if rate == nil || err
    $stderr.put "Failover: primary failed for get_rate(#{c1}, #{c2}, #{time}) : #{err.inspect}"
    rate = @secondary.get_rate(c1, c2, time)
  end


  unless rate
    raise("Failover: secondary failed for get_rate(#{c1}, #{c2}, #{time})")
  end

  rate
end

#nameObject



16
17
18
# File 'lib/currency/exchange/rate/source/failover.rb', line 16

def name
  "failover(#{primary.name}, #{secondary.name})"
end