coinquery 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 758496fcb32b9afca80bfefaa6b2f5c08453bd608048ddb0692465461c403afe
4
- data.tar.gz: 874d762332465a5aacd1819cb3dcc21f3e0e6e995ce7931a931ee3f53ccb2221
3
+ metadata.gz: af2b23cb6aab30532b99a66db98e5425da892c1989219eaf25099582971beb4c
4
+ data.tar.gz: eae541068ad3050c2dda702977d51cd1ec7dbcaa7e642352448163b76fd459f4
5
5
  SHA512:
6
- metadata.gz: 85b0a5363cd75c308e3613910aab22f15a8a13f584482a10dff27da2863200c038f88af1428b056103105b171a6da525db7bc77a6c830ed4c70b66c47a00e845
7
- data.tar.gz: 3eaa803951290bba188013a2b5f6c4c963b16de0aa020ec8c2caa0fb19fa50d3588bba46290a4944365505e2be525d16f40349fd1cef06a7e02f912c6e25463e
6
+ metadata.gz: 726587aa2b8da2869e3ed6cc59dc6028a27cbcd449a825041c7d211e0ce31d8c0c99619039f912b4f326cc196028833bb8600eae17f27745fc4157cd93201dc5
7
+ data.tar.gz: 7f308647d75cf2d26083377be5376a2e46663f59d2a9d7593bc490cdc4f8204140933a01ae44d804e3189dfa25649838cf1ee7c124be020d1c01fbf82d5c8073
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/coinquery.rb CHANGED
@@ -8,6 +8,8 @@ require 'json'
8
8
  require 'excon'
9
9
  require 'unichron'
10
10
  require 'did_you_mean'
11
+ require 'recordx_sqlite'
12
+
11
13
 
12
14
 
13
15
  class CoinQueryException < Exception
@@ -18,9 +20,11 @@ class CoinQuery
18
20
 
19
21
  attr_reader :list
20
22
 
21
- def initialize(autofind: true, dym: true, debug: false)
23
+ def initialize(autofind: true, dym: true, timeout: 5, filepath: '.',
24
+ debug: false)
22
25
 
23
- @autofind, @dym, @debug = autofind, dym, debug
26
+ @autofind, @dym, @timeout, @debug = autofind, dym, timeout, debug
27
+ @filepath = filepath
24
28
 
25
29
  @url_base = 'https://api.coingecko.com/api/v3/'
26
30
  r = ping()
@@ -54,14 +58,14 @@ class CoinQuery
54
58
 
55
59
  if not File.exists? file then
56
60
 
57
- File.open(file, 'w+') do |f|
61
+ File.open(File.join(@filepath, file), 'w+') do |f|
58
62
  Marshal.dump([@list, @dym], f)
59
63
  end
60
64
 
61
65
  else
62
66
 
63
67
  puts ('loading coins list ...').info
64
- File.open(file) do |f|
68
+ File.open(File.join(@filepath, file)) do |f|
65
69
  @list, @dym = Marshal.load(f)
66
70
  end
67
71
 
@@ -69,7 +73,33 @@ class CoinQuery
69
73
 
70
74
  end
71
75
  end
76
+
77
+ @table = RecordxSqlite.new(File.join(@filepath, 'coinquery.db'),
78
+ table: {coins: {id: '', cname: '', price: '', date: 0}})
79
+
72
80
 
81
+ end
82
+
83
+ # archives the cryptocurrency prices in a local sqlite database
84
+ # note: intended for archiving prices on a daily basis
85
+ #
86
+ def archive(limit: 250)
87
+
88
+ puts 'archive: fetching coins ...'.info
89
+ a = coins(limit: limit)
90
+
91
+ puts 'archive: saving to database ...'.info
92
+
93
+ a.each do |coin|
94
+
95
+ uid = coin['id'] + Date.today.to_time.to_i.to_s
96
+ @table.create id: uid.to_s, cname: coin['name'],
97
+ price: coin['current_price'].to_s, date: Date.today.to_time.to_i
98
+
99
+ end
100
+
101
+ puts 'archive: completed'.info
102
+
73
103
  end
