ctfc 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6df1fb47102b8c5c62d4340a69adf54db78ee380e8d0b88cf2b3201ad2229acb
4
+ data.tar.gz: 6302f744ba17f497fbd35bff59e695443bd74eec481b357462f70b3d049531a7
5
+ SHA512:
6
+ metadata.gz: 050abfa5bf9c9dd8b3444c1e89dca6af824b2cc4d2b215dd4e991058026e8b7dbf018231601d0b9d6e8f221eee5cffbd77f4b38dd53bdf8787f21cb9d2e0713a
7
+ data.tar.gz: 862f70090801ffedd6e9a01b06f1a341f2d771673b685868c87c683a542f09da7bb2cb03c039897b34b75bbb0638369ec42634c9e63a6c988baae5a9d4fb7341
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 alx3dev
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 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,
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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,133 @@
1
+ # About
2
+
3
+ Convert any crypto to fiat currency, gather all data and/or save in `.csv` table.
4
+ For now only prices are printed/saved, while all data remain easily accessible from variable (for developers).
5
+
6
+
7
+ # How to install
8
+
9
+ Make sure you have ruby and git installed:
10
+
11
+ ```bash
12
+ git clone https://github.com/alx3dev/ctfc.git
13
+ cd ctfc && bundle install
14
+ ```
15
+
16
+ # How to run
17
+
18
+ ```bash
19
+ ruby bin/ctfc fiat_1 fiat_2 fiat_3
20
+ ```
21
+
22
+ This command also accept multiple arguments:
23
+
24
+ - `--no-save` - do not save `.csv.` output
25
+ - `--no-print` - do not print terminal output
26
+ - `--coins` - coins to scrap (default: BTC, LTC, XMR, ETH, BCH, ZEC )
27
+ - `--help` - help menu
28
+
29
+
30
+ # Script Examples
31
+
32
+ 1 - Run script without arguments (default options)
33
+
34
+ ```
35
+ ruby bin/ctfc
36
+
37
+ @return:
38
+ print EUR rates for default coins (BTC, LTC, XMR, ETH, BCH, ZEC)
39
+ do not save '.csv' table
40
+ ```
41
+
42
+
43
+ 2 - Add fiat currencies as arguments
44
+
45
+ ```
46
+ ruby bin/ctfc eur usd rsd
47
+
48
+ @return:
49
+ print EUR, USD, RSD rates for default coins
50
+ save data in '.csv' table with pattern: 'crypto_CURRENCY.csv'
51
+ -> './crypto_eur.csv', './crypto_usd.csv', './crypto_rsd.csv'
52
+ ```
53
+
54
+ 3 - Use `--no-save` and/or `--no-print`
55
+
56
+ ```
57
+ ruby bin/ctfc eur --no-print --coins btc xmr ltc
58
+
59
+ @return:
60
+ save EUR rates for BTC, XMR and LTC
61
+ do not print output
62
+
63
+
64
+ ruby bin/ctfc rsd --no-save --coins btc xmr
65
+
66
+ @return:
67
+ print RSD rates for BTC and XMR
68
+
69
+ ```
70
+
71
+
72
+ # Developer Examples
73
+
74
+
75
+ ```ruby
76
+ # define coins to scrap
77
+ COINS = %w[ BTC XMR LTC ETH ]
78
+
79
+ # initialize Data class
80
+ # DEPRECEATED - use Ctfc.new instead
81
+ @data = CTFC::Data.new :eur, save: false, print: false, coins: COINS
82
+ @return CTFC::Data object with data to perform request
83
+ => #<CTFC::Data:0x000055715a6ce898 @coins=["BTC", "LTC", "XMR", "ETH", "BCH", "ZEC"], @currency="EUR", @print=true, @save=false>
84
+
85
+ # execute request
86
+ @data.get
87
+ @return Hash with upcase string coins as keys, and float prices
88
+ => {"BTC"=>36760.11, "XMR"=>169.55, "LTC"=>114.4, "ETH"=>2746.22}
89
+
90
+ # now you can use ::Data instance methods
91
+ @data.response
92
+ @return RestClient response to cryptocomare API
93
+ => <RestClient::Response 200 "{\"RAW\":{\"BT...">
94
+
95
+ @data.url
96
+ @return Cryptocompare API url
97
+ => "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC&fsyms=LTC&fsyms=XMR&fsyms=ETH&fsyms=BCH&fsyms=ZEC&tsyms=EUR"
98
+
99
+ @data.table
100
+ @return '.csv' table name
101
+ => 'ctfc_eur.csv'
102
+
103
+ @data.coins
104
+ @return coins for scrap, also allow setter method @data.coins = [...]
105
+ => ['BTC', 'XMR', 'LTC', 'ETH']
106
+
107
+ @data.data
108
+ @return all data returned by cryptocompare API
109
+ => ... ... ...
110
+
111
+
112
+ TO BE CONTINIUED ...
113
+ ```
114
+
115
+ **Class methods added in Version-0.2.1**
116
+
117
+ ```ruby
118
+ # Ctfc class extend CTFC::Data, for easier work:
119
+
120
+ prices = Ctfc.new :eur, print: false
121
+
122
+ # Class method `#to` was added as shortcut:
123
+
124
+ Ctfc.to :rsd, save: false
125
+
126
+ # For those who don't like name `Ctfc`, you can use `Crypto` too:
127
+
128
+ prices = Crypto.to :eur, coins: %w[BTC XMR]
129
+
130
+ ```
131
+
132
+ # TO-DO:
133
+ Write documentation, examples and use-cases as gem dependency
data/bin/console ADDED
@@ -0,0 +1,9 @@
1
+ #/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'pry'
5
+ require 'bundler/setup'
6
+
7
+ require_relative '../lib/ctfc'
8
+
9
+ Pry.start
data/bin/ctfc ADDED
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/ctfc'
5
+ require 'optimist'
6
+
7
+ opts = Optimist.options do
8
+ version "Software Version: #{CTFC::VERSION}"
9
+
10
+ banner ''
11
+ banner ' Enter fiat currencies with/out additional arguments:'
12
+ banner ''
13
+ banner ' ruby bin/ctfc eur'
14
+ banner ' ruby bin/ctfc eur usd --no-save --coins btc xmr ltc'
15
+ banner ''
16
+
17
+ opt :coins, 'Set crypto coins', default: CTFC::CONFIG::COINS
18
+ opt :no_save, "Do not save '.csv' output"
19
+ opt :no_print, 'Do not print terminal output'
20
+ end
21
+
22
+ save = opts[:no_save] ? false : true
23
+ print = opts[:no_print] ? false : true
24
+
25
+ if ARGV.empty?
26
+
27
+ Crypto.to :eur, save: false, print: true
28
+
29
+ else
30
+
31
+ ARGV.each do |fiat|
32
+ next if opts.include? fiat.downcase
33
+
34
+ Ctfc.to(fiat,
35
+ save: save,
36
+ print: print,
37
+ coins: opts.coins)
38
+ end
39
+ end
data/ctfc.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './lib/ctfc/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'ctfc'
7
+ s.version = CTFC::VERSION
8
+ s.summary = 'Crypto to Fiat currency data gathering'
9
+ s.description = 'Convert any crypto to fiat currency and/or save in csv table.'
10
+
11
+ s.license = 'MIT'
12
+ s.authors = 'alx3dev'
13
+ s.homepage = 'https://github.com/alx3dev/ctfc'
14
+
15
+ s.bindir = 'bin'
16
+ s.require_paths = ['lib']
17
+ s.executables = ['ctfc']
18
+
19
+ s.metadata['homepage_uri'] = 'https://github.com/alx3dev/ctfc'
20
+ s.metadata['source_code_uri'] = 'https://github.com/alx3dev/ctfc'
21
+ s.metadata['bug_tracker_uri'] = 'https://github.com/alx3dev/ctfc/issues'
22
+
23
+ s.files = ['bin/ctfc', 'bin/console', 'lib/ctfc.rb', 'LICENSE', 'README.md', 'ctfc.gemspec',
24
+ 'lib/ctfc/config.rb', 'lib/ctfc/version.rb', 'lib/ctfc/base.rb']
25
+
26
+ s.required_ruby_version = '>= 3.0.1'
27
+
28
+ s.add_runtime_dependency 'colorize', '~> 0.8.1'
29
+ s.add_runtime_dependency 'optimist', '~> 3.0.1'
30
+ s.add_runtime_dependency 'rest-client', '~> 2.1.0'
31
+
32
+ s.add_development_dependency 'bundler', '~> 2.2.9'
33
+ s.add_development_dependency 'pry', '~> 0.14.1'
34
+ s.add_development_dependency 'rake', '~> 13.0.3'
35
+ end
data/lib/ctfc/base.rb ADDED
@@ -0,0 +1,196 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'config'
4
+ require_relative 'version'
5
+
6
+ require 'json'
7
+ require 'csv'
8
+ require 'colorize'
9
+ require 'rest-client'
10
+
11
+ module CTFC
12
+ ##
13
+ # Data class keep all the logic to send request, receive response,
14
+ # and everything between. Class Ctfc extend CTFC::Data, for easier work.
15
+ #
16
+ # @note Instead of using CTFC::Data.new, recommended way is to call Ctfc.new
17
+ #
18
+ class Data
19
+ include CONFIG
20
+
21
+ attr_reader :response, :data, :url, :table, :count, :prices
22
+ attr_accessor :fiat, :coins
23
+
24
+ alias currency fiat
25
+
26
+ ##
27
+ # @example Initialization example
28
+ #
29
+ # @data = CTFC::Data.new :eur, save: true
30
+ #
31
+ # @param [Symbol] currency **Optional**. Define fiat currency.
32
+ # @param [Hash] opts **Optional**. Additional options hash.
33
+ #
34
+ # @option opts [Boolean] print **Optional**. Print terminal output.
35
+ # @option opts [Boolean] save **Optional**. Save `.csv` output.
36
+ # @option opts [Array] coins **Optional**. Define coins to scrap.
37
+ #
38
+ # @return [Object] Data object to work with
39
+ #
40
+ def initialize(currency = :eur, opts = {})
41
+ @fiat = currency.to_s.upcase
42
+ @save = opts[:save].nil? ? true : opts[:save]
43
+ @print = opts[:print].nil? ? true : opts[:print]
44
+ @coins = opts[:coins].nil? ? COINS : Array(opts[:coins])
45
+ end
46
+
47
+ ##
48
+ # @example Get fiat prices for previous config
49
+ #
50
+ # @data.get
51
+ #
52
+ # @example Get prices and change previous config "on-the-fly"
53
+ #
54
+ # @data.get :usd, save: false, coins: %w[BTC XMR ETH]
55
+ #
56
+ # @param [Symbol || String] currency **Optional**. Change fiat currency and execute request.
57
+ # @param [Hash] opts **Optional**. Options hash to change config 'on-the-fly' - see #initialize.
58
+ #
59
+ def get(currency = nil, opts = {})
60
+ @fiat = currency.to_s.upcase unless currency.nil?
61
+ @coins = opts[:coins] unless opts[:coins].nil?
62
+ @save = opts[:save] unless opts[:save].nil?
63
+ @print = opts[:print] unless opts[:print].nil?
64
+ @count = 0
65
+ @table = "ctfc_#{@fiat}.csv".downcase
66
+ do_rest_request
67
+ end
68
+
69
+ ##
70
+ # Get fiat value from response hash with crypto prices
71
+ #
72
+ # @example
73
+ #
74
+ # @data.price(:btc)
75
+ #
76
+ # @param [Symbol || String] coin **Required**. Coin name as symbol or string.
77
+ # @return [Float]
78
+ #
79
+ def price(coin)
80
+ @prices[coin.to_s.upcase]
81
+ end
82
+
83
+ ##
84
+ # Check if crypto prices will be saved in `.csv` table
85
+ #
86
+ # @return [true || false]
87
+ #
88
+ def save?
89
+ @save == true
90
+ end
91
+
92
+ ##
93
+ # Check if crypto prices will be printed in terminal
94
+ #
95
+ # @return [true || false]
96
+ #
97
+ def print?
98
+ @print == true
99
+ end
100
+
101
+ ##
102
+ # Change option to save '.csv' table with prices
103
+ #
104
+ # @return [true || false]
105
+ #
106
+ def save=(opt)
107
+ @save = opt.is_a?(TrueClass) ? true : false
108
+ end
109
+
110
+ ##
111
+ # Change option to print prices in terminal
112
+ #
113
+ # @return [true || false]
114
+ #
115
+ def print=(opt)
116
+ @print = opt.is_a?(TrueClass) ? true : false
117
+ end
118
+
119
+ ##
120
+ # Check if request was successful or not.
121
+ #
122
+ # @return [true || false]
123
+ #
124
+ def success?
125
+ return false if @response.nil?
126
+
127
+ @response.code == 200
128
+ end
129
+
130
+ private
131
+
132
+ def do_rest_request
133
+ prepare_uri
134
+ process_data
135
+ @prices
136
+ rescue StandardError
137
+ @count += 1
138
+
139
+ if @count >= MAX_RETRY
140
+ puts @response.to_s.split(',')
141
+ else
142
+ do_rest_request
143
+ end
144
+ end
145
+
146
+ def process_data
147
+ @response = RestClient.get @url
148
+ @data = JSON.parse @response
149
+
150
+ @data_array << Time.now.to_s
151
+ @coins.each do |coin|
152
+ value = @data['RAW'][coin.to_s.upcase][@fiat.to_s.upcase]['PRICE'].round(2)
153
+ @prices[coin] = value
154
+ @data_array << value
155
+ end
156
+
157
+ print_fiat_values
158
+ save_csv_data
159
+ end
160
+
161
+ def prepare_uri
162
+ @prices = Hash.new {}
163
+ @data_array = Array.new []
164
+ coin_uri = String.new ''
165
+ @coins.collect { |coin| coin_uri << "fsyms=#{coin}&" }
166
+ @url = URL + "#{coin_uri}tsyms=#{@fiat}"
167
+ end
168
+
169
+ def print_fiat_values
170
+ return unless print?
171
+
172
+ 30.times { print '='.green }
173
+ puts ''
174
+ puts "#{'['.green}#{@fiat.to_s.upcase.yellow.bold}#{']'.green} conversion rate"
175
+ 30.times { print '='.green }
176
+ puts ''
177
+ @prices.each do |name, value|
178
+ print '['.yellow.bold + name.to_s.green.bold + ']'.yellow.bold
179
+ puts ": #{value}".bold
180
+ end
181
+ end
182
+
183
+ def save_csv_data
184
+ return unless save?
185
+
186
+ create_csv_headers unless File.exist?(@table)
187
+ CSV.open(@table, 'ab') { |column| column << @data_array }
188
+ end
189
+
190
+ def create_csv_headers
191
+ header_array = ['TIME']
192
+ @coins.each { |coin| header_array << coin }
193
+ CSV.open(@table, 'w') { |header| header << header_array }
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CTFC
4
+ ##
5
+ # Keep default configuration data, like coins to scrap, max number
6
+ # of retries and cryptocompare API url.
7
+ #
8
+ module CONFIG
9
+ # default coins to use
10
+ COINS = %w[BTC LTC XMR ETH BCH ZEC].freeze
11
+
12
+ # max number of retries if request fail
13
+ MAX_RETRY = 3
14
+
15
+ # Cryptocompare API - base url for requests
16
+ URL = 'https://min-api.cryptocompare.com/data/pricemultifull?'
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CTFC
4
+ VERSION = '0.3.1'
5
+ end
data/lib/ctfc.rb ADDED
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'ctfc/base'
4
+
5
+ ##
6
+ # For easier job use Ctfc, instead of typing CTFC::Data.
7
+ # You can define default coins with Ctfc::COINS=
8
+ #
9
+ class Ctfc < CTFC::Data
10
+ ##
11
+ # @todo Allow Ctfc to use proxy and/or tor
12
+ #
13
+ def initialize(currency = :eur, opts = {})
14
+ opts[:coins] ||= COINS
15
+ super(currency, opts)
16
+ end
17
+
18
+ ##
19
+ # @example Get EUR data for BTC, XMR, LTC, ETH, print but don't save output
20
+ #
21
+ # Ctfc.to :eur, save: false, coins: %w[BTC XMR LTC ETH]
22
+ #
23
+ # @param [Symbol] currency **Required**. Define fiat currency.
24
+ # @param [Hash] opts **Optional**. Additional options hash.
25
+ #
26
+ # @option opts [Boolean] print **Optional**. Print terminal output.
27
+ # @option opts [Boolean] save **Optional**. Save `.csv` output.
28
+ # @option opts [Array] coins **Optional**. Define coins to scrap.
29
+ #
30
+ # @return [Hash] CTFC::Data#prices || CTFC::Data#response
31
+ #
32
+ def self.to(currency, opts = {})
33
+ new(currency.to_sym, opts).get
34
+ end
35
+ end
36
+
37
+ ##
38
+ # Same as Ctfc
39
+ # @see Ctfc
40
+ # @see CTFC::Data
41
+ #
42
+ class Crypto < Ctfc
43
+ def initialize(currency = :eur, opts = {})
44
+ opts[:coins] ||= COINS
45
+ super(currency, opts)
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ctfc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.1
5
+ platform: ruby
6
+ authors:
7
+ - alx3dev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: optimist
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.1.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.2.9
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.2.9
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.14.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.14.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 13.0.3
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 13.0.3
97
+ description: Convert any crypto to fiat currency and/or save in csv table.
98
+ email:
99
+ executables:
100
+ - ctfc
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - LICENSE
105
+ - README.md
106
+ - bin/console
107
+ - bin/ctfc
108
+ - ctfc.gemspec
109
+ - lib/ctfc.rb
110
+ - lib/ctfc/base.rb
111
+ - lib/ctfc/config.rb
112
+ - lib/ctfc/version.rb
113
+ homepage: https://github.com/alx3dev/ctfc
114
+ licenses:
115
+ - MIT
116
+ metadata:
117
+ homepage_uri: https://github.com/alx3dev/ctfc
118
+ source_code_uri: https://github.com/alx3dev/ctfc
119
+ bug_tracker_uri: https://github.com/alx3dev/ctfc/issues
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: 3.0.1
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubygems_version: 3.2.32
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Crypto to Fiat currency data gathering
139
+ test_files: []