stock_markets 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ require 'stock_markets/version'
2
+ require 'stock_markets/file_data_processor'
3
+ require 'stock_markets/markets'
4
+ require 'stock_markets/configuration'
5
+
6
+ module StockMarkets
7
+ class << self
8
+ attr_writer :configuration
9
+ end
10
+
11
+ def self.configuration
12
+ @configuration ||= Configuration.new
13
+ end
14
+
15
+ def self.configure
16
+ yield(configuration)
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StockMarkets
4
+ class Configuration
5
+ attr_accessor :data_file_path
6
+
7
+ def initialize
8
+ @data_file_path = File.expand_path('../../data/stock_markets.csv', __FILE__)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+ require 'csv'
3
+
4
+ module StockMarkets
5
+ class FileDataProcessor
6
+ LAST_UPDATE_MARKET_MINIMAL_YEARS_COUNT = 2
7
+ ACTIVE_MARKET_STATUS_NAME = 'ACTIVE'
8
+ MARKET_NAME_CSV_COLUMN = :nameinstitution_description
9
+
10
+ attr_accessor :data
11
+
12
+ def initialize
13
+ @data = []
14
+ load_from_disk!
15
+ end
16
+
17
+ def load_from_disk!
18
+ self.parsed_csv = CSV.table(StockMarkets.configuration.data_file_path)
19
+ end
20
+
21
+ def transform_to_hash(key_name: :mic)
22
+ raise TypeError, 'Invalid object of CSV provided!' unless parsed_csv.instance_of?(CSV::Table)
23
+
24
+ self.data = parsed_csv.each_with_object({}) do |data_row, result_hash|
25
+ if market_recently_updated_proc.call(data_row, key_name)
26
+ result_hash[data_row[key_name.to_sym]] = data_row.to_h
27
+ end
28
+ end
29
+ self
30
+ end
31
+
32
+ def load_data_for_mics!
33
+ transform_to_hash.data
34
+ end
35
+
36
+ def load_data_for_names!
37
+ transform_to_hash(key_name: MARKET_NAME_CSV_COLUMN).data
38
+ end
39
+
40
+ private
41
+
42
+ attr_accessor :parsed_csv
43
+
44
+ def last_update_market_minimal_date
45
+ return @_last_update_market_minimal_date if @_last_update_market_minimal_date
46
+
47
+ current_month_date = Date.today - Date.today.mday + 1
48
+
49
+ @_last_update_market_minimal_date = Date.new(current_month_date.year - LAST_UPDATE_MARKET_MINIMAL_YEARS_COUNT, 1, 1)
50
+ end
51
+
52
+ def market_recently_updated_proc
53
+ @_market_recently_updated_proc ||= proc do |data_row, key_name|
54
+ (Date.parse(data_row[:status_date]) >= last_update_market_minimal_date) &&
55
+ data_row[:status] == ACTIVE_MARKET_STATUS_NAME &&
56
+ !data_row[key_name].nil?
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,43 @@
1
+ module StockMarkets
2
+ class Markets
3
+ extend Enumerable
4
+
5
+ class << self
6
+ def each(&block)
7
+ stock_markets_with_mics.values.each do |stock|
8
+ block.call(stock)
9
+ end
10
+ end
11
+
12
+ def [](stock_mic)
13
+ stock_markets_with_mics[stock_mic]
14
+ end
15
+
16
+ def data_list
17
+ stock_markets_with_mics.values
18
+ end
19
+
20
+ def table
21
+ stock_markets_with_mics
22
+ end
23
+
24
+ def table_for_names
25
+ stock_markets_with_names
26
+ end
27
+
28
+ private
29
+
30
+ def stock_markets_with_mics
31
+ @_stock_markets_with_mics ||= file_data_processor.load_data_for_mics!
32
+ end
33
+
34
+ def stock_markets_with_names
35
+ @_stock_markets_with_names ||= file_data_processor.load_data_for_names!
36
+ end
37
+
38
+ def file_data_processor(file_data_processor_klass = FileDataProcessor)
39
+ @_file_data_processor ||= file_data_processor_klass.new
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module StockMarkets
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,38 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stock_markets/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'stock_markets'
8
+ spec.version = StockMarkets::VERSION
9
+ spec.authors = ['Kirill Tatchihin']
10
+ spec.email = ['kirill.tatchihin@craft.co']
11
+
12
+ spec.summary = %q{See information about stock markets from all over the World}
13
+ spec.description = %q{See information about stock markets from all over the World}
14
+ spec.homepage = 'http://github.com/craft-machine/stock_markets'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['homepage_uri'] = spec.homepage
21
+ else
22
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
23
+ 'public gem pushes.'
24
+ end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = 'exe'
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ['lib']
34
+
35
+ spec.add_development_dependency 'bundler', '~> 2.0'
36
+ spec.add_development_dependency 'rake', '~> 10.0'
37
+ spec.add_development_dependency 'minitest', '~> 5.0'
38
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stock_markets
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kirill Tatchihin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: See information about stock markets from all over the World
56
+ email:
57
+ - kirill.tatchihin@craft.co
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - CHANGELOG.md
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/data/stock_markets.csv
74
+ - lib/stock_markets.rb
75
+ - lib/stock_markets/configuration.rb
76
+ - lib/stock_markets/file_data_processor.rb
77
+ - lib/stock_markets/markets.rb
78
+ - lib/stock_markets/version.rb
79
+ - stock_markets.gemspec
80
+ homepage: http://github.com/craft-machine/stock_markets
81
+ licenses:
82
+ - MIT
83
+ metadata:
84
+ homepage_uri: http://github.com/craft-machine/stock_markets
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.7.7
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: See information about stock markets from all over the World
105
+ test_files: []