74
104
 
75
105
  # lists the top coins (limited to 5 by default)
@@ -84,6 +114,45 @@ class CoinQuery
84
114
  def coins_list
85
115
  @list
86
116
  end
117
+
118
+ def find_coin(coin_name)
119
+
120
+ return coin_name unless @autofind
121
+
122
+ s = coin_name.to_s.downcase
123
+ puts 's: ' + s.inspect if @debug
124
+ r = @list.find {|coin| coin['symbol'].downcase == s || coin['name'].downcase == s}
125
+ puts 'r: ' + r.inspect if @debug
126
+
127
+ if r.nil? then
128
+
129
+ if @dym then
130
+
131
+ suggestion = @dym.correct coin_name
132
+ raise CoinQueryException, "unknown coin or token name. \n" \
133
+ + "Did you mean %s?" % [suggestion.first]
134
+
135
+ else
136
+
137
+ raise CoinQueryException, "unknown coin or token name."
138
+
139
+ end
140
+
141
+ end
142
+
143
+ r
144
+
145
+ end
146
+
147
+ def find_id(name)
148
+ r = find_coin(name)
149
+ r['id']
150
+ end
151
+
152
+ def find_name(s)
153
+ r = find_coin s
154
+ r['name']
155
+ end
87
156
 
88
157
  # returns the price of a coin for a given historical date
89
158
  # e.g. historical_price('Bitcoin', '01-05-2021')
@@ -121,6 +190,23 @@ class CoinQuery
121
190
  val < 1 ? val : val.round(2)
122
191
  end
123
192
  end
193
+
194
+ # use the database archive to query the historical price of a coin
195
+ # e.g. query_archive :btc, '1 May 2021'
196
+ #
197
+ def query_archive(coin_name, rawdate)
198
+
199
+ uc = Unichron.new(rawdate.to_s, :little_endian)
200
+ raise 'invalid date' unless uc.valid?
201
+ date = uc.to_date
202
+
203
+ coin_id = find_id coin_name
204
+
205
+ id = coin_id + date.to_time.to_i.to_s
206
+ r = @table.query "select * from coins where id == '#{id}'"
207
+ r.first if r
208
+
209
+ end
124
210
 
125
211
 
126
212
  private
@@ -128,7 +214,7 @@ class CoinQuery
128
214
  def api_call(api_request)
129
215
 
130
216
  begin
131
- Timeout::timeout(3){
217
+ Timeout::timeout(@timeout){
132
218
  response = Excon.get(@url_base + api_request)
133
219
  JSON.parse(response.body)
134
220
  }
@@ -139,35 +225,5 @@ class CoinQuery
139
225
  end
140
226
 
141
227
  end
142
-
143
- def find_id(coin_name)
144
-
145
- return coin_name unless @autofind
146
-
147
- s = coin_name.to_s.downcase
148
- puts 's: ' + s.inspect if @debug
149
- r = @list.find {|coin| coin['symbol'].downcase == s || coin['name'].downcase == s}
150
- puts 'r: ' + r.inspect if @debug
151
-
152
- if r.nil? then
153
-
154
- if @dym then
155
-
156
- suggestion = @dym.correct coin_name
157
- raise CoinQueryException, "unknown coin or token name. \n" \
158
- + "Did you mean %s?" % [suggestion.first]
159
-
160
- else
161
-
162
- raise CoinQueryException, "unknown coin or token name."
163
-
164
- end
165
-
166
- end
167
-
168
- r['id']
169
-
170
- end
171
-
228
+
172
229
  end
173
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coinquery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -77,6 +77,26 @@ dependencies:
77
77
  - - ">="
78
78
  - !ruby/object:Gem::Version
79
79
  version: 0.3.4
80
+ - !ruby/object:Gem::Dependency
81
+ name: recordx_sqlite
82
+ requirement: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - "~>"
85
+ - !ruby/object:Gem::Version
86
+ version: '0.3'
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 0.3.3
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.3'
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: 0.3.3
80
100
  description:
81
101
  email: digital.robertson@gmail.com
82
102
  executables: []
metadata.gz.sig CHANGED
Binary file