forked from MaG21/curry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
central_back_xls_test.rb
executable file
·60 lines (43 loc) · 1.15 KB
/
central_back_xls_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require 'open-uri'
require 'simple-spreadsheet'
require 'thread'
require 'fileutils'
require 'byebug'
DATA_URI = URI('https://www.bancentral.gov.do/tasas_cambio/TASA_DOLAR_REFERENCIA_MC.XLS')
# yields file
def get_data(url, &block)
mutex = Mutex.new
file_length = 0
opts = {}
# This lambda gets called once. The argument is the
# length of the file as per the HTTP header sent by
# the server.
opts[:content_length_proc] = lambda do|len|
file_length = len
mutex.lock
end
# This lambda could be called multiple times. Each time,
# it passes the actual number of bytes downloaded.
opts[:progress_proc] = lambda do|delta|
mutex.unlock if delta >= file_length
end
open(url, opts) do|file|
# Attempts to grab the lock and waits if it isn't available
# We want to wait if the file isn't fully downloaded.
mutex.lock
block.call(file)
end
end
hsh = nil
get_data(DATA_URI) do|file|
path = "#{file.path}.xls"
FileUtils.mv(file.path, path)
xls = SimpleSpreadsheet::Workbook.read(path)
xls.selected_sheet = xls.sheets.first
row = xls.last_row
hsh = {
buying_rate: xls.cell(row, 4),
selling_rate: xls.cell(row, 5)
}
end
p hsh