Class: Money::Bank::ECB

Inherits:
VariableExchange
  • Object
show all
Defined in:
lib/money/bank/ecb.rb,
lib/money/bank/ecb/cache.rb,
lib/money/bank/ecb/cache_file.rb,
lib/money/bank/ecb/simple_cache.rb

Overview

This class represents the European Central Bank or more precisely, its foreign exchange rates.

Defined Under Namespace

Modules: Cache Classes: CacheFile, InvalidCacheError, SimpleCache

Constant Summary collapse

RATES_URL =
'http://www.ecb.europa.eu/stats/eurofxref/eurofxref.zip'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache = nil) {|x| ... } ⇒ Money::Bank::ECB

Creates an Money::Bank::ECB.

Examples:

Money::Bank::ECB.new {|x| x.floor}
#=> #<Money::Bank::ECB
      @rounding_method=#<Proc>,
      @auto_update=true,
      @rates=#<Hash>,
      @rates_date=#<Time>,
      @currencies=#<Array>>

Yields:

  • (x)

    Optional block to use for rounding after exchanging.

Yield Parameters:

  • x (Float)

    The exchanged amount of cents to be rounded.

Yield Returns:

  • (Integer)


58
59
60
61
62
63
# File 'lib/money/bank/ecb.rb', line 58

def initialize(cache = nil, &rounding_method)
  @cache = self.class.build(cache)
  super(&rounding_method)

  setup
end

Instance Attribute Details

#auto_updatetrue, false

Auto-updating on or off.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/money/bank/ecb.rb', line 34

