Skip to content

Commit

Permalink
Merge pull request #7 from kirs/feature/stabilnost
Browse files Browse the repository at this point in the history
Safe fetch with fallback (fixed #5)
  • Loading branch information
rmustafin authored Mar 25, 2019
2 parents 200ea84 + bdaad2a commit 563586d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ Autoupdate
# Check expiration date
bank.rates_expired_at

### Safe rates fetch

There are some cases, when the `cbr.ru` returns HTTP 302.
To avoid issues in production, you use fallback:

```ruby
bank = Money::Bank::RussianCentralBank.new
begin
bank.update_rates
rescue Money::Bank::RussianCentralBank::FetchError => e
Rails.logger.info "CBR failed: #{e.response}"

## fallback
Money.default_bank = Money::Bank::VariableExchange.new

Money.default_bank.add_rate(:usd, :eur, 1.3)
Money.default_bank.add_rate(:eur, :usd, 0.7)
end
```

## Contributing

1. Fork it
Expand Down
10 changes: 10 additions & 0 deletions lib/russian_central_bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
class Money
module Bank
class RussianCentralBank < Money::Bank::VariableExchange
class FetchError < StandardError
attr_reader :response

def initialize(message, response=nil)
super(message)
@response = response
end
end

CBR_SERVICE_URL = 'http://www.cbr.ru/DailyInfoWebServ/DailyInfo.asmx?WSDL'

Expand Down Expand Up @@ -64,6 +72,8 @@ def exchange_rates(date = Date.today)
follow_redirects: true
response = client.call(:get_curs_on_date, message: { 'On_date' => date.strftime('%Y-%m-%dT%H:%M:%S') })
response.body[:get_curs_on_date_response][:get_curs_on_date_result][:diffgram][:valute_data][:valute_curs_on_date]
rescue Wasabi::Resolver::HTTPError => e
raise FetchError.new(e.message, e.response)
end

def update_parsed_rates(rates)
Expand Down

0 comments on commit 563586d

Please sign in to comment.