Skip to content

Commit 2b4b27b

Browse files
author
Hamdi Bayhan
committed
Merged in feature/update_version (pull request #1)
KBP-166 #time 5h - update code and version for rails 4 to version 0.5 Approved-by: İsmail Akbudak <ismail.akbudak@lab2023.com> Approved-by: Fatih Avsan <fatih.avsan@lab2023.com>
2 parents 6f000f9 + af9d290 commit 2b4b27b

16 files changed

+161
-121
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ test/version_tmp
1717
tmp
1818
.idea
1919
.idea/**/*
20-
atlassian-ide-plugin.xml
20+
atlassian-ide-plugin.xml
21+
.DS_Store

.rubocop.yml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
AllCops:
2+
Include:
3+
- 'Rakefile'
4+
- 'config.ru'
5+
- 'Gemfile'
6+
Exclude:
7+
- Guardfile
8+
- bin/**
9+
- db/**/**
10+
- scripts/**
11+
- spec/**/**/**/**
12+
- test/**/**/**/**
13+
- tmp/**/**/**/**
14+
- lib/modules/**/**
15+
Documentation:
16+
Enabled: false
17+
18+
Metrics/LineLength:
19+
Max: 100
20+
Exclude:
21+
- 'Gemfile'
22+
23+
Metrics/MethodLength:
24+
CountComments: false
25+
Max: 20
26+
27+
Metrics/AbcSize:
28+
Max: 40
29+
30+
Metrics/CyclomaticComplexity:
31+
Max: 10
32+
33+
Metrics/PerceivedComplexity:
34+
Max: 10
35+
36+
Metrics/BlockLength:
37+
CountComments: false
38+
Max: 40
39+
Exclude:
40+
- 'Rakefile'
41+
- '**/*.rake'
42+
- 'spec/**/*.rb'
43+
44+
Style/FrozenStringLiteralComment:
45+
EnforcedStyle: when_needed

Gemfile

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ source 'https://rubygems.org'
33
# Specify your gem's dependencies in tcmb_currency.gemspec
44
gemspec
55

6-
gem 'yard', ">= 0.5.8"
7-
gem 'json', ">= 1.4.0"
8-
gem 'yajl-ruby', ">= 1.0.0"
96
gem 'ffi'
10-
gem 'money'
11-
gem 'money-rails'
7+
gem 'json', '>= 1.4.0'
8+
gem 'money', '~> 6.7.1'
9+
gem 'money-rails', '~> 1.7'
1210
gem 'nokogiri'
11+
gem 'rubocop', '~> 0.51.0'
12+
gem 'yajl-ruby', '>= 1.0.0'
13+
gem 'yard', '>= 0.5.8'
1314

1415
group :development do
1516
gem 'rake'

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@ Add this line to your application's Gemfile:
1414

1515
### For rails 4.x
1616

17-
gem 'tcmb_currency', '~> 0.4.0', :git => 'git://github.com/lab2023/tcmb_currency.git'
17+
gem 'tcmb_currency', '~> 0.5.0', :git => 'git://github.com/lab2023/tcmb_currency.git'
1818

19-
### For rails 3.x
20-
21-
gem 'tcmb_currency', '~> 0.3.0', git: 'git://github.com/lab2023/tcmb_currency.git', branch: 'rails3'
2219

2320
And then execute:
2421

@@ -52,6 +49,8 @@ And you can use it as
5249
Money.new(1000,"USD").exchange_to(:EUR)
5350
Money.new(1000,"USD").exchange_to(:EUR, "2013-03-02")
5451

52+
If there is no record for searches with history, you will get `There is no record in that date` error.
53+
5554
## Contributing
5655

5756
Once you've made your great commits:

Rakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
require "bundler/gem_tasks"
1+
require 'bundler/gem_tasks'

lib/generators/tcmb_currency/initializer/initializer_generator.rb

+6-8
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33

44
module TcmbCurrency
55
class InitializerGenerator < ::Rails::Generators::Base
6+
source_root File.join(File.dirname(__FILE__), 'templates')
67

7-
source_root File.join(File.dirname(__FILE__), 'templates')
8-
9-
desc 'Creates a sample Tcmb_Currency initializer.'
10-
11-
def copy_initializer
12-
copy_file 'money.rb', 'config/initializers/money.rb'
13-
end
8+
desc 'Creates a sample Tcmb_Currency initializer.'
149

10+
def copy_initializer
11+
copy_file 'money.rb', 'config/initializers/money.rb'
1512
end
16-
end
13+
end
14+
end
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
# frozen_string_literal: true
2+
13
# encoding : utf-8
24
require 'money'
35
require 'money/bank/tcmb_currency'
46
require 'json'
57
MultiJson.engine = :json_gem
68
Money.default_bank = Money::Bank::TcmbCurrency.new
79
CROSS_RATE = Money::Bank::TcmbCurrency::CrossRate
8-
CURRENCY_TYPE = Money::Bank::TcmbCurrency::CurrencyType
10+
CURRENCY_TYPE = Money::Bank::TcmbCurrency::CurrencyType

lib/generators/tcmb_currency/migration/migration_generator.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module TcmbCurrency
55
class MigrationGenerator < Rails::Generators::Base
66
include Rails::Generators::Migration
77

8-
namespace "tcmb_currency:migration"
8+
namespace 'tcmb_currency:migration'
99

1010
source_root File.join(File.dirname(__FILE__), 'templates')
1111

@@ -14,8 +14,10 @@ def self.next_migration_number(dirname)
1414
end
1515

1616
def create_migration_file
17-
migration_template 'typemigration.rb', 'db/migrate/create_currency_types.rb' rescue nil
18-
migration_template 'migration.rb', 'db/migrate/create_cross_rates_table.rb' rescue nil
17+
migration_template 'typemigration.rb', 'db/migrate/create_currency_types.rb'
18+
migration_template 'migration.rb', 'db/migrate/create_cross_rates_table.rb'
19+
rescue StandardError => e
20+
raise e
1921
end
2022
end
21-
end
23+
end

lib/generators/tcmb_currency/migration/templates/migration.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ def self.up
1313
def self.down
1414
drop_table :cross_rates
1515
end
16-
end
16+
end

lib/generators/tcmb_currency/migration/templates/typemigration.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ def self.up
55

66
t.timestamps
77
end
8-
9-
currencies = ['USD', 'EUR', 'GBP', 'TRY', 'CAD', 'XDR', 'DKK', 'SEK', 'CHF', 'NOK', 'JPY', 'SAR', 'KWD', 'AUD', 'RUB', 'RON', 'PKR', 'IRR', 'CNY', 'BGN']
8+
currencies = %w[ USD EUR GBP TRY CAD XDR DKK SEK CHF NOK
9+
JPY SAR KWD AUD RUB RON PKR IRR CNY BGN ]
1010
currencies.each do |c|
1111
CURRENCY_TYPE.create(currency: c.to_s)
1212
end
@@ -15,5 +15,4 @@ def self.up
1515
def self.down
1616
drop_table :currency_types
1717
end
18-
19-
end
18+
end

lib/money/bank/tcmb_currency.rb

+33-42
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,32 @@
66

77
class Money
88
def exchange_to(to_currency, date = nil)
9-
other_currency = Currency.wrap(to_currency)
109
bank = Bank::TcmbCurrency.new
11-
bank.exchange_with(self, to_currency, date)
10+
bank.exchange_with!(self, to_currency, date)
1211
end
1312

1413
module Bank
1514
class TcmbCurrency < Money::Bank::VariableExchange
16-
1715
class CrossRate < ActiveRecord::Base
18-
1916
end
2017

2118
class CurrencyType < ActiveRecord::Base
22-
2319
end
2420

2521
attr_reader :rates
2622

27-
def flush_rates
28-
@mutex.synchronize {
29-
@rates = {}
30-
}
31-
end
32-
33-
34-
def flush_rate(from, to)
35-
key = rate_key_for(from,to)
36-
@mutex.synchronize {
37-
@rates.delete(key)
38-
}
39-
end
40-
41-
def exchange_with(*args)
42-
from, to_currency, date = args[0], args[1], args[2]
23+
def exchange_with!(*args)
24+
from = args[0]
25+
to_currency = args[1]
26+
date = args[2]
4327
return from if same_currency?(from.currency, to_currency)
44-
rate = get_rate(from.currency, to_currency, date)
28+
rate = fetch_rate!(from.currency, to_currency, date)
4529
unless rate
46-
raise UnknownRate, "No conversion rate known for '#{from.currency.iso_code}' -> '#{to_currency}'"
30+
raise UnknownRate, "No conversion rate known for
31+
'#{from.currency.iso_code}' -> '#{to_currency}'"
4732
end
48-
_to_currency_ = Currency.wrap(to_currency)
49-
fractional = BigDecimal.new(from.fractional.to_s) / (BigDecimal.new(from.currency.subunit_to_unit.to_s) / BigDecimal.new(_to_currency_.subunit_to_unit.to_s))
33+
wrap_to_currency_ = Currency.wrap(to_currency)
34+
fractional = get_fractional(from, wrap_to_currency_)
5035
ex = (fractional * BigDecimal.new(rate.to_s)).to_f
5136
ex = if block_given?
5237
yield ex
@@ -55,31 +40,37 @@ def exchange_with(*args)
5540
else
5641
ex.to_s.to_i
5742
end
58-
Money.new(ex, _to_currency_)
59-
end
60-
61-
def get_rate(from, to, date)
62-
@mutex.synchronize {
63-
@rates[rate_key_for(from, to)] ||= fetch_rate(from, to, date)
64-
}
43+
Money.new(ex, wrap_to_currency_)
6544
end
6645

6746
private
6847

69-
def fetch_rate(from, to, date)
70-
from, to = Currency.wrap(from), Currency.wrap(to)
48+
def get_fractional(from, to_currency)
49+
bd_from_fractional = BigDecimal.new(from.fractional.to_s)
50+
bd_from_currency = BigDecimal.new(from.currency.subunit_to_unit.to_s)
51+
bd_to_currency = BigDecimal.new(to_currency.subunit_to_unit.to_s)
52+
bd_from_fractional / (bd_from_currency / bd_to_currency)
53+
end
54+
55+
def fetch_rate!(from, to, date)
56+
from_to_s = Money::Currency.wrap(from).to_s
57+
to_to_s = Money::Currency.wrap(to).to_s
58+
from_rate = 1.0
59+
to_rate = 1.0
60+
froms = Money::Bank::TcmbCurrency::CrossRate.where(code: from_to_s)
61+
tos = Money::Bank::TcmbCurrency::CrossRate.where(code: to_to_s)
7162
if date.nil?
72-
f = CrossRate.where(code: from.to_s).last
73-
t = CrossRate.where(code: to.to_s).last
63+
from_rate = froms.last.rate.to_f unless from_to_s == 'USD'
64+
to_rate = tos.last.rate.to_f unless to_to_s == 'USD'
7465
else
75-
f = CrossRate.where(code: from.to_s, date: date).last
76-
t = CrossRate.where(code: to.to_s, date: date).last
66+
unless Money::Bank::TcmbCurrency::CrossRate.where(date: date).present?
67+
raise 'There is no record in that date.'
68+
end
69+
from_rate = froms.where(date: date).last.rate.to_f unless from_to_s == 'USD'
70+
to_rate = tos.where(date: date).last.rate.to_f unless to_to_s == 'USD'
7771
end
78-
79-
return rate = t.rate.to_f/f.rate.to_f
72+
to_rate / from_rate
8073
end
81-
8274
end
8375
end
8476
end
85-

lib/tasks/tcmb_currency.rake

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
namespace :tcmb_currency do
2-
desc 'insert tcmb_currency rates to db'
3-
task :insert_from_tcmb => [:environment] do
4-
require "nokogiri"
5-
require "open-uri"
2+
desc 'insert tcmb_currency rates to db'
3+
task insert_from_tcmb: [:environment] do
4+
require 'nokogiri'
5+
require 'open-uri'
66
@doc = Nokogiri::XML(open('http://www.tcmb.gov.tr/kurlar/today.xml'))
7-
@code = Array.new
8-
@doc.css("Currency").each do |response_node|
9-
@code.push(response_node["Kod"])
7+
@code = []
8+
@doc.css('Currency').each do |response_node|
9+
@code.push(response_node['Kod'])
1010
end
1111
@data = @doc.xpath('//Currency')
12-
@currency = Hash.new
13-
i=0
14-
while i<@code.length
15-
cross = ""
16-
if @data[i].css('CrossRateUSD').children.to_s == ""
17-
cross = 1.to_f/@data[i].css('CrossRateOther').children.to_s.to_f
18-
else
19-
cross = @data[i].css('CrossRateUSD').children.to_s
20-
end
12+
@currency = {}
13+
i = 0
14+
while i < @code.length
15+
cross = ''
16+
cross = if @data[i].css('CrossRateUSD').children.to_s == ''
17+
1.to_f / @data[i].css('CrossRateOther').children.to_s.to_f
18+
else
19+
@data[i].css('CrossRateUSD').children.to_s
20+
end
2121
cross = cross.to_f.round(4)
22-
if cross.to_s != "Infinity"
22+
if cross.to_s != 'Infinity'
2323
@currency = {
24-
:code => @code[i],
25-
:name => @data[i].css('CurrencyName').children.to_s.encode('UTF-8'),
26-
:rate => cross.to_s,
27-
:date => Time.now.to_date
24+
code: @code[i],
25+
name: @data[i].css('CurrencyName').children.to_s.encode('UTF-8'),
26+
rate: cross.to_s,
27+
date: Time.now.to_date
2828
}
2929
Money::Bank::TcmbCurrency::CrossRate.create!(@currency)
3030
end
3131
i += 1
3232
end
3333
@currency = {
34-
:code => "TRY",
35-
:name => "Turkish Lira",
36-
:rate => @data[0].css('ForexBuying').children.to_s.to_f.round(4),
37-
:date => Time.now.to_date
34+
code: 'TRY',
35+
name: 'Turkish Lira',
36+
rate: @data[0].css('ForexBuying').children.to_s.to_f.round(4),
37+
date: Time.now.to_date
3838
}
3939
Money::Bank::TcmbCurrency::CrossRate.create(@currency)
4040
end
41-
end
41+
end

lib/tcmb_currency.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
require "tcmb_currency/version"
2-
require "active_support/dependencies"
1+
require 'tcmb_currency/version'
2+
require 'active_support/dependencies'
33

44
module TcmbCurrency
55
require 'tcmb_currency/railtie' if defined?(Rails)
6-
end
6+
end

lib/tcmb_currency/railtie.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Railtie < Rails::Railtie
55
railtie_name :tcmb_currency
66

77
rake_tasks do
8-
load "tasks/tcmb_currency.rake"
8+
load 'tasks/tcmb_currency.rake'
99
end
1010
end
11-
end
11+
end

lib/tcmb_currency/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module TcmbCurrency
2-
VERSION = '0.4.0'
2+
VERSION = '0.4.0'.freeze
33
end

0 commit comments

Comments
 (0)