Skip to content

Commit

Permalink
database operations included
Browse files Browse the repository at this point in the history
  • Loading branch information
hamitturkukaya committed Mar 5, 2013
1 parent e8c50c1 commit 5eb577d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 37 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ it based on [money gem](https://github.com/RubyMoney/money), [money-rails Gem](h

Add this line to your application's Gemfile:

gem 'money-rails'
gem 'tcmb_currency'

And then execute:
Expand All @@ -24,8 +23,10 @@ Or install it yourself as:
After the installing gems run:

$ rails g money_rails:initializer
$ rails g tcmb_currency:migration
$ rake db:migrate

then add
then add to /config/initializers/money.rb file


require 'money'
Expand All @@ -36,6 +37,9 @@ then add
# set default bank to instance of GoogleCurrency
Money.default_bank = Money::Bank::TcmbCurrency.new

at last add to daily cron tasks this rake

$ rake tcmb_currency:insert

And you can use it as

Expand Down
40 changes: 13 additions & 27 deletions lib/money/bank/tcmb_currency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,34 @@
require 'money'
require 'open-uri'
require 'multi_json'
require "active_record"

class Money

def exchange_to( to_currency, date = Time.now.to_date)
other_currency = Currency.wrap(to_currency)
bank = Bank::TcmbCurrency.new
bank.exchange_with(self,to_currency,date)
end


module Bank
class TcmbCurrency < Money::Bank::VariableExchange

class CrossRate < ActiveRecord::Base
attr_accessible :code, :rate, :date
end
SERVICE_HOST = "whispering-springs-5160.herokuapp.com"
SERVICE_PATH = "/currencies"

attr_reader :rates


def flush_rates
@mutex.synchronize{
@rates = {}
}
end


def flush_rate(from, to, date)
key = rate_key_for(from, to)
@mutex.synchronize{
Expand All @@ -35,7 +40,7 @@ def flush_rate(from, to, date)
def exchange_with(*args)
from , to_currency , date = args[0], args[1], args[2]
return from if same_currency?(from.currency, to_currency)

rate = get_rate(from.currency, to_currency, date)
unless rate
raise UnknownRate, "No conversion rate known for '#{from.currency.iso_code}' -> '#{to_currency}'"
Expand Down Expand Up @@ -64,32 +69,12 @@ def get_rate(from, to, date)

private


def fetch_rate(from, to, date)
from, to = Currency.wrap(from), Currency.wrap(to)

data = build_uri(from, to, date).read
data = fix_response_json_data(data)

return data['amount']
end

def build_uri(from, to, date)
uri = URI::HTTP.build(
:host => SERVICE_HOST,
:path => SERVICE_PATH,
:query => "cash=1&from=#{from.iso_code}&to=#{to.iso_code}&date=#{date}"
)
end

def fix_response_json_data(data)
data.gsub!(/from:/, '"from":')
data.gsub!(/from_name:/, '"from_name":')
data.gsub!(/to:/, '"to":')
data.gsub!(/to_name:/, '"to_name":')
data.gsub!(/amount:/, '"amount":')
data.gsub!(Regexp.new("(\\\\x..|\\\\240)"), '')

MultiJson.decode(data)
f = CrossRate.where(code: from.to_s, date: date).last
t = CrossRate.where(code: to.to_s, date: date).last
return rate = t.rate.to_f/f.rate.to_f
end

def rate_key_for(from, to, date)
Expand All @@ -98,3 +83,4 @@ def rate_key_for(from, to, date)
end
end
end

5 changes: 3 additions & 2 deletions lib/tcmb_currency.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "tcmb_currency/version"
require "active_support/dependencies"

module TcmbCurrency
# Your code goes here...
end
require 'tcmb_currency/railtie' if defined?(Rails)
end
2 changes: 1 addition & 1 deletion lib/tcmb_currency/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module TcmbCurrency
VERSION = "0.1.0"
VERSION = "0.2.0"
end
12 changes: 7 additions & 5 deletions tcmb_currency.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ Gem::Specification.new do |gem|
gem.email = ["htkaya90@gmail.com"]
gem.description = %q{Tcmb gem}
gem.summary = %q{Access the TCMB exchange rate data.}
gem.homepage = ""
gem.homepage = "https://github.com/lab2023/tcmb_currency"
gem.required_rubygems_version = ">= 1.3.6"
gem.add_development_dependency "yard", ">= 0.5.8"
gem.add_development_dependency "json", ">= 1.4.0"
gem.add_development_dependency "yajl-ruby", ">= 1.0.0"
gem.add_development_dependency "ffi"
gem.add_dependency "yard", ">= 0.5.8"
gem.add_dependency "json", ">= 1.4.0"
gem.add_dependency "yajl-ruby", ">= 1.0.0"
gem.add_dependency "ffi"
gem.add_dependency "money"
gem.add_dependency "money-rails"
gem.files = `git ls-files`.split($/)
gem.files = Dir.glob("{lib}/**/*")
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
Expand Down

0 comments on commit 5eb577d

Please sign in to comment.