tcmb_currency 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ba26eb3eee5d183725d086dbc13ff2303777bc86
4
+ data.tar.gz: 97282259c487cf56f1061477a4673339c47b7b8d
5
+ SHA512:
6
+ metadata.gz: 0cf0f1980f7d6033af3d2bd4fd78721c97d9c04fd99ccc6f7e1fd332566854aa7d053f7a6dfbddfe0590b1ed756d68b53708672d863efedc175a4b643864b8b1
7
+ data.tar.gz: d98aa0de70a77bf9a1fd02b108e60a4d629ad369bd472ac7e9b565acf4660c9bf2443508b29045f07fd46db8be7cb467ef7c8bf646becfd5a024a117c386fad0
@@ -0,0 +1,14 @@
1
+ require 'rails/generators/migration'
2
+ require 'rails/generators/active_record'
3
+
4
+ module TcmbCurrency
5
+ class InitializerGenerator < ::Rails::Generators::Base
6
+ source_root File.join(File.dirname(__FILE__), 'templates')
7
+
8
+ desc 'Creates a sample Tcmb_Currency initializer.'
9
+
10
+ def copy_initializer
11
+ copy_file 'money.rb', 'config/initializers/money.rb'
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # encoding : utf-8
4
+ require 'money'
5
+ require 'money/bank/tcmb_currency'
6
+ require 'json'
7
+ MultiJson.engine = :json_gem
8
+ Money.default_bank = Money::Bank::TcmbCurrency.new
9
+ CROSS_RATE = Money::Bank::TcmbCurrency::CrossRate
10
+ CURRENCY_TYPE = Money::Bank::TcmbCurrency::CurrencyType
@@ -0,0 +1,23 @@
1
+ require 'rails/generators/migration'
2
+ require 'rails/generators/active_record'
3
+
4
+ module TcmbCurrency
5
+ class MigrationGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ namespace 'tcmb_currency:migration'
9
+
10
+ source_root File.join(File.dirname(__FILE__), 'templates')
11
+
12
+ def self.next_migration_number(dirname)
13
+ ActiveRecord::Generators::Base.next_migration_number(dirname)
14
+ end
15
+
16
+ def create_migration_file
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
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ class CreateCrossRatesTable < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :cross_rates do |t|
4
+ t.string :code
5
+ t.string :name
6
+ t.string :rate
7
+ t.date :date
8
+ t.timestamps
9
+ end
10
+ add_index :cross_rates, :code
11
+ end
12
+
13
+ def self.down
14
+ drop_table :cross_rates
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ class CreateCurrencyTypes < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :currency_types do |t|
4
+ t.string :currency
5
+
6
+ t.timestamps
7
+ end
8
+ currencies = %w[ USD EUR GBP TRY CAD XDR DKK SEK CHF NOK
9
+ JPY SAR KWD AUD RUB RON PKR IRR CNY BGN ]
10
+ currencies.each do |c|
11
+ CURRENCY_TYPE.create(currency: c.to_s)
12
+ end
13
+ end
14
+
15
+ def self.down
16
+ drop_table :currency_types
17
+ end
18
+ end
@@ -0,0 +1,76 @@
1
+ require 'tcmb_currency/version'
2
+ require 'money'
3
+ require 'open-uri'
4
+ require 'multi_json'
5
+ require 'active_record'
6
+
7
+ class Money
8
+ def exchange_to(to_currency, date = nil)
9
+ bank = Bank::TcmbCurrency.new
10
+ bank.exchange_with!(self, to_currency, date)
11
+ end
12
+
13
+ module Bank
14
+ class TcmbCurrency < Money::Bank::VariableExchange
15
+ class CrossRate < ActiveRecord::Base
16
+ end
17
+
18
+ class CurrencyType < ActiveRecord::Base
19
+ end
20
+
21
+ attr_reader :rates
22
+
23
+ def exchange_with!(*args)
24
+ from = args[0]
25
+ to_currency = args[1]
26
+ date = args[2]
27
+ return from if same_currency?(from.currency, to_currency)
28
+ rate = fetch_rate!(from.currency, to_currency, date)
29
+ unless rate
30
+ raise UnknownRate, "No conversion rate known for
31
+ '#{from.currency.iso_code}' -> '#{to_currency}'"
32
+ end
33
+ wrap_to_currency_ = Currency.wrap(to_currency)
34
+ fractional = get_fractional(from, wrap_to_currency_)
35
+ ex = (fractional * BigDecimal.new(rate.to_s)).to_f
36
+ ex = if block_given?
37
+ yield ex
38
+ elsif @rounding_method
39
+ @rounding_method.call(ex)
40
+ else
41
+ ex.to_s.to_i
42
+ end
43
+ Money.new(ex, wrap_to_currency_)
44
+ end
45
+
46
+ private
47
+
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)
62
+ if date.nil?
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'
65
+ else
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'
71
+ end
72
+ to_rate / from_rate
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,41 @@
1
+ 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'
6
+ @doc = Nokogiri::XML(open('http://www.tcmb.gov.tr/kurlar/today.xml'))
7
+ @code = []
8
+ @doc.css('Currency').each do |response_node|
9
+ @code.push(response_node['Kod'])
10
+ end
11
+ @data = @doc.xpath('//Currency')
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
21
+ cross = cross.to_f.round(4)
22
+ if cross.to_s != 'Infinity'
23
+ @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
28
+ }
29
+ Money::Bank::TcmbCurrency::CrossRate.create!(@currency)
30
+ end
31
+ i += 1
32
+ end
33
+ @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
38
+ }
39
+ Money::Bank::TcmbCurrency::CrossRate.create(@currency)
40
+ end
41
+ end
@@ -0,0 +1,11 @@
1
+ require 'tcmb_currency'
2
+ require 'rails'
3
+ module TcmbCurrency
4
+ class Railtie < Rails::Railtie
5
+ railtie_name :tcmb_currency
6
+
7
+ rake_tasks do
8
+ load 'tasks/tcmb_currency.rake'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module TcmbCurrency
2
+ VERSION = '0.5.1'.freeze
3
+ end
@@ -0,0 +1,6 @@
1
+ require 'tcmb_currency/version'
2
+ require 'active_support/dependencies'
3
+
4
+ module TcmbCurrency
5
+ require 'tcmb_currency/railtie' if defined?(Rails)
6
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tcmb_currency
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.1
5
+ platform: ruby
6
+ authors:
7
+ - lab2023
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.4.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.4.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: money
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: money-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yajl-ruby
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 1.0.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 1.0.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 0.5.8
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 0.5.8
111
+ description: TCMB gem
112
+ email:
113
+ - info@lab2023.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - lib/generators/tcmb_currency/initializer/initializer_generator.rb
119
+ - lib/generators/tcmb_currency/initializer/templates/money.rb
120
+ - lib/generators/tcmb_currency/migration/migration_generator.rb
121
+ - lib/generators/tcmb_currency/migration/templates/migration.rb
122
+ - lib/generators/tcmb_currency/migration/templates/typemigration.rb
123
+ - lib/money/bank/tcmb_currency.rb
124
+ - lib/tasks/tcmb_currency.rake
125
+ - lib/tcmb_currency.rb
126
+ - lib/tcmb_currency/railtie.rb
127
+ - lib/tcmb_currency/version.rb
128
+ homepage: https://github.com/lab2023/tcmb_currency
129
+ licenses: []
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: 1.3.6
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.5.2
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Access the TCMB exchange rate data.
151
+ test_files: []