Skip to content

Commit

Permalink
first release
Browse files Browse the repository at this point in the history
  • Loading branch information
vlmonk committed Oct 21, 2009
1 parent 3f5c9c6 commit 4b370c7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.*.sw?
Binary file not shown.
Binary file removed lib/.exchange_rates.rb.swp
Binary file not shown.
62 changes: 62 additions & 0 deletions lib/exchange_rates.rb
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

0 comments on commit 4b370c7

Please sign in to comment.