crates 0.1.0

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.
Files changed (7) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +174 -0
  4. data/bin/crates +32 -0
  5. data/crates.gemspec +33 -0
  6. data/lib/crates.rb +101 -0
  7. metadata +109 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4a5780e22b232f7b5a861327bf82afa1280f2cd8ca5d8a0092b65ba79f7152b0
4
+ data.tar.gz: 1b45f8cb9729d9f4a2a4ba61853c126d0a58d2b24db5b8b98d6862048e2f1d1c
5
+ SHA512:
6
+ metadata.gz: '0408be7c32cdefe1555d1a58dad04744af227990810f5a081d9d0ea3f88bfe84056a0e5d972320884e68be869dcef3df57b521b69a26bf0c30d7d20596474447'
7
+ data.tar.gz: 497f810951e51ea9cd169f1ee7e0da0e5f2c44965da74a20e32aea29af78fc6ea15ad45511f8defcfff2fef0b579b3632e27a3b703b043cb68bf86fd55cdc213
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 decentralizuj
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,174 @@
1
+ # Introduction
2
+ Fast and easy way to get all data about all coins in any fiat value.
3
+ Get data for analyze or use it to make a brand new crypto app.
4
+ After scrapping prices in CSV file, and printing colorized output,
5
+ you can access each rate with `#price(coin)`. No configuration, just run.
6
+
7
+ - Collect prices of defined coins from _cryptocompare.com_
8
+ - Save data into object
9
+ - Save to CSV and print colorized output
10
+ - Print-only without saving
11
+ - Save-only without printing
12
+
13
+ # How to install
14
+
15
+ Make sure you have ruby and git installed:
16
+
17
+ ```bash
18
+ # download repo and install dependencies like colorize and rest-client
19
+ git clone https://github.com/decentralizuj/crates.git
20
+ cd crates
21
+ bundle install
22
+ ```
23
+
24
+ # How to run
25
+
26
+ If you want to edit default coins:
27
+
28
+ ```ruby
29
+ # open 'bin/crates' and edit line(5):
30
+ > (5): COINS = %w[ ... ].freeze
31
+ ```
32
+
33
+ Run from terminal:
34
+
35
+ ```ruby
36
+ # add fiat currencies as arguments
37
+ # also accept `--no-save` and `--no-print` as args
38
+
39
+ $ ruby bin/crates usd eur rsd
40
+ $ ruby bin/crates usd eur rsd --no-save
41
+ $ ruby bin/crates usd eur rsd --no-print
42
+ ```
43
+
44
+ # How to use
45
+
46
+ Initialize new object with your own configuration. If you want to use defaults:
47
+
48
+ ```ruby
49
+ # you can edit this in 'bin/crates'
50
+
51
+ - #coins: [BTC, LTC, XMR, ETH, BCH, ZEC]
52
+ - #save: true
53
+ - #print: true
54
+ ```
55
+
56
+ Otherwise, #new accept 'currency' as argument, and an 'options hash':
57
+
58
+ ```ruby
59
+ - first parameter is currency, defauilt is 'EUR'
60
+ - other accepted options are:
61
+ print: boolean
62
+ save: boolean
63
+ coins: array
64
+ ```
65
+
66
+ Example:
67
+
68
+ ```ruby
69
+ # configure default values
70
+
71
+ COINS = %w[ BTC XMR LTC ETH ZEC ].freeze
72
+ PRINT = true
73
+ SAVE = true
74
+
75
+ # create new object
76
+
77
+ @rates = C::Rates.new( :eur, coins: COINS, print: PRINT, save: SAVE )
78
+
79
+ # make single request
80
+
81
+ @rates.get
82
+
83
+ # C::Rates#get accept same args as #new, but do not change default values
84
+
85
+ CURRENCIES = %w[ USD EUR RSD ].freeze
86
+
87
+ CURRENCIES.each do |currency|
88
+ @rates.get currency
89
+ end
90
+ ```
91
+
92
+ This will print/save data as configured, while making prices easily
93
+ accessible with Rates#price(:symbol).
94
+
95
+ ```ruby
96
+ # Getter method with all coins and values
97
+
98
+ @rates.prices
99
+ # => { "BTC"=>48867.67, "XMR"=>200.31, "LTC"=>164.37 }
100
+ # => Accessible with @rates['BTC']
101
+
102
+
103
+ # Get price for each coin
104
+ # C::Rates#price(:coin)
105
+
106
+ @rates.price(:btc) # accept symbol
107
+ # => 48867.67
108
+
109
+ @rates.price('xmr') # or string
110
+ # => 200.31
111
+ ```
112
+
113
+ C::Rates has two setter methods:
114
+
115
+ ```ruby
116
+ @rates.currency = 'EUR'
117
+
118
+ @rates.coins = %w[BTC XMR LTC]
119
+ ```
120
+
121
+ Other available objects are:
122
+
123
+ ```ruby
124
+ # After new object is initialized, you can use:
125
+
126
+ @rates.currency
127
+ # => "EUR"
128
+
129
+ @rates.coins
130
+ # => "BTC, XMR, LTC"
131
+
132
+ @rates.save?
133
+ # => save output -> (true/false)
134
+
135
+ @rates.print?
136
+ # => print output -> (true/false)
137
+
138
+ @rates.count
139
+ # => 0 -> (number of fail requests)
140
+
141
+ # After you call Rates#get:
142
+
143
+ @rates.url
144
+ # => constructed URL
145
+
146
+ @rates.reponse
147
+ # => response from RestClient.get (accept #code, #headers, #body)
148
+
149
+ @rates.data
150
+ # => JSON parsed object will all data about all coins
151
+
152
+ @rates.table
153
+ # => path to saved CSV file
154
+ # => file is named CURRENCY_rates.csv (eur_rates.csv)
155
+ ```
156
+
157
+ # TO-DO
158
+
159
+ This gem is start of app that I am working on to help me with auto-trades.
160
+ At the moment I use it to notify me when price change more then 2% from last trade.
161
+ Then I perform crypto-to-crypto trade, and wait for price to change again.
162
+ To make it reliable and worth of using, plan is to add next functions:
163
+
164
+ - add more sources to get data from them
165
+ - scrap more data into tables (only data I need)
166
+ - add support for API-KEYS
167
+ - rotate sources if requests are sent too often
168
+ - add support for proxy and headers rotation
169
+ - create charts from prices at given time (each 10min etc)
170
+ - create GIF animation from charts per time (each 30 min etc)
171
+
172
+ # PREVIEW
173
+
174
+ ![GIF](docs/crates.gif)
data/bin/crates ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/crates.rb'
4
+
5
+ COINS = %w[ BTC LTC XMR ETH BCH ZEC ].freeze
6
+
7
+ BANNER = [
8
+ "\n Run C::Rates with fiat currencies as arguments:".cyan,
9
+ " $ ruby bin/crates usd eur rsd".light_green,
10
+ " --no-print".light_green,
11
+ " --no-save".light_green,
12
+ " To edit default coins, open 'bin/crates'\n".cyan
13
+ ]
14
+
15
+ if ARGV.include?('-h') or ARGV.include?('--help')
16
+ puts BANNER
17
+ exit 1
18
+ else
19
+ ARGV.include?('--no-save') ? ns = false : ns = true
20
+ ARGV.include?('--no-print') ? np = false : np = true
21
+ @rates = C::Rates.new(:eur, save: ns, print: np, coins: COINS)
22
+ end
23
+
24
+ unless ARGV.empty?
25
+ ARGV.each do |fiat|
26
+ next if %w[--no-print --no-save].include?(fiat.to_s.downcase)
27
+ @rates.get fiat
28
+ end
29
+ else
30
+ @rates.get :eur, save: false
31
+ end
32
+
data/crates.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'crates'
5
+ s.version = '0.1.0'
6
+ s.summary = 'Print and Save Cryptocurrency Rates'
7
+ s.description = <<~DESC
8
+ Collect cryptocurrency prices for any fiat currency.
9
+ Print colorized output (or not), save as CSV (or not).
10
+ Lightweight, fast, simple and easily extendible.
11
+ DESC
12
+ s.authors = ['decentralizuj']
13
+ s.files = ['lib/crates.rb']
14
+ s.homepage = 'https://github.com/decentralizuj/crates'
15
+ s.license = 'MIT'
16
+
17
+ s.metadata['homepage_uri'] = 'https://github.com/decentralizuj/crates'
18
+ s.metadata['source_code_uri'] = 'https://github.com/decentralizuj/crates'
19
+ s.metadata['bug_tracker_uri'] = 'https://github.com/decentralizuj/crates/issues'
20
+
21
+ s.files = ['bin/crates', 'lib/crates.rb', 'LICENSE', 'README.md', 'crates.gemspec']
22
+ s.bindir = 'bin'
23
+ s.executables = ['crates']
24
+ s.require_paths = ['lib']
25
+
26
+ s.add_runtime_dependency 'rest-client', '~> 2.1.0'
27
+ s.add_runtime_dependency 'colorize', '~> 0.8.1'
28
+
29
+ s.add_development_dependency 'bundler', '~> 2.2.9'
30
+ s.add_development_dependency 'rake', '~> 13.0.3'
31
+ end
32
+
33
+
data/lib/crates.rb ADDED
@@ -0,0 +1,101 @@
1
+ require 'json'
2
+ require 'csv'
3
+ require 'colorize'
4
+ require 'rest-client'
5
+
6
+ module C
7
+
8
+ # set default coins
9
+ COINS = %w[ BTC XMR LTC ETH BCH ZEC ].freeze
10
+ # max number of retries
11
+ MAX_RETRY = 3
12
+ # api base-url
13
+ URL = 'https://min-api.cryptocompare.com/data/pricemultifull?'.freeze
14
+
15
+ class Rates
16
+
17
+ attr_reader :response, :data, :url, :table, :count, :prices
18
+ attr_accessor :currency, :coins
19
+
20
+ def self.get!( currency = :eur, opts = {} )
21
+ @rates = Rates.new currency, opts
22
+ @rates.get
23
+ end
24
+
25
+ def initialize( currency = :eur, opts = {} )
26
+ @save = opts[:save ].nil? ? true : opts[:save]
27
+ @print = opts[:print].nil? ? true : opts[:print]
28
+ @coins = opts[:coins].nil? ? COINS : Array(opts[:coins])
29
+ @currency = currency.to_s.upcase
30
+ end
31
+
32
+ def get( currency = nil, opts = {} )
33
+ @save = opts[:save] unless opts[:save].nil?
34
+ @print = opts[:print] unless opts[:print].nil?
35
+ opts[:coins] ||= @coins
36
+ currency ||= @currency
37
+ @table = currency.to_s.downcase + '_rates.csv'
38
+ @count = 0
39
+ execute_request(opts[:coins], currency)
40
+ end
41
+
42
+ def price( coin )
43
+ @prices[coin.to_s.upcase]
44
+ end
45
+
46
+ def save?
47
+ @save == true
48
+ end
49
+
50
+ def print?
51
+ @print == true
52
+ end
53
+
54
+ private
55
+
56
+ def execute_request( coins = @coins, currency = @currency )
57
+ @prices, @data_array, coin_uri = {}, [], ''
58
+ coins.collect { |coin| coin_uri << "fsyms=#{coin}&" }
59
+ @url = URL + "#{coin_uri}tsyms=#{currency}"
60
+ @response = RestClient.get @url
61
+ @data = JSON.parse @response
62
+ @data_array << Time.now.to_s
63
+ coins.each do |coin|
64
+ @data_array << value = @data["RAW"][coin.to_s.upcase][currency.to_s.upcase]["PRICE"].round(2)
65
+ @prices[coin] = value
66
+ end
67
+ print_data_for(currency) if print?
68
+ save_data(coins) if save?
69
+ rescue
70
+ @count += 1
71
+ if @count >= MAX_RETRY
72
+ puts @response.to_s.split(',')
73
+ exit 1
74
+ else
75
+ execute_request(coins, currency)
76
+ end
77
+ end
78
+
79
+ def print_data_for( currency )
80
+ 30.times { print '='.green } and puts
81
+ puts "[".green + currency.to_s.upcase.yellow.bold + "]".green + " conversion rate"
82
+ 30.times { print '='.green } and puts
83
+ @prices.each do |name, value|
84
+ print "[".yellow.bold + "#{name}".green.bold + "]".yellow.bold
85
+ puts ": #{value}".bold
86
+ end
87
+ end
88
+
89
+ def save_data( coins = nil )
90
+ coins ||= @coins
91
+ create_csv_headers(coins) unless File.exist?(@table)
92
+ CSV.open(@table, 'ab') { |column| column << @data_array }
93
+ end
94
+
95
+ def create_csv_headers( coins = [] )
96
+ header_array = ['TIME']
97
+ coins.each { |coin| header_array << coin }
98
+ CSV.open(@table, "w" ) { |header| header << header_array }
99
+ end
100
+ end
101
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crates
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - decentralizuj
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: colorize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.8.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.2.9
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.2.9
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 13.0.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 13.0.3
69
+ description: "Collect cryptocurrency prices for any fiat currency.\nPrint colorized
70
+ output (or not), save as CSV (or not).\nLightweight, fast, simple and easily extendible.
71
+ \n"
72
+ email:
73
+ executables:
74
+ - crates
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - LICENSE
79
+ - README.md
80
+ - bin/crates
81
+ - crates.gemspec
82
+ - lib/crates.rb
83
+ homepage: https://github.com/decentralizuj/crates
84
+ licenses:
85
+ - MIT
86
+ metadata:
87
+ homepage_uri: https://github.com/decentralizuj/crates
88
+ source_code_uri: https://github.com/decentralizuj/crates
89
+ bug_tracker_uri: https://github.com/decentralizuj/crates/issues
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubygems_version: 3.2.3
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Print and Save Cryptocurrency Rates
109
+ test_files: []