diff --git a/README.md b/README.md index 53b806f..2cbc905 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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' @@ -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 diff --git a/lib/money/bank/tcmb_currency.rb b/lib/money/bank/tcmb_currency.rb index 3713765..0340ecc 100644 --- a/lib/money/bank/tcmb_currency.rb +++ b/lib/money/bank/tcmb_currency.rb @@ -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{ @@ -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}'" @@ -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) @@ -98,3 +83,4 @@ def rate_key_for(from, to, date) end end end + diff --git a/lib/tcmb_currency.rb b/lib/tcmb_currency.rb index ad6ffba..3083ce1 100644 --- a/lib/tcmb_currency.rb +++ b/lib/tcmb_currency.rb @@ -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 \ No newline at end of file diff --git a/lib/tcmb_currency/version.rb b/lib/tcmb_currency/version.rb index 9fe5879..9b80594 100644 --- a/lib/tcmb_currency/version.rb +++ b/lib/tcmb_currency/version.rb @@ -1,3 +1,3 @@ module TcmbCurrency - VERSION = "0.1.0" + VERSION = "0.2.0" end diff --git a/tcmb_currency.gemspec b/tcmb_currency.gemspec index 52ce7b6..26df68e 100644 --- a/tcmb_currency.gemspec +++ b/tcmb_currency.gemspec @@ -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) }