crypto-service 0.0.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: caeb3d4bf826a92cf335e649c50fb7abadd7b6175c021f438641781f070f3e05
4
+ data.tar.gz: 9b5a35056f03f7701f8482cd28b2a55edbb0d587548192859bcfd2c0b5cc7c49
5
+ SHA512:
6
+ metadata.gz: 91558907b161b5cde84533d8df178cf2c20e31d570e029045a7eea9cf85a80868b39d7f0f51f4d9fdb03f712209affe94fc6f296c97b6db609ee18ca0fb7aea4
7
+ data.tar.gz: 4024d5c5eaa55c595a46f0404d778eabdcaa20cd27eba183c9b996f40f6053def3d78be7e41cf4bbafb0f3fab4fcb67a45b3100acba536a5ae398dc93cec4c18
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Crypto
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/crypto`. 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
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add crypto
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install crypto
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/crypto.
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zeitwerk'
4
+
5
+ module Crypto
6
+ class Autoloader
7
+ class Inflector < ::Zeitwerk::Inflector
8
+ INFLECTIONS_MAP = {
9
+ version: 'VERSION'
10
+ }.freeze
11
+
12
+ def camelize(basename, _abspath)
13
+ INFLECTIONS_MAP[basename.to_sym] || super
14
+ end
15
+ end
16
+
17
+ class << self
18
+ LIB_PATH = ::File.dirname(__dir__).freeze
19
+
20
+ def setup!
21
+ loader = ::Zeitwerk::Loader.new
22
+ loader.tag = 'crypto'
23
+ loader.inflector = Inflector.new
24
+ loader.push_dir(LIB_PATH)
25
+ loader.collapse(::File.join(LIB_PATH, 'crypto', 'resources'))
26
+
27
+ ignored_paths.each { |path| loader.ignore(path) }
28
+
29
+ loader.setup
30
+ end
31
+
32
+ private
33
+
34
+ def ignored_paths
35
+ [
36
+ ::File.join(LIB_PATH, 'crypto', 'patches')
37
+ ].freeze
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+ require 'net/http'
5
+ require 'json'
6
+
7
+ module Crypto
8
+ class Base
9
+ class << self
10
+ attr_accessor :app_info, :path_prefix
11
+ end
12
+
13
+ self.app_info = ::ENV['APP_INFO'] || "#{::Crypto::NAME} V#{::Crypto.version}"
14
+ self.path_prefix = 'https://www.binance.com/bapi/composite/v1/public/promo/cmc/cryptocurrency'
15
+ end
16
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/all'
4
+
5
+ module Crypto
6
+ class Listing < Base
7
+ MINUTES = 15
8
+ BASE_IMG_URL = 'https://s2.coinmarketcap.com/static/img/coins/64x64'
9
+
10
+ class << self
11
+ # GET /listings/new?limit=100&start=1
12
+ # Description: Acquire data of listing
13
+ # Parameter: /
14
+
15
+ # rubocop: disable Metrics/MethodLength, Lint/MissingCopEnableDirective
16
+ def info
17
+ payload = detail
18
+ if status?(payload)
19
+ news = []
20
+
21
+ announcements(payload).each do |announcement|
22
+ next unless correct_time?(announcement['date_added'])
23
+
24
+ news.append(formatting(announcement))
25
+ end
26
+
27
+ correct(news)
28
+ else
29
+ error(payload)
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def detail
36
+ uri = URI("#{Crypto.path}/listings/new?limit=100&start=1")
37
+ Crypto::Request.request(uri: uri, method: 'GET')
38
+ end
39
+
40
+ def status?(payload)
41
+ payload.dig('data', 'success') == true
42
+ end
43
+
44
+ def timezone
45
+ Time.now.in_time_zone('London').to_datetime
46
+ end
47
+
48
+ def correct_time?(time)
49
+ format_date = Time.parse(time).to_datetime
50
+ format_date >= (timezone.to_time - MINUTES.to_f.minutes).to_datetime
51
+ end
52
+
53
+ def announcements(payload)
54
+ payload.dig('data', 'body', 'data')
55
+ end
56
+
57
+ def formatting(announcement)
58
+ {
59
+ 'mode' => :listing,
60
+ 'img' => "#{BASE_IMG_URL}/#{announcement['id']}.png",
61
+ 'date_added' => announcement['date_added'],
62
+ 'name' => announcement['name'],
63
+ 'symbol' => announcement['symbol'],
64
+ 'quote' => announcement.dig('quote', 'USD')
65
+ }
66
+ end
67
+
68
+ def correct(news)
69
+ { 'status' => :success, 'code' => 200, 'payload' => news }
70
+ end
71
+
72
+ def error(payload)
73
+ base = payload.dig('data', 'body', 'status')
74
+
75
+ { 'status' => :failed, 'code' => base['error_code'], 'message' => base['error_message'] }
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crypto
4
+ class Request < Base
5
+ class << self
6
+ def request(uri:, method:)
7
+ req = get_method(name: method, uri: uri)
8
+ req['Content-Type'] = 'application/json'
9
+
10
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
11
+ http.request(req)
12
+ end
13
+
14
+ JSON.parse(res.body)
15
+ end
16
+
17
+ def get_method(name:, uri:)
18
+ case name
19
+ when 'GET'
20
+ req = Net::HTTP::Get.new(uri)
21
+ # when 'POST'
22
+ # req = Net::HTTP::Post.new(uri)
23
+ # when 'DELETE'
24
+ # req = Net::HTTP::Delete.new(uri)
25
+ end
26
+
27
+ req
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crypto
4
+ class Top < Base
5
+ PERCENTAGE = 20.0
6
+ BASE_IMG_URL = 'https://s2.coinmarketcap.com/static/img/coins/64x64'
7
+
8
+ class << self
9
+ # GET /trending/gainers-losers?limit=100&sort_dir=desc&start=1
10
+ # Description: Acquire data of top gainers crypto
11
+ # Parameter: /
12
+
13
+ # rubocop: disable Metrics/MethodLength, Lint/MissingCopEnableDirective
14
+ def info
15
+ payload = detail
16
+ if status?(payload)
17
+ news = []
18
+
19
+ cryptos(payload).each do |announcement|
20
+ next unless correct_percentage?(announcement.dig('quote', 'USD'))
21
+
22
+ news.append(formatting(announcement))
23
+ end
24
+
25
+ correct(news)
26
+ else
27
+ error(payload)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def detail
34
+ uri = URI("#{Crypto.path}/trending/gainers-losers?limit=100&sort_dir=desc&start=1")
35
+ Crypto::Request.request(uri: uri, method: 'GET')
36
+ end
37
+
38
+ def status?(payload)
39
+ payload.dig('data', 'success') == true
40
+ end
41
+
42
+ def correct_percentage?(percentages)
43
+ percentages['percent_change_24h'] >= PERCENTAGE
44
+ end
45
+
46
+ def cryptos(payload)
47
+ payload.dig('data', 'body', 'data')
48
+ end
49
+
50
+ def formatting(announcement)
51
+ {
52
+ 'mode' => :top,
53
+ 'img' => "#{BASE_IMG_URL}/#{announcement['id']}.png",
54
+ 'name' => announcement['name'],
55
+ 'symbol' => announcement['symbol'],
56
+ 'quote' => announcement.dig('quote', 'USD')
57
+ }
58
+ end
59
+
60
+ def correct(news)
61
+ { 'status' => :success, 'code' => 200, 'payload' => news }
62
+ end
63
+
64
+ def error(payload)
65
+ base = payload.dig('data', 'body', 'status')
66
+
67
+ { 'status' => :failed, 'code' => base['error_code'], 'message' => base['error_message'] }
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crypto
4
+ VERSION = '0.0.0'
5
+ end
data/lib/crypto.rb ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './crypto/autoloader'
4
+
5
+ module Crypto
6
+ NAME = 'Crypto Ruby'
7
+
8
+ @mutex = ::Mutex.new
9
+
10
+ class << self
11
+ def app_info=(value)
12
+ ::Crypto::Base.app_info = value
13
+ end
14
+
15
+ def ping
16
+ { status: 200 }
17
+ end
18
+
19
+ def path=(value)
20
+ @mutex.synchronize { ::Crypto::Base.path_prefix = value }
21
+ end
22
+
23
+ def path
24
+ @mutex.synchronize { ::Crypto::Base.path_prefix }
25
+ end
26
+
27
+ def version
28
+ VERSION
29
+ end
30
+ end
31
+ end
32
+
33
+ ::Crypto::Autoloader.setup!
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crypto-service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Stefano Baldazzi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: zeitwerk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bump
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: digest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '13.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '13.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.8'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.8'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.16'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.16'
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.14'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.14'
153
+ description: Crypto Ruby is a lightweight gem that return utils data for GreenAnt
154
+ web services.
155
+ email:
156
+ - stefanobaldazzi40@gmail.com
157
+ executables: []
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - README.md
162
+ - lib/crypto.rb
163
+ - lib/crypto/autoloader.rb
164
+ - lib/crypto/base.rb
165
+ - lib/crypto/resources/listing.rb
166
+ - lib/crypto/resources/request.rb
167
+ - lib/crypto/resources/top.rb
168
+ - lib/crypto/version.rb
169
+ homepage: https://github.com/Baldaz02/crypto-ruby
170
+ licenses:
171
+ - MIT
172
+ metadata: {}
173
+ post_install_message:
174
+ rdoc_options: []
175
+ require_paths:
176
+ - lib
177
+ required_ruby_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '2.7'
182
+ required_rubygems_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ requirements: []
188
+ rubygems_version: 3.3.24
189
+ signing_key:
190
+ specification_version: 4
191
+ summary: crypto0.0.0
192
+ test_files: []