forked from MaG21/curry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurry.rb
150 lines (107 loc) · 2.92 KB
/
curry.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env ruby
# By: MaG
#
# Get port before sinatra
port = ARGV.first =~ /\A\d+\z/ ? ARGV.first.to_i : 8080
require 'bundler/setup'
Bundler.require(:default)
Bundler.require(:production)
Bundler.require(:curry) # This line is important, in case curry is embedded in other project
# with it's own Gemfile (like Rails)
require 'thread'
require_relative 'scraper'
set :port, port
set :bind, '0.0.0.0'
set :environment, :production
# semver
VERSION = '1.5.4'
$mutex = Mutex.new
$info = Scraper::Info.new
$counter = 0
Thread.new do
loop do
sleep 10_800 # update every three ours
info = Scraper::Info.new
$mutex.synchronize do
$info = info
end
end
end
before do
$counter += 1 unless request.path_info =~ /^\/(?:requests)?$/
end
get '/' do
headers({'Content-Type' => 'text/plain'})
send_file 'README.md'
end
get '/rates' do
headers({'Content-Type' => 'application/json'})
$mutex.synchronize do
$info.serialize
end
end
get '/central_bank_rates' do
headers({'Content-Type' => 'application/json'})
$mutex.synchronize do
$info.central_bank.serialize
end
end
get '/requests' do
headers({'Content-Type' => 'text/plain'})
"#{$counter} requests."
end
get '/ncf/:rnc/:ncf' do|rnc, ncf|
agent = Mechanize.new
page = agent.get 'http://www.dgii.gov.do/app/WebApps/Consultas/NCF/ConsultaNCF.aspx'
form = page.form id: 'form1'
rnc.gsub!(/\D/, '')
len = rnc.length
return {valid: false}.to_json unless len == 9 or len == 11
form.txtRNC = rnc
form.txtNCF = ncf
form.add_field! 'btnConsultar'
form.add_field! '__EVENTTARGET'
form.add_field! '__EVENTARGUMENT'
form.add_field! '__LASTFOCUS'
form.btnConsultar = 'Consultar'
ret = form.submit
ret.content =~ /Comprobante Fiscal digitado es v.{1,2}lido./
{valid: !!$&}.to_json
end
get '/rnc/:keyword' do|keyword|
content_type 'application/json'
agent = Mechanize.new
page = agent.get 'http://www.dgii.gov.do/app/WebApps/Consultas/rnc/RncWeb.aspx'
form = page.form name: 'Form1'
form.add_field! '__EVENTTARGET'
form.add_field! '__EVENTARGUMENT'
form.add_field! '__LASTFOCUS'
form.add_field! 'btnBuscaRncCed'
keyword.gsub!(/\D/, '')
len = keyword.length
return {}.to_json unless len == 9 or len == 11
form.txtRncCed = keyword
form.btnBuscaRncCed = 'Buscar'
ret = form.submit
return {}.to_json unless ret
tmp = ret.search '//tr[@class="GridItemStyle"]'
return {}.to_json unless tmp
fields = tmp.children
return {}.to_json if fields.empty?
payload = {
:rnc => fields[1].text.strip,
:name => fields[2].text.strip,
:comercial_name => fields[3].text.strip,
:category => fields[4].text.strip,
:payment_regimen => fields[5].text.strip,
:status => fields[6].text.strip
}
JSON.pretty_generate(payload)
end
not_found do
redirect '/'
end
get '/__sinatra__/*' do
headers({'Content-Type' => 'text/plain'})
'Nope, this a Rails application, or maybe not, dunno.'
end