ru_exchange 0.0.5
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 +7 -0
- data/lib/ru_exchange.rb +237 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0e89cd13c6696ba063f78e2ac659e7061a43ed86
|
4
|
+
data.tar.gz: 401c55378c66d674f5090c8a55f438058415bea4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d92d0daa866eb9df7cf955d2fc9c8a5f5baf8e2272c2ebd2cbfc220ee2300fcae43f78c7d7ef68a40463e6405577d4fe54b372ad0538d598c604d389aa5c9ab0
|
7
|
+
data.tar.gz: a844d040a13b0ee5a80ddd7bb25d5e7bf00f0b1a5a795c151ad71f362183b4192a3b5a80a10668f6b07a87db54700580b907fbe30ed9642758c0089c62062411
|
data/lib/ru_exchange.rb
ADDED
@@ -0,0 +1,237 @@
|
|
1
|
+
class RuExchange
|
2
|
+
require 'logger'
|
3
|
+
require 'nokogiri'
|
4
|
+
require 'open-uri'
|
5
|
+
|
6
|
+
def self.update_currencies
|
7
|
+
RuExchange.log "Updating currency. Time: " + Time.now.strftime('%d.%m.%Y %H:%M')
|
8
|
+
|
9
|
+
cur = Dashboard::Currency.find_or_create_by(id: 1)
|
10
|
+
begin
|
11
|
+
cur_new = Dashboard::Currency.where('created_at >= ?', 24.hours.ago).first || Dashboard::Currency.new
|
12
|
+
rescue ActiveRecord::RecordNotUnique
|
13
|
+
retry
|
14
|
+
end
|
15
|
+
|
16
|
+
cur_new = Dashboard::Currency.new if (0..3).include?(Time.now.strftime('%H').to_i)
|
17
|
+
RuExchange.log 'Currency is new record: ' + cur_new.new_record?.to_s
|
18
|
+
RuExchange.log ''
|
19
|
+
|
20
|
+
# update_rur = false
|
21
|
+
# update_uah = false
|
22
|
+
# if cur_new.uah.nil?
|
23
|
+
# update_uah = true
|
24
|
+
# end
|
25
|
+
# if cur_new.rur.nil?
|
26
|
+
# update_rur = true
|
27
|
+
# end
|
28
|
+
|
29
|
+
if true
|
30
|
+
update_uah = true
|
31
|
+
update_rur = true
|
32
|
+
end
|
33
|
+
|
34
|
+
begin
|
35
|
+
|
36
|
+
if update_rur && result_rur = RuExchange.get_rur
|
37
|
+
cur.rur = result_rur
|
38
|
+
cur.rur_changed_at = Time.now
|
39
|
+
|
40
|
+
pack_rur = Dashboard::Package.where(rur_auto: true)
|
41
|
+
RuExchange.log 'Preparing to update package prices in rur'
|
42
|
+
pack_rur.each do |pack|
|
43
|
+
unless pack.usd.nil?
|
44
|
+
pack.rur = Dashboard::Currency.auto_price(pack.usd.to_f * result_rur.to_f)
|
45
|
+
pack.rur_changed_at = Time.now
|
46
|
+
pack.rur_changed_by = -1
|
47
|
+
puts pack.errors.inspect unless pack.save
|
48
|
+
|
49
|
+
RuExchange.log 'Package: ' + pack.name
|
50
|
+
RuExchange.log 'Package USD: ' + pack.usd.to_s
|
51
|
+
RuExchange.log "Math RUR: #{pack.usd.to_s} * #{result_rur.to_s} = #{pack.usd.to_f * result_rur.to_f}"
|
52
|
+
RuExchange.log 'Package RUR AutoPrice: ' + pack.rur.to_s
|
53
|
+
RuExchange.log '------'
|
54
|
+
|
55
|
+
r_c = Dashboard::ReportCurrency.new
|
56
|
+
r_c.package_id = pack.id
|
57
|
+
r_c.currency = 'RUR'
|
58
|
+
r_c.ex_rate = result_rur.to_f
|
59
|
+
r_c.price_in_usd = pack.usd
|
60
|
+
r_c.price_in_curr = pack.usd.to_f * result_rur.to_f
|
61
|
+
r_c.auto_price = Dashboard::Currency.auto_price(pack.usd.to_f * result_rur.to_f)
|
62
|
+
r_c.error = nil
|
63
|
+
r_c.status = 1
|
64
|
+
r_c.save
|
65
|
+
else
|
66
|
+
RuExchange.log 'No USD set for package'
|
67
|
+
r_c = Dashboard::ReportCurrency.new
|
68
|
+
r_c.error = ['custom' => 'Не задана цена в долларах'].to_json
|
69
|
+
r_c.status = 3
|
70
|
+
r_c.currency = 'RUR'
|
71
|
+
r_c.package_id = pack.id
|
72
|
+
r_c.ex_rate = result_rur.to_f
|
73
|
+
r_c.save
|
74
|
+
end
|
75
|
+
end
|
76
|
+
else
|
77
|
+
RuExchange.log 'Not new record. Nothing to save'
|
78
|
+
end
|
79
|
+
if cur_new.rur_changed_at.nil?
|
80
|
+
cur_new.rur = result_rur
|
81
|
+
cur_new.rur_changed_at = Time.now
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
rescue => exception
|
86
|
+
RuExchange.log exception.class.to_s
|
87
|
+
RuExchange.log exception.to_s
|
88
|
+
RuExchange.log exception.backtrace.join("\n")
|
89
|
+
|
90
|
+
r_c = Dashboard::ReportCurrency.new
|
91
|
+
r_c.error = [exception.to_s => exception.backtrace.join("\n")].to_json
|
92
|
+
r_c.status = 2
|
93
|
+
puts r_c.errors.inspect unless r_c.save
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
RuExchange.log ''
|
98
|
+
|
99
|
+
begin
|
100
|
+
# puts m
|
101
|
+
# if cur.uah_changed_at < 24.hours.ago
|
102
|
+
if update_uah && result_uah = RuExchange.get_uah
|
103
|
+
cur.uah = result_uah
|
104
|
+
cur.uah_changed_at = Time.now
|
105
|
+
|
106
|
+
pack_uah = Dashboard::Package.where(uah_auto: true)
|
107
|
+
RuExchange.log 'Preparing to update package prices in uah'
|
108
|
+
pack_uah.each do |pack|
|
109
|
+
unless pack.usd.nil?
|
110
|
+
pack.uah = Dashboard::Currency.auto_price(pack.usd.to_f * result_uah.to_f)
|
111
|
+
pack.uah_changed_at = Time.now
|
112
|
+
pack.uah_changed_by = -1
|
113
|
+
pack.save
|
114
|
+
RuExchange.log 'Package: ' + pack.name
|
115
|
+
RuExchange.log 'Package USD: ' + pack.usd.to_s
|
116
|
+
RuExchange.log "Math UAH: #{pack.usd.to_s} * #{result_uah.to_s} = #{pack.usd.to_f * result_uah.to_f}"
|
117
|
+
RuExchange.log 'Package UAH AutoPrice: ' + pack.uah.to_s
|
118
|
+
RuExchange.log '------'
|
119
|
+
|
120
|
+
r_c = Dashboard::ReportCurrency.new
|
121
|
+
r_c.package_id = pack.id
|
122
|
+
r_c.currency = 'UAH'
|
123
|
+
r_c.ex_rate = result_uah.to_f
|
124
|
+
r_c.price_in_usd = pack.usd
|
125
|
+
r_c.price_in_curr = pack.usd.to_f * result_uah.to_f
|
126
|
+
r_c.auto_price = Dashboard::Currency.auto_price(pack.usd.to_f * result_uah.to_f)
|
127
|
+
r_c.error = nil
|
128
|
+
r_c.status = 1
|
129
|
+
r_c.save
|
130
|
+
else
|
131
|
+
RuExchange.log 'No USD set for package'
|
132
|
+
r_c = Dashboard::ReportCurrency.new
|
133
|
+
r_c.error = ['custom' => 'Не задана цена в долларах'].to_json
|
134
|
+
r_c.status = 3
|
135
|
+
r_c.currency = 'UAH'
|
136
|
+
r_c.package_id = pack.id
|
137
|
+
r_c.ex_rate = result_uah.to_f
|
138
|
+
r_c.save
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
if cur_new.uah_changed_at.nil?
|
143
|
+
cur_new.uah = result_uah
|
144
|
+
cur_new.uah_changed_at = Time.now
|
145
|
+
end
|
146
|
+
rescue => exception
|
147
|
+
RuExchange.log exception.class.to_s
|
148
|
+
RuExchange.log exception.to_s
|
149
|
+
RuExchange.log exception.backtrace.join("\n")
|
150
|
+
end
|
151
|
+
|
152
|
+
begin
|
153
|
+
if cur.save
|
154
|
+
RuExchange.log 'First currency updated!'
|
155
|
+
else
|
156
|
+
RuExchange.log '--- ERROR First currency not updated!'
|
157
|
+
end
|
158
|
+
if cur_new.save
|
159
|
+
RuExchange.log 'Current currency updated!'
|
160
|
+
RuExchange.log 'RUR: ' + result_rur.to_s
|
161
|
+
RuExchange.log 'UAH: ' + result_uah.to_s
|
162
|
+
else
|
163
|
+
RuExchange.log '--- ERROR Current currency not updated!'
|
164
|
+
end
|
165
|
+
|
166
|
+
rescue => exception
|
167
|
+
RuExchange.log exception.class.to_s
|
168
|
+
RuExchange.log exception.to_s
|
169
|
+
RuExchange.log exception.backtrace.join("\n")
|
170
|
+
r_c = Dashboard::ReportCurrency.new
|
171
|
+
r_c.error = [exception.to_s => exception.backtrace.join("\n")].to_json
|
172
|
+
r_c.status = 2
|
173
|
+
r_c.save
|
174
|
+
end
|
175
|
+
|
176
|
+
RuExchange.log 'FINISHED'
|
177
|
+
RuExchange.log ''
|
178
|
+
end
|
179
|
+
|
180
|
+
|
181
|
+
def self.auto_price(price = 999)
|
182
|
+
# Auto generating price example
|
183
|
+
price = price.to_f
|
184
|
+
if price < 1000
|
185
|
+
result = ( (price / 100).round(1) * 100 - 1 ).round.to_i
|
186
|
+
else
|
187
|
+
result = ( (price / 1000).round(1) * 1000 - 10 ).round.to_i
|
188
|
+
end
|
189
|
+
return result
|
190
|
+
end
|
191
|
+
|
192
|
+
|
193
|
+
def self.get_rur
|
194
|
+
begin
|
195
|
+
url = "http://quote.rbc.ru/cash/averagerates.html"
|
196
|
+
RuExchange.log 'Trying to parce site in rur'
|
197
|
+
doc = Nokogiri::HTML(open(url))
|
198
|
+
result = doc.search('div.stats__td').last.text
|
199
|
+
rescue => e
|
200
|
+
RuExchange.log("Couldn't get current rur exchange rate. Error in parcing site.")
|
201
|
+
RuExchange.log(e)
|
202
|
+
end
|
203
|
+
return result
|
204
|
+
end
|
205
|
+
|
206
|
+
def self.get_uah
|
207
|
+
begin
|
208
|
+
url = "http://finance.i.ua/usd/"
|
209
|
+
doc = Nokogiri::HTML(open(url))
|
210
|
+
z = doc.at_css('div.Right div.block_gamma_dark table.local_table')
|
211
|
+
k = z.css('tr')[1]
|
212
|
+
result = k.css('td')[2].css('big').text
|
213
|
+
RuExchange.log 'Trying to parce site in uah'
|
214
|
+
rescue => e
|
215
|
+
RuExchange.log("Couldn't get current uah exchange rate. Error in parcing site.")
|
216
|
+
RuExchange.log(e)
|
217
|
+
end
|
218
|
+
return result
|
219
|
+
end
|
220
|
+
|
221
|
+
def self.logger_file=(file)
|
222
|
+
@logger_file = file
|
223
|
+
end
|
224
|
+
|
225
|
+
|
226
|
+
private
|
227
|
+
def self.log(text)
|
228
|
+
if @logger_file
|
229
|
+
@logger ||= Logger.new(@logger_file)
|
230
|
+
@logger.datetime_format ||= '%d.%m %H:%M:%S'
|
231
|
+
@logger.info text
|
232
|
+
puts text
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
|
237
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ru_exchange
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dmytro Koval
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Get current exchange rate of russian rubles and ukrainian chryvnas
|
14
|
+
email: dawidofdk@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/ru_exchange.rb
|
20
|
+
homepage: https://github.com/dawidof/ru_exchange
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.5.1
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Get current exchange rates
|
44
|
+
test_files: []
|
45
|
+
has_rdoc:
|