-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.*.sw? |
Binary file removed
BIN
-12 KB
generators/exchange_rates_migration/.exchange_rates_migration_generator.rb.swp
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,64 @@ | ||
require 'net/http' | ||
require 'uri' | ||
require 'hpricot' | ||
|
||
class ExchangeRates < ActiveRecord::Base | ||
serialize :fields | ||
|
||
class << self | ||
def get | ||
Record.new first | ||
end | ||
|
||
def update | ||
row = first || new | ||
updater = Updater.new | ||
row.date = updater.date | ||
row.fields = updater.fields | ||
row.save | ||
end | ||
end | ||
|
||
class Record | ||
def initialize(record) | ||
@record = record | ||
end | ||
|
||
def date | ||
@record.date | ||
end | ||
|
||
def value(key) | ||
@record.fields[key][:value] | ||
end | ||
end | ||
|
||
class Updater | ||
URL = 'http://www.cbr.ru/scripts/XML_daily.asp' | ||
attr_reader :date, :fields | ||
|
||
def initialize | ||
@date = nil | ||
@fields = {} | ||
load_data | ||
end | ||
|
||
private | ||
|
||
def load_data | ||
url = URI.parse URL | ||
respond = Net::HTTP.get url | ||
doc = Hpricot::XML(respond) | ||
parse_data(doc) | ||
end | ||
|
||
def parse_data(doc) | ||
@date = Date.parse doc.at(:ValCurs)[:Date] | ||
(doc/'ValCurs/Valute').each do |valute| | ||
code = (valute/:CharCode).html.to_sym | ||
@fields[code] = {} | ||
@fields[code][:value] = (valute/:Value).html.gsub(',','.').to_f | ||
end | ||
end | ||
end | ||
end |