rus_bank_rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +20 -0
  3. data/README.md +4 -0
  4. data/README.rdoc +3 -0
  5. data/Rakefile +32 -0
  6. data/lib/generators/rus_bank_rails_generator.rb +18 -0
  7. data/lib/generators/rus_bank_rails_generator/USAGE +9 -0
  8. data/lib/generators/rus_bank_rails_generator/templates/migration.rb +26 -0
  9. data/lib/generators/rus_bank_rails_generator/templates/model.rb +3 -0
  10. data/lib/rus_bank_rails.rb +4 -0
  11. data/lib/rus_bank_rails/acts_as_bank.rb +133 -0
  12. data/lib/rus_bank_rails/version.rb +3 -0
  13. data/lib/tasks/rus_bank_rails_tasks.rake +4 -0
  14. data/test/dummy/README.rdoc +28 -0
  15. data/test/dummy/Rakefile +6 -0
  16. data/test/dummy/app/assets/javascripts/application.js +13 -0
  17. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  18. data/test/dummy/app/controllers/application_controller.rb +5 -0
  19. data/test/dummy/app/helpers/application_helper.rb +2 -0
  20. data/test/dummy/app/models/bank.rb +3 -0
  21. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  22. data/test/dummy/bin/bundle +3 -0
  23. data/test/dummy/bin/rails +4 -0
  24. data/test/dummy/bin/rake +4 -0
  25. data/test/dummy/config.ru +4 -0
  26. data/test/dummy/config/application.rb +23 -0
  27. data/test/dummy/config/boot.rb +5 -0
  28. data/test/dummy/config/database.yml +25 -0
  29. data/test/dummy/config/environment.rb +5 -0
  30. data/test/dummy/config/environments/development.rb +29 -0
  31. data/test/dummy/config/environments/production.rb +80 -0
  32. data/test/dummy/config/environments/test.rb +36 -0
  33. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  34. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  35. data/test/dummy/config/initializers/inflections.rb +16 -0
  36. data/test/dummy/config/initializers/mime_types.rb +5 -0
  37. data/test/dummy/config/initializers/secret_token.rb +12 -0
  38. data/test/dummy/config/initializers/session_store.rb +3 -0
  39. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  40. data/test/dummy/config/locales/en.yml +23 -0
  41. data/test/dummy/config/routes.rb +56 -0
  42. data/test/dummy/db/development.sqlite3 +0 -0
  43. data/test/dummy/db/migrate/20140126153450_create_banks.rb +7 -0
  44. data/test/dummy/db/migrate/20140126171627_add_fields_to_banks.rb +23 -0
  45. data/test/dummy/db/schema.rb +40 -0
  46. data/test/dummy/db/test.sqlite3 +0 -0
  47. data/test/dummy/log/development.log +1338 -0
  48. data/test/dummy/log/test.log +7294 -0
  49. data/test/dummy/public/404.html +58 -0
  50. data/test/dummy/public/422.html +58 -0
  51. data/test/dummy/public/500.html +57 -0
  52. data/test/dummy/public/favicon.ico +0 -0
  53. data/test/dummy/test/fixtures/banks.yml +7 -0
  54. data/test/dummy/test/models/bank_test.rb +7 -0
  55. data/test/test_helper.rb +15 -0
  56. metadata +167 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 671510be9988431965721054876afe03d7d49367
4
+ data.tar.gz: ce78ac2be25277e4f0bcf83c4fdc5b7440be2384
5
+ SHA512:
6
+ metadata.gz: 795fba742e4addff50e99eee350b19a9e2af15c4b708e0f8324041820681e5e0bad41883c62edd54a4b48ea2103f938ab3ae37ce9dd3cf9ace710121f83e60af
7
+ data.tar.gz: 105b35174624be0d57c83d6b8a760c7b490e4cfb5e020e1d6e8f4a507b80ca770787e989827c38b2ae11d55437c1cb359e12ac2f3691ce40306619541529a1c4
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Alexsandrov Denis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ rus_bank_rails
2
+ ==============
3
+
4
+ DB-версия гема rus_bank - https://github.com/wildDAlex/rus_bank
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = RusBankRails
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'RusBankRails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,18 @@
1
+ class RusBankRailsGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path('../rus_bank_rails_generator', __FILE__)
3
+
4
+ def generate
5
+ migration_file_name = "db/migrate/#{Time.now.strftime("%Y%m%d%H%M%S")}_create_#{plural_name.parameterize.underscore}.rb"
6
+ model_file_name = "app/models/#{file_name.parameterize.underscore}.rb"
7
+
8
+ copy_file "templates/migration.rb", migration_file_name
9
+ copy_file "templates/model.rb", model_file_name
10
+
11
+ gsub_file migration_file_name, 'CreateMigration', "create_#{plural_name.parameterize.underscore}".camelize
12
+ gsub_file migration_file_name, 'table_name', ":#{plural_name.parameterize.underscore}"
13
+
14
+ gsub_file model_file_name, 'ModelName', "#{file_name.parameterize.underscore.camelize}"
15
+ end
16
+
17
+
18
+ end
@@ -0,0 +1,9 @@
1
+ Description:
2
+ Provides prerequisities to work with rus_bank_rails gem
3
+
4
+ Example:
5
+ rails generate rus_bank_rails Bank
6
+
7
+ This will create:
8
+ bank.rb in app/models
9
+ {timestamp}_create_banks.rb in db/migrations
@@ -0,0 +1,26 @@
1
+ class CreateMigration < ActiveRecord::Migration
2
+ def change
3
+ create_table table_name, force: true do |t|
4
+ t.integer "reg_number"
5
+ t.integer "internal_code"
6
+ t.string "bic"
7
+ t.string "org_name"
8
+ t.string "org_full_name"
9
+ t.string "phones"
10
+ t.date "date_kgr_registration"
11
+ t.string "main_reg_number"
12
+ t.date "main_date_reg"
13
+ t.string "ustav_adr"
14
+ t.string "fact_adr"
15
+ t.string "director"
16
+ t.integer "ust_money"
17
+ t.string "org_status"
18
+ t.integer "reg_code"
19
+ t.date "ssv_date"
20
+ t.integer "l_code"
21
+ t.string "lt"
22
+ t.date "l_date"
23
+ t.timestamps
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ class ModelName < ActiveRecord::Base
2
+ acts_as_bank
3
+ end
@@ -0,0 +1,4 @@
1
+ require "rus_bank_rails/acts_as_bank"
2
+
3
+ module RusBankRails
4
+ end
@@ -0,0 +1,133 @@
1
+ module RusBankRails
2
+ module ActsAsBank
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_bank
10
+ include RusBankRails::ActsAsBank::LocalInstanceMethods
11
+ end
12
+ end
13
+
14
+ module LocalInstanceMethods
15
+ ##
16
+ # Метод возвращает внутренний номер банка по БИК
17
+
18
+ def BicToIntCode(bic)
19
+ resp = check_and_update(bic)
20
+ resp ? resp.internal_code : nil
21
+ end
22
+
23
+ ##
24
+ # Метод возвращает регистрационный номер банка по БИК
25
+
26
+ def BicToRegNumber(bic)
27
+ resp = check_and_update(bic)
28
+ resp ? resp.reg_number : nil
29
+ end
30
+
31
+ ##
32
+ # Возвращает регистрационный номер банка по внутреннему номеру
33
+
34
+ def RegNumToIntCode(reg_number)
35
+ bank = self.class.find_by_reg_number(reg_number.to_i)
36
+ get_int_code_by_reg_number = lambda {
37
+ begin
38
+ cbr = RusBank.new
39
+ internal_code = cbr.RegNumToIntCode(reg_number)
40
+ bic = cbr.CreditInfoByIntCode(internal_code)[:co][:bic]
41
+ check_and_update(bic)
42
+ return internal_code.to_i
43
+ rescue SocketError, Savon::SOAPFault => e
44
+ handle_exception(e)
45
+ return nil
46
+ end
47
+ }
48
+ if bank.nil?
49
+ get_int_code_by_reg_number[]
50
+ else
51
+ resp = check_and_update(bank.bic)
52
+ if resp.reg_number == reg_number.to_i # на случай если после обновления записи в базе reg_number
53
+ resp.internal_code # поменялся и найденный банк из базы становится неактуальным
54
+ else
55
+ get_int_code_by_reg_number[]
56
+ end
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ ##
63
+ # Метод проверяет дату обновления записи в базе и пытается обновить в случае необходимости
64
+
65
+ def check_and_update(bic)
66
+ bank = self.class.find_by_bic(bic)
67
+ if bank.nil?
68
+ new_bank(bic)
69
+ else
70
+ if bank.updated_at < 1.minute.ago
71
+ update_bank(bank)
72
+ else
73
+ bank
74
+ end
75
+ end
76
+ end
77
+
78
+ ##
79
+ # Метод создает новый банк в базе
80
+
81
+ def new_bank(bic)
82
+ if info = get_info(bic)
83
+ bank = self.class.new(info)
84
+ bank.save
85
+ bank
86
+ else
87
+ nil
88
+ end
89
+ end
90
+
91
+
92
+ ##
93
+ # Обновляет переданный экземпляр банка в базе
94
+
95
+ def update_bank(bank)
96
+ info = get_info(bank.bic)
97
+ bank.update(info.merge(updated_at: Time.now)) unless info.nil?
98
+ bank
99
+ end
100
+
101
+ ##
102
+ # Метод возвращает актуальную информацию по банку с сайта ЦБР
103
+
104
+ def get_info(bic)
105
+ begin
106
+ cbr = RusBank.new
107
+ internal_code = cbr.BicToIntCode(bic)
108
+ reg_number = cbr.BicToRegNumber(bic)
109
+ info = cbr.CreditInfoByIntCode(internal_code) if internal_code
110
+ rescue SocketError, Savon::SOAPFault => e
111
+ handle_exception(e)
112
+ return nil
113
+ end
114
+
115
+ if internal_code && reg_number && info
116
+ info[:co].merge(info[:lic]).merge(internal_code: internal_code, reg_number: reg_number)
117
+ else
118
+ nil
119
+ end
120
+ end
121
+
122
+ def handle_exception(e)
123
+ puts "========== ========== =========="
124
+ puts e.inspect
125
+ puts "========== ========== =========="
126
+ end
127
+
128
+ end
129
+
130
+ end
131
+ end
132
+
133
+ ActiveRecord::Base.send :include, RusBankRails::ActsAsBank
@@ -0,0 +1,3 @@
1
+ module RusBankRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rus_bank_rails do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,3 @@
1
+ class Bank < ActiveRecord::Base
2
+ acts_as_bank
3
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,23 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require(*Rails.groups)
6
+ require "rus_bank_rails"
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
15
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
16
+ # config.time_zone = 'Central Time (US & Canada)'
17
+
18
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
19
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
20
+ # config.i18n.default_locale = :de
21
+ end
22
+ end
23
+
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)