class ECB < Money::Bank::VariableExchange
  # Error thrown when the cache file to be loaded isn't "good enough".
  class InvalidCacheError < StandardError; end

  RATES_URL = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref.zip'

  # Creates an Money::Bank::ECB.
  #
  # @param [String] cache_filename Absolute path and filename to the cache.
  #
  # @yield [x] Optional block to use for rounding after exchanging.
  # @yieldparam [Float] x The exchanged amount of cents to be rounded.
  # @yieldreturn [Integer]
  #
  # @return [Money::Bank::ECB]
  #
  # @example
  #   Money::Bank::ECB.new {|x| x.floor}
  #   #=> #<Money::Bank::ECB
  #         @rounding_method=#<Proc>,
  #         @auto_update=true,
  #         @rates=#<Hash>,
  #         @rates_date=#<Time>,
  #         @currencies=#<Array>>
  def initialize(cache = nil, &rounding_method)
    @cache = self.class.build(cache)
    super(&rounding_method)

    setup
  end

  # Builds a Cache from input.
  #
  # @param [*] something The thing to build a Cache from.
  # @return [Cache]
  def self.build(cache)
    case cache
    when Money::Bank::ECB::Cache
      cache
    when String
      Money::Bank::ECB::CacheFile.new(cache)
    else
      Money::Bank::ECB::SimpleCache.new
    end
  end

  attr_reader :cache
  attr_accessor :auto_update

  # Setup rates hash, mutex for rates locking, and auto_update default.
  #
  # @return [self]
  def setup
    @auto_update = true
    super
    reload rescue update
  end

  # Exchange some money
  #
  # @param [Money] from To exchange from.
  # @param [String,Symbol] to Currency to exchange to.
  # @yield [x] Optional block to use for rounding after exchanging.
  # @yieldparam [Float] x The exchanged amount of cents to be rounded.
  # @yieldreturn [Integer]
  #
  # @return [Money]
  #
  # @example
  #   ecb.exchange_with(Money.new(100, :EUR), :USD)
  def exchange_with(from, to, &rounding_method)
    update if @auto_update and Time.now.utc > (@rates_date + 60*60*24)
    super(from, to, &rounding_method)
  end

  # Update the cache file and load the new rates.
  #
  # This is only relevant if #auto_update is false.
  #
  # @return [self]
  def update
    update_cache
    reload

    self
  end

  # Update the cache.
  #
  # @return [self]
  def update_cache
    Zip::InputStream.open(open(RATES_URL)) do |io|
      io.get_next_entry
      @cache.set(io.read)
    end

    self
  end

  attr_reader :currencies
  attr_reader :rates_date

  # Load rates from the cache file.
  #
  # Be "loose" to accommodate for future changes in list of currencies etc.
  #
  # @raise [InvalidCacheError] if cache is bogus.
  # @return [self]
  def reload
    time, quotations = cache_content

    @mutex.synchronize do
      @rates_date = time
      @currencies = quotations.map{|key, _| key}

      quotations.each do |currency, rate|
        set_rate('EUR', currency, rate, :without_mutex => true)
        set_rate(currency, 'EUR', 1/rate, :without_mutex => true)
      end

      quotations.product(quotations) do |one, another|
        one_cur, one_rate = one
        another_cur, another_rate = another
        next if one_cur == another_cur

        set_rate(one_cur, another_cur, another_rate/one_rate, :without_mutex => true)
      end
    end

    self
  end

  private

  # Read the cache and understand it.
  #
  # @return [[Time, Hash]]
  def cache_content
    csv = CSV.parse(@cache.get, :headers => true)
    csv = csv.first # Only one line.

    # Clean-up
    csv = csv.map{|key, value| [key.strip, value.strip]}
             .delete_if{|key, _| key.empty?}

    date_entry, *quotations = *csv
    _, date = date_entry

    time = Time.parse(date + ' ' + self.class.new_rates_time_of_day_s(date))
    quotations.map!{|cur,rate| [cur, BigDecimal.new(rate)]}

    [time, quotations]
  rescue
    raise InvalidCacheError
  end

  # Time of day (as a string ready for Time.parse) at which ECB publishes
  # new rates.
  #
  # @return [String] Either '13:00:00 UTC' or '14:00:00 UTC' depending on
  #   DST.
  def self.new_rates_time_of_day_s(date)
    # FIXME: It should be off by one day!

    # 15:00 ECB local time = 14:00 UTC when CET, 13:00 UTC when CEST.
    #
    # CEST:
    # - Starts in the morning of the last Sunday in March.
    # - Ends in the morning of the last Sunday in October.
    #
    # March  ...25  26  27  28  29  30  31
    #
    #          [Su] Mo  Tu  We  Th  Fr  Sa
    #           Sa [Su] Mo  Tu  We  Th  Fr
    #           Fr  Sa [Su] Mo  Tu  We  Th
    #           Th  Fr  Sa [Su] Mo  Tu  We
    #           We  Th  Fr  Sa [Su] Mo  Tu
    #           Tu  We  Th  Fr  Sa [Su] Mo
    #           Mo  Tu  We  Th  Fr  Sa [Su]
    #
    # October...25  26  27  28  29  30  31
    #
    # Where are we relative to the Sunday-diagonal?

    time = Time.parse(date)

    on = time.day >= 25 && time.sunday?
    above =
      (time.day == 26 && [1].include?(time.wday)) ||
      (time.day == 27 && [2,1].include?(time.wday)) ||
      (time.day == 28 && [3,2,1].include?(time.wday)) ||
      (time.day == 29 && [4,3,2,1].include?(time.wday)) ||
      (time.day == 30 && [5,4,3,2,1].include?(time.wday)) ||
      (time.day == 31 && [6,5,4,3,2,1].include?(time.wday))
    below =
      (time.day == 25 && [6,5,4,3,2,1].include?(time.wday)) ||
      (time.day == 26 && [6,5,4,3,2].include?(time.wday)) ||
      (time.day == 27 && [6,5,4,3].include?(time.wday)) ||
      (time.day == 28 && [6,5,4].include?(time.wday)) ||
      (time.day == 29 && [6,5].include?(time.wday)) ||
      (time.day == 30 && [6].include?(time.wday))

    cest = (4..9).include?(time.month) ||
      (time.month ==  3 && (on || above)) ||
      (time.month == 10 && (on || below))

    cest ? '13:00:00 UTC' : '14:00:00 UTC'
  end
end

#cacheObject (readonly)

The cache path and file name @return



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/money/bank/ecb.rb', line 34

class ECB < Money::Bank::VariableExchange
  # Error thrown when the cache file to be loaded isn't "good enough".
  class InvalidCacheError < StandardError; end

  RATES_URL = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref.zip'

  # Creates an Money::Bank::ECB.
  #
  # @param [String] cache_filename Absolute path and filename to the cache.
  #
  # @yield [x] Optional block to use for rounding after exchanging.
  # @yieldparam [Float] x The exchanged amount of cents to be rounded.
  # @yieldreturn [Integer]
  #
  # @return [Money::Bank::ECB]
  #
  # @example
  #   Money::Bank::ECB.new {|x| x.floor}
  #   #=> #<Money::Bank::ECB
  #         @rounding_method=#<Proc>,
  #         @auto_update=true,
  #         @rates=#<Hash>,
  #         @rates_date=#<Time>,
  #         @currencies=#<Array>>
  def initialize(cache = nil, &rounding_method)
    @cache = self.class.build(cache)
    super(&rounding_method)

    setup
  end

  # Builds a Cache from input.
  #
  # @param [*] something The thing to build a Cache from.
  # @return [Cache]
  def self.build(cache)
    case cache
    when Money::Bank::ECB::Cache
      cache
    when String
      Money::Bank::ECB::CacheFile.new(cache)
    else
      Money::Bank::ECB::SimpleCache.new
    end
  end

  attr_reader :cache
  attr_accessor :auto_update

  # Setup rates hash, mutex for rates locking, and auto_update default.
  #
  # @return [self]
  def setup
    @auto_update = true
    super
    reload rescue update
  end

  # Exchange some money
  #
  # @param [Money] from To exchange from.
  # @param [String,Symbol] to Currency to exchange to.
  # @yield [x] Optional block to use for rounding after exchanging.
  # @yieldparam [Float] x The exchanged amount of cents to be rounded.
  # @yieldreturn [Integer]
  #
  # @return [Money]
  #
  # @example
  #   ecb.exchange_with(Money.new(100, :EUR), :USD)
  def exchange_with(from, to, &rounding_method)
    update if @auto_update and Time.now.utc > (@rates_date + 60*60*24)
    super(from, to, &rounding_method)
  end

  # Update the cache file and load the new rates.
  #
  # This is only relevant if #auto_update is false.
  #
  # @return [self]
  def update
    update_cache
    reload

    self
  end

  # Update the cache.
  #
  # @return [self]
  def update_cache
    Zip::InputStream.open(open(RATES_URL)) do |io|
      io.get_next_entry
      @cache.set(io.read)
    end

    self
  end

  attr_reader :currencies
  attr_reader :rates_date

  # Load rates from the cache file.
  #
  # Be "loose" to accommodate for future changes in list of currencies etc.
  #
  # @raise [InvalidCacheError] if cache is bogus.
  # @return [self]
  def reload
    time, quotations = cache_content

    @mutex.synchronize do
      @rates_date = time
      @currencies = quotations.map{|key, _| key}

      quotations.each do |currency, rate|
        set_rate('EUR', currency, rate, :without_mutex => true)
        set_rate(currency, 'EUR', 1/rate, :without_mutex => true)
      end

      quotations.product(quotations) do |one, another|
        one_cur, one_rate = one
        another_cur, another_rate = another
        next if one_cur == another_cur

        set_rate(one_cur, another_cur, another_rate/one_rate, :without_mutex => true)
      end
    end

    self
  end

  private

  # Read the cache and understand it.
  #
  # @return [[Time, Hash]]
  def cache_content
    csv = CSV.parse(@cache.get, :headers => true)
    csv = csv.first # Only one line.

    # Clean-up
    csv = csv.map{|key, value| [key.strip, value.strip]}
             .delete_if{|key, _| key.empty?}

    date_entry, *quotations = *csv
    _, date = date_entry

    time = Time.parse(date + ' ' + self.class.new_rates_time_of_day_s(date))
    quotations.map!{|cur,rate| [cur, BigDecimal.new(rate)]}

    [time, quotations]
  rescue
    raise InvalidCacheError
  end

  # Time of day (as a string ready for Time.parse) at which ECB publishes
  # new rates.
  #
  # @return [String] Either '13:00:00 UTC' or '14:00:00 UTC' depending on
  #   DST.
  def self.new_rates_time_of_day_s(date)
    # FIXME: It should be off by one day!

    # 15:00 ECB local time = 14:00 UTC when CET, 13:00 UTC when CEST.
    #
    # CEST:
    # - Starts in the morning of the last Sunday in March.
    # - Ends in the morning of the last Sunday in October.
    #
    # March  ...25  26  27  28  29  30  31
    #
    #          [Su] Mo  Tu  We  Th  Fr  Sa
    #           Sa [Su] Mo  Tu  We  Th  Fr
    #           Fr  Sa [Su] Mo  Tu  We  Th
    #           Th  Fr  Sa [Su] Mo  Tu  We
    #           We  Th  Fr  Sa [Su] Mo  Tu
    #           Tu  We  Th  Fr  Sa [Su] Mo
    #           Mo  Tu  We  Th  Fr  Sa [Su]
    #
    # October...25  26  27  28  29  30  31
    #
    # Where are we relative to the Sunday-diagonal?

    time = Time.parse(date)

    on = time.day >= 25 && time.sunday?
    above =
      (time.day == 26 && [1].include?(time.wday)) ||
      (time.day == 27 && [2,1].include?(time.wday)) ||
      (time.day == 28 && [3,2,1].include?(time.wday)) ||
      (time.day == 29 && [4,3,2,1].include?(time.wday)) ||
      (time.day == 30 && [5,4,3,2,1].include?(time.wday)) ||
      (time.day == 31 && [6,5,4,3,2,1].include?(time.wday))
    below =
      (time.day == 25 && [6,5,4,3,2,1].include?(time.wday)) ||
      (time.day == 26 && [6,5,4,3,2].include?(time.wday)) ||
      (time.day == 27 && [6,5,4,3].include?(time.wday)) ||
      (time.day == 28 && [6,5,4].include?(time.wday)) ||
      (time.day == 29 && [6,5].include?(time.wday)) ||
      (time.day == 30 && [6].include?(time.wday))

    cest = (4..9).include?(time.month) ||
      (time.month ==  3 && (on || above)) ||
      (time.month == 10 && (on || below))

    cest ? '13:00:00 UTC' : '14:00:00 UTC'
  end
end

#currenciesArray<String> (readonly)

The available currencies to exchange between.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/money/bank/ecb.rb', line 34

class ECB < Money::Bank::VariableExchange
  # Error thrown when the cache file to be loaded isn't "good enough".
  class InvalidCacheError < StandardError; end

  RATES_URL = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref.zip'

  # Creates an Money::Bank::ECB.
  #
  # @param [String] cache_filename Absolute path and filename to the cache.
  #
  # @yield [x] Optional block to use for rounding after exchanging.
  # @yieldparam [Float] x The exchanged amount of cents to be rounded.
  # @yieldreturn [Integer]
  #
  # @return [Money::Bank::ECB]
  #
  # @example
  #   Money::Bank::ECB.new {|x| x.floor}
  #   #=> #<Money::Bank::ECB
  #         @rounding_method=#<Proc>,
  #         @auto_update=true,
  #         @rates=#<Hash>,
  #         @rates_date=#<Time>,
  #         @currencies=#<Array>>
  def initialize(cache = nil, &rounding_method)
    @cache = self.class.build(cache)
    super(&rounding_method)

    setup
  end

  # Builds a Cache from input.
  #
  # @param [*] something The thing to build a Cache from.
  # @return [Cache]
  def self.build(cache)
    case cache
    when Money::Bank::ECB::Cache
      cache
    when String
      Money::Bank::ECB::CacheFile.new(cache)
    else
      Money::Bank::ECB::SimpleCache.new
    end
  end

  attr_reader :cache
  attr_accessor :auto_update

  # Setup rates hash, mutex for rates locking, and auto_update default.
  #
  # @return [self]
  def setup
    @auto_update = true
    super
    reload rescue update
  end

  # Exchange some money
  #
  # @param [Money] from To exchange from.
  # @param [String,Symbol] to Currency to exchange to.
  # @yield [x] Optional block to use for rounding after exchanging.
  # @yieldparam [Float] x The exchanged amount of cents to be rounded.
  # @yieldreturn [Integer]
  #
  # @return [Money]
  #
  # @example
  #   ecb.exchange_with(Money.new(100, :EUR), :USD)
  def exchange_with(from, to, &rounding_method)
    update if @auto_update and Time.now.utc > (@rates_date + 60*60*24)
    super(from, to, &rounding_method)
  end

  # Update the cache file and load the new rates.
  #
  # This is only relevant if #auto_update is false.
  #
  # @return [self]
  def update
    update_cache
    reload

    self
  end

  # Update the cache.
  #
  # @return [self]
  def update_cache
    Zip::InputStream.open(open(RATES_URL)) do |io|
      io.get_next_entry
      @cache.set(io.read)
    end

    self
  end

  attr_reader :currencies
  attr_reader :rates_date

  # Load rates from the cache file.
  #
  # Be "loose" to accommodate for future changes in list of currencies etc.
  #
  # @raise [InvalidCacheError] if cache is bogus.
  # @return [self]
  def reload
    time, quotations = cache_content

    @mutex.synchronize do
      @rates_date = time
      @currencies = quotations.map{|key, _| key}

      quotations.each do |currency, rate|
        set_rate('EUR', currency, rate, :without_mutex => true)
        set_rate(currency, 'EUR', 1/rate, :without_mutex => true)
      end

      quotations.product(quotations) do |one, another|
        one_cur, one_rate = one
        another_cur, another_rate = another
        next if one_cur == another_cur

        set_rate(one_cur, another_cur, another_rate/one_rate, :without_mutex => true)
      end
    end

    self
  end

  private

  # Read the cache and understand it.
  #
  # @return [[Time, Hash]]
  def cache_content
    csv = CSV.parse(@cache.get, :headers => true)
    csv = csv.first # Only one line.

    # Clean-up
    csv = csv.map{|key, value| [key.strip, value.strip]}
             .delete_if{|key, _| key.empty?}

    date_entry, *quotations = *csv
    _, date = date_entry

    time = Time.parse(date + ' ' + self.class.new_rates_time_of_day_s(date))
    quotations.map!{|cur,rate| [cur, BigDecimal.new(rate)]}

    [time, quotations]
  rescue
    raise InvalidCacheError
  end

  # Time of day (as a string ready for Time.parse) at which ECB publishes
  # new rates.
  #
  # @return [String] Either '13:00:00 UTC' or '14:00:00 UTC' depending on
  #   DST.
  def self.new_rates_time_of_day_s(date)
    # FIXME: It should be off by one day!

    # 15:00 ECB local time = 14:00 UTC when CET, 13:00 UTC when CEST.
    #
    # CEST:
    # - Starts in the morning of the last Sunday in March.
    # - Ends in the morning of the last Sunday in October.
    #
    # March  ...25  26  27  28  29  30  31
    #
    #          [Su] Mo  Tu  We  Th  Fr  Sa
    #           Sa [Su] Mo  Tu  We  Th  Fr
    #           Fr  Sa [Su] Mo  Tu  We  Th
    #           Th  Fr  Sa [Su] Mo  Tu  We
    #           We  Th  Fr  Sa [Su] Mo  Tu
    #           Tu  We  Th  Fr  Sa [Su] Mo
    #           Mo  Tu  We  Th  Fr  Sa [Su]
    #
    # October...25  26  27  28  29  30  31
    #
    # Where are we relative to the Sunday-diagonal?

    time = Time.parse(date)

    on = time.day >= 25 && time.sunday?
    above =
      (time.day == 26 && [1].include?(time.wday)) ||
      (time.day == 27 && [2,1].include?(time.wday)) ||
      (time.day == 28 && [3,2,1].include?(time.wday)) ||
      (time.day == 29 && [4,3,2,1].include?(time.wday)) ||
      (time.day == 30 && [5,4,3,2,1].include?(time.wday)) ||
      (time.day == 31 && [6,5,4,3,2,1].include?(time.wday))
    below =
      (time.day == 25 && [6,5,4,3,2,1].include?(time.wday)) ||
      (time.day == 26 && [6,5,4,3,2].include?(time.wday)) ||
      (time.day == 27 && [6,5,4,3].include?(time.wday)) ||
      (time.day == 28 && [6,5,4].include?(time.wday)) ||
      (time.day == 29 && [6,5].include?(time.wday)) ||
      (time.day == 30 && [6].include?(time.wday))

    cest = (4..9).include?(time.month) ||
      (time.month ==  3 && (on || above)) ||
      (time.month == 10 && (on || below))

    cest ? '13:00:00 UTC' : '14:00:00 UTC'
  end
end

#rates_dateObject (readonly)

The time on which ECB published the rates that is currently in use. @return



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/money/bank/ecb.rb', line 34

class ECB < Money::Bank::VariableExchange
  # Error thrown when the cache file to be loaded isn't "good enough".
  class InvalidCacheError < StandardError; end

  RATES_URL = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref.zip'

  # Creates an Money::Bank::ECB.
  #
  # @param [String] cache_filename Absolute path and filename to the cache.
  #
  # @yield [x] Optional block to use for rounding after exchanging.
  # @yieldparam [Float] x The exchanged amount of cents to be rounded.
  # @yieldreturn [Integer]
  #
  # @return [Money::Bank::ECB]
  #
  # @example
  #   Money::Bank::ECB.new {|x| x.floor}
  #   #=> #<Money::Bank::ECB
  #         @rounding_method=#<Proc>,
  #         @auto_update=true,
  #         @rates=#<Hash>,
  #         @rates_date=#<Time>,
  #         @currencies=#<Array>>
  def initialize(cache = nil, &rounding_method)
    @cache = self.class.build(cache)
    super(&rounding_method)

    setup
  end

  # Builds a Cache from input.
  #
  # @param [*] something The thing to build a Cache from.
  # @return [Cache]
  def self.build(cache)
    case cache
    when Money::Bank::ECB::Cache
      cache
    when String
      Money::Bank::ECB::CacheFile.new(cache)
    else
      Money::Bank::ECB::SimpleCache.new
    end
  end

  attr_reader :cache
  attr_accessor :auto_update

  # Setup rates hash, mutex for rates locking, and auto_update default.
  #
  # @return [self]
  def setup
    @auto_update = true
    super
    reload rescue update
  end

  # Exchange some money
  #
  # @param [Money] from To exchange from.
  # @param [String,Symbol] to Currency to exchange to.
  # @yield [x] Optional block to use for rounding after exchanging.
  # @yieldparam [Float] x The exchanged amount of cents to be rounded.
  # @yieldreturn [Integer]
  #
  # @return [Money]
  #
  # @example
  #   ecb.exchange_with(Money.new(100, :EUR), :USD)
  def exchange_with(from, to, &rounding_method)
    update if @auto_update and Time.now.utc > (@rates_date + 60*60*24)
    super(from, to, &rounding_method)
  end

  # Update the cache file and load the new rates.
  #
  # This is only relevant if #auto_update is false.
  #
  # @return [self]
  def update
    update_cache
    reload

    self
  end

  # Update the cache.
  #
  # @return [self]
  def update_cache
    Zip::InputStream.open(open(RATES_URL)) do |io|
      io.get_next_entry
      @cache.set(io.read)
    end

    self
  end

  attr_reader :currencies
  attr_reader :rates_date

  # Load rates from the cache file.
  #
  # Be "loose" to accommodate for future changes in list of currencies etc.
  #
  # @raise [InvalidCacheError] if cache is bogus.
  # @return [self]
  def reload
    time, quotations = cache_content

    @mutex.synchronize do
      @rates_date = time
      @currencies = quotations.map{|key, _| key}

      quotations.each do |currency, rate|
        set_rate('EUR', currency, rate, :without_mutex => true)
        set_rate(currency, 'EUR', 1/rate, :without_mutex => true)
      end

      quotations.product(quotations) do |one, another|
        one_cur, one_rate = one
        another_cur, another_rate = another
        next if one_cur == another_cur

        set_rate(one_cur, another_cur, another_rate/one_rate, :without_mutex => true)
      end
    end

    self
  end

  private

  # Read the cache and understand it.
  #
  # @return [[Time, Hash]]
  def cache_content
    csv = CSV.parse(@cache.get, :headers => true)
    csv = csv.first # Only one line.

    # Clean-up
    csv = csv.map{|key, value| [key.strip, value.strip]}
             .delete_if{|key, _| key.empty?}

    date_entry, *quotations = *csv
    _, date = date_entry

    time = Time.parse(date + ' ' + self.class.new_rates_time_of_day_s(date))
    quotations.map!{|cur,rate| [cur, BigDecimal.new(rate)]}

    [time, quotations]
  rescue
    raise InvalidCacheError
  end

  # Time of day (as a string ready for Time.parse) at which ECB publishes
  # new rates.
  #
  # @return [String] Either '13:00:00 UTC' or '14:00:00 UTC' depending on
  #   DST.
  def self.new_rates_time_of_day_s(date)
    # FIXME: It should be off by one day!

    # 15:00 ECB local time = 14:00 UTC when CET, 13:00 UTC when CEST.
    #
    # CEST:
    # - Starts in the morning of the last Sunday in March.
    # - Ends in the morning of the last Sunday in October.
    #
    # March  ...25  26  27  28  29  30  31
    #
    #          [Su] Mo  Tu  We  Th  Fr  Sa
    #           Sa [Su] Mo  Tu  We  Th  Fr
    #           Fr  Sa [Su] Mo  Tu  We  Th
    #           Th  Fr  Sa [Su] Mo  Tu  We
    #           We  Th  Fr  Sa [Su] Mo  Tu
    #           Tu  We  Th  Fr  Sa [Su] Mo
    #           Mo  Tu  We  Th  Fr  Sa [Su]
    #
    # October...25  26  27  28  29  30  31
    #
    # Where are we relative to the Sunday-diagonal?

    time = Time.parse(date)

    on = time.day >= 25 && time.sunday?
    above =
      (time.day == 26 && [1].include?(time.wday)) ||
      (time.day == 27 && [2,1].include?(time.wday)) ||
      (time.day == 28 && [3,2,1].include?(time.wday)) ||
      (time.day == 29 && [4,3,2,1].include?(time.wday)) ||
      (time.day == 30 && [5,4,3,2,1].include?(time.wday)) ||
      (time.day == 31 && [6,5,4,3,2,1].include?(time.wday))
    below =
      (time.day == 25 && [6,5,4,3,2,1].include?(time.wday)) ||
      (time.day == 26 && [6,5,4,3,2].include?(time.wday)) ||
      (time.day == 27 && [6,5,4,3].include?(time.wday)) ||
      (time.day == 28 && [6,5,4].include?(time.wday)) ||
      (time.day == 29 && [6,5].include?(time.wday)) ||
      (time.day == 30 && [6].include?(time.wday))

    cest = (4..9).include?(time.month) ||
      (time.month ==  3 && (on || above)) ||
      (time.month == 10 && (on || below))

    cest ? '13:00:00 UTC' : '14:00:00 UTC'
  end
end

Class Method Details

.build(cache) ⇒ Cache

Builds a Cache from input.



69
70
71
72
73
74
75
76
77
78
# File 'lib/money/bank/ecb.rb', line 69

def self.build(cache)
  case cache
  when Money::Bank::ECB::Cache
    cache
  when String
    Money::Bank::ECB::CacheFile.new(cache)
  else
    Money::Bank::ECB::SimpleCache.new
  end
end

Instance Method Details

#exchange_with(from, to) {|x| ... } ⇒ Money

Exchange some money

Examples:

ecb.exchange_with(Money.new(100, :EUR), :USD)

Yields:

  • (x)

    Optional block to use for rounding after exchanging.

Yield Parameters:

  • x (Float)

    The exchanged amount of cents to be rounded.

Yield Returns:

  • (Integer)


104
105
106
107
# File 'lib/money/bank/ecb.rb', line 104

def exchange_with(from, to, &rounding_method)
  update if @auto_update and Time.now.utc > (@rates_date + 60*60*24)
  super(from, to, &rounding_method)
end

#reloadself

Load rates from the cache file.

Be “loose” to accommodate for future changes in list of currencies etc.

Raises:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/money/bank/ecb.rb', line 142

def reload
  time, quotations = cache_content

  @mutex.synchronize do
    @rates_date = time
    @currencies = quotations.map{|key, _| key}

    quotations.each do |currency, rate|
      set_rate('EUR', currency, rate, :without_mutex => true)
      set_rate(currency, 'EUR', 1/rate, :without_mutex => true)
    end

    quotations.product(quotations) do |one, another|
      one_cur, one_rate = one
      another_cur, another_rate = another
      next if one_cur == another_cur

      set_rate(one_cur, another_cur, another_rate/one_rate, :without_mutex => true)
    end
  end

  self
end

#setupself

Setup rates hash, mutex for rates locking, and auto_update default.



86
87
88
89
90
# File 'lib/money/bank/ecb.rb', line 86

def setup
  @auto_update = true
  super
  reload rescue update
end

#updateself

Update the cache file and load the new rates.

This is only relevant if #auto_update is false.



114
115
116
117
118
119
# File 'lib/money/bank/ecb.rb', line 114

def update
  update_cache
  reload

  self
end

#update_cacheself

Update the cache.



124
125
126
127
128
129
130
131
# File 'lib/money/bank/ecb.rb', line 124

def update_cache
  Zip::InputStream.open(open(RATES_URL)) do |io|
    io.get_next_entry
    @cache.set(io.read)
  end

  self
end