abot-whitelist 0.1.0

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
+ SHA256:
3
+ metadata.gz: f8d096025944adc5c7c4dd674f96eea9886bc1a36687da0a7064f4ddbeb0d3ff
4
+ data.tar.gz: ef946a8ba024db803aac412b3c9fc6e4300f466af926533f1b422302671a9e10
5
+ SHA512:
6
+ metadata.gz: d4672433ba0a62ccb14f0ab2376ac3b489f02abd43592f5319851346e3594f3c236bdb31a7bb8484b1d773689c5cd39964fb8e9364978a9c05f5a6b68a437466
7
+ data.tar.gz: d4a7c2f736ad90b32ffe4c7c8d67a14fc62761394047ed7d82b5d5bb5a6b04cfa8aed7f73f4ff2d4abd57d26e30f1f784c9388942c0ba82a81474c03e84d1a99
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 6.1.3'
4
+ # gem 'paint', '~> 2.2.1'
5
+ # gem 'sqlite3', '~> 1.4.2'
6
+ # gem 'terminal-table', '~> 3.0.1'
7
+ gem 'binance-ruby', '~> 1.3.1'
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 w_dmitrii
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Abot::Whitelist
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/abot/whitelist`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'abot-whitelist'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install abot-whitelist
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/abot-whitelist. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Abot::Whitelist project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/abot-whitelist/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,39 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "abot/whitelist/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'abot-whitelist'
8
+ spec.version = Abot::Whitelist::VERSION
9
+ spec.authors = ['w_dmitrii']
10
+ spec.email = ['wiz.work2021@gmail.com']
11
+
12
+ spec.summary = 'Abot whitelist'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6.6')
15
+
16
+ spec.files = Dir['lib/**/*.rb', 'exe/*'] + %w[
17
+ abot-whitelist.gemspec
18
+ Gemfile
19
+ LICENSE
20
+ Rakefile
21
+ README.md
22
+ ]
23
+
24
+ spec.test_files = Dir['spec/**/*_spec.rb']
25
+
26
+ spec.extra_rdoc_files = %w[LICENSE README.md]
27
+
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.17"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+
36
+ spec.add_dependency "activerecord", '~> 6.1.3'
37
+ spec.add_dependency "binance-ruby", '~> 1.3.1'
38
+
39
+ end
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/abot/whitelist'
5
+
6
+ Abot::Whitelist.run
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'binance-ruby'
5
+
6
+ require_relative 'whitelist/option_parser'
7
+ # require_relative 'whitelist/coin'
8
+ # require_relative 'whitelist/binance_account'
9
+ require_relative 'whitelist/binance_cap'
10
+ require_relative 'whitelist/version'
11
+
12
+ module Abot
13
+ module Whitelist
14
+ class << self
15
+ def run
16
+ Whitelist::OptionParser.instance do |parser|
17
+ parser.program_name = 'abot-whitelist'
18
+ parser.version = Abot::Whitelist::VERSION
19
+
20
+ parser.separator ''
21
+ parser.separator 'Application options:'
22
+
23
+ parser.add_option(
24
+ :list_number, '--list_number=number',
25
+ 'Порядковый номер списка',
26
+ required: true
27
+ )
28
+
29
+ parser.add_option(
30
+ :top, '--top=number',
31
+ 'Число монет (ТОП по капитализации)',
32
+ )
33
+ end.final!
34
+
35
+ list_number = Whitelist::OptionParser.instance.options[:list_number].to_i
36
+ top_number = (Whitelist::OptionParser.instance.options[:top] || 100).to_i
37
+
38
+ raise 'list_number is wrong' unless list_number.positive?
39
+ raise 'top is wrong' unless top_number.positive?
40
+
41
+ base_list = Abot::Whitelist::BaseWhitelist.base_whitelist(list_number: list_number)
42
+ BinanceCap.send_req(base_list: base_list, top_number: top_number)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Abot
4
+ module Whitelist
5
+ class BaseWhitelist
6
+ include HTTParty
7
+
8
+ class << self
9
+ def base_whitelist(list_number:)
10
+ self.base_uri 'https://abot-config.ru'
11
+
12
+ path = "/coins/whitelist-#{list_number}.txt"
13
+ response = get(path)
14
+
15
+ if response.code == 200
16
+ response.body.split(' ') || ''
17
+ else
18
+ raise "get base whitelist from #{base_uri} error"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'base_whitelist'
3
+
4
+ module Abot
5
+ module Whitelist
6
+ class BinanceCap
7
+ include HTTParty
8
+
9
+ DEFAULT_COINS = %w[ONE UNFI]
10
+ FIAT = %w[USDT BUSD AUD BIDR BRL EUR GBR RUB TRY TUSD USDC DAI IDRT PAX UAH NGN VAI BVND]
11
+
12
+ class << self
13
+ def send_req(base_list:, top_number:)
14
+ self.base_uri 'https://www.binance.com'
15
+
16
+ path = '/exchange-api/v2/public/asset-service/product/get-products'
17
+ response = get(path)
18
+ process!(response: response || '{}', base_list: base_list, top_number: top_number)
19
+ end
20
+
21
+ private
22
+
23
+ def process!(response:, base_list:, top_number:)
24
+ result = top(response: response, top_number: top_number).map { |m| base_list.include?(m) ? m : nil }.compact
25
+ puts "base list: #{base_list}"
26
+ puts "+++++++++++++++++++++++++++++++++++++++++++++"
27
+ puts "coins (count = #{result.count}) from the base list in the top #{top_number} by capitalization:"
28
+ puts result.join(' ')
29
+ end
30
+
31
+ def top(response:, top_number:)
32
+ all_coins = usdt_coins(response: response).map { |s| [s[:b], (s[:cs].to_f * s[:c].to_f)] }
33
+ .sort_by { |s| s[1] }
34
+ .reverse
35
+ .map { |m| m[0] }
36
+ result = (all_coins - FIAT)[0..((top_number - 1) - DEFAULT_COINS.count)]
37
+ result + DEFAULT_COINS
38
+ end
39
+
40
+ def usdt_coins(response:)
41
+ serialized_response(response: response)[:data].select { |s| s[:q] == 'USDT' }
42
+ end
43
+
44
+ def serialized_response(response:)
45
+ begin
46
+ JSON.parse(response.body, symbolize_names: true)
47
+ rescue JSON::ParserError => error
48
+ # binance 500 errors are html format
49
+ raise Error.new(message: error)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,178 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'optparse'
4
+ require 'ostruct'
5
+
6
+ module Abot
7
+ module Whitelist
8
+ # Класс раширяет возможности стандартного gem'а
9
+ #
10
+ class OptionParser < OptionParser
11
+ class Error < StandardError; end
12
+
13
+ Option = Struct.new(:type, :value, :required)
14
+ private_class_method :new
15
+
16
+ private
17
+
18
+ def initialize
19
+ super
20
+ @options = {}
21
+ end
22
+
23
+ protected
24
+
25
+ def add_help_about_envs
26
+ envs = @options.select { |_, option| option[:type] == :environment }
27
+
28
+ return if envs.empty?
29
+
30
+ separator ''
31
+ separator 'Accessed environments:'
32
+ envs.each do |key, v|
33
+ separator ' ' * 4 + [key.to_s.upcase, v[:value]].join('=')
34
+ end
35
+ end
36
+
37
+ def show_help!
38
+ show_help = @options.fetch(
39
+ :help,
40
+ Option.new(:option, false, false)
41
+ )
42
+
43
+ return unless show_help[:value]
44
+
45
+ add_help_about_envs
46
+
47
+ print help
48
+ # получение справочной информации
49
+ Process.exit! unless ENV.fetch('RUN_TEST', nil)
50
+ end
51
+
52
+ def show_version!
53
+ show_version = @options.fetch(
54
+ :version,
55
+ Option.new(:option, false, false)
56
+ )
57
+
58
+ return unless show_version[:value]
59
+
60
+ puts ver
61
+ # аналогично методу #show_help!
62
+ Process.exit! unless ENV.fetch('RUN_TEST', nil)
63
+ end
64
+
65
+ def validate!
66
+ required_options = @options.select do |_, option|
67
+ option[:required] && option[:value].nil?
68
+ end
69
+
70
+ return if required_options.empty?
71
+
72
+ raise Error, <<~ERROR
73
+ Having problems:
74
+ #{
75
+ required_options.map do |key, option|
76
+ case option[:type]
77
+ when :environment
78
+ "- environment '#{key.to_s.upcase}' is required"
79
+ when :option
80
+ "- option '#{key}' is required"
81
+ end
82
+ end.join("\n")
83
+ }
84
+ ERROR
85
+ end
86
+
87
+ public
88
+
89
+ def self.instance
90
+ @instance ||= new
91
+
92
+ yield @instance if block_given?
93
+
94
+ @instance
95
+ end
96
+
97
+ # Обрабатывает переменную среды окружения и регистрирует его в опциях
98
+ #
99
+ # Указанный ключ в аргументе метода, при поиске переменной среды окружения
100
+ # будет преобразован сл. образом: `:env_key => "ENV_KEY"`
101
+ #
102
+ # @param key [Symbol]
103
+ #
104
+ # @param default [Object]
105
+ #
106
+ # @param required [FalseClass,TrueClass]
107
+ #
108
+ def add_env(key, default: nil, required: false)
109
+ raise Error, 'call after parse' if frozen?
110
+
111
+ @options[key] = Option.new(
112
+ :environment,
113
+ environment(key.to_s.upcase) || default,
114
+ required
115
+ )
116
+ end
117
+
118
+ # Добавляет обработчик опций командной строки
119
+ #
120
+ # @param key [Symbol]
121
+ #
122
+ # @param args [Array]
123
+ #
124
+ # @param default [Object]
125
+ #
126
+ # @param required [FalseClass,TrueClass]
127
+ #
128
+ def add_option(key, *args, default: nil, required: false)
129
+ raise Error, 'call after parse' if frozen?
130
+
131
+ @options[key] = Option.new(
132
+ :option,
133
+ default,
134
+ required
135
+ )
136
+
137
+ on(*args) { |value| @options[key][:value] = value }
138
+ end
139
+
140
+ # Выполняет обработку входящих аргументов и читает конфигурационый файл
141
+ #
142
+ # @return [void]
143
+ #
144
+ def final!(args = ARGV)
145
+ raise Error, 'duplicate call #final!' if frozen?
146
+
147
+ parse args
148
+ load @options[:config_file] ? @options[:config_file][:value] : nil
149
+ # заполняет значение до заморозки экземпляра класса
150
+ @banner = "Usage: #{program_name} [options]" if @banner.nil?
151
+
152
+ freeze
153
+
154
+ show_help!
155
+ show_version!
156
+ validate!
157
+ end
158
+
159
+ # Возвращает полученные в результате работы парсера опции
160
+ #
161
+ # @param keys_filter [Array<Symbol>] исключает отсуствующие ключи
162
+ #
163
+ # @return [Hash]
164
+ #
165
+ def options(keys_filter = [])
166
+ raise Error, 'call before #final!' unless frozen?
167
+
168
+ result = @options.each_with_object({}) do |option, acc|
169
+ acc[option[0]] = option[1][:value] unless option[1][:value].nil?
170
+ end
171
+
172
+ return result if keys_filter.empty?
173
+
174
+ result.select { |k, _| keys_filter.include? k }
175
+ end
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,5 @@
1
+ module Abot
2
+ module Whitelist
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ RSpec.describe Abot::Whitelist do
2
+ it "has a version number" do
3
+ expect(Abot::Whitelist::VERSION).not_to be nil
4
+ end
5
+
6
+ it "does something useful" do
7
+ expect(false).to eq(true)
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: abot-whitelist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - w_dmitrii
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-08-12 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: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 6.1.3
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 6.1.3
69
+ - !ruby/object:Gem::Dependency
70
+ name: binance-ruby
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.3.1
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.3.1
83
+ description:
84
+ email:
85
+ - wiz.work2021@gmail.com
86
+ executables:
87
+ - abot-whitelist
88
+ extensions: []
89
+ extra_rdoc_files:
90
+ - LICENSE
91
+ - README.md
92
+ files:
93
+ - Gemfile
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - abot-whitelist.gemspec
98
+ - exe/abot-whitelist
99
+ - lib/abot/whitelist.rb
100
+ - lib/abot/whitelist/base_whitelist.rb
101
+ - lib/abot/whitelist/binance_cap.rb
102
+ - lib/abot/whitelist/option_parser.rb
103
+ - lib/abot/whitelist/version.rb
104
+ - spec/abot/whitelist_spec.rb
105
+ homepage:
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 2.6.6
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubygems_version: 3.0.9
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Abot whitelist
128
+ test_files:
129
+ - spec/abot/whitelist_spec.rb