generated from datauy/Plantilla--NombreDeProyecto-Version-backSiAplica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inumet.rb
119 lines (106 loc) · 2.8 KB
/
inumet.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
# inumet.rb
require 'net/http'
require 'json'
class Inumet
@token = ''
#
def initialize(user, pass)
@token = self.login(user, pass)
raise "Usuario o password inválidos" if @token.nil?
p "Inumet - User logged in"
end
#login
def login(user, pass)
uri = URI('https://api.inumet.gub.uy/sesiones/login')
req = Net::HTTP::Post.new(uri)
req.content_type = 'application/json'
req['accept'] = 'text/plain'
req.body = {
'userId' => user,
'password' => pass
}.to_json
req_options = {
use_ssl: uri.scheme == 'https'
}
res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(req)
end
if res.is_a?(Net::HTTPSuccess)
return res.body
end
return
end
#check session
#ERROR DA ERROR
def check_session
uri = URI('https://api.inumet.gub.uy/sesiones/sesion_activa')
req = Net::HTTP::Get.new(uri)
req['accept'] = '*/*'
req['x-access-token'] = @token
req_options = {
use_ssl: uri.scheme == 'https'
}
res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(req)
end
end
#Get estaciones
def get_estaciones
uri = URI('https://api.inumet.gub.uy/estaciones')
req = Net::HTTP::Get.new(uri)
req['accept'] = 'application/json'
req['x-access-token'] = @token
req_options = {
use_ssl: uri.scheme == 'https'
}
res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(req)
end
if res.is_a?(Net::HTTPSuccess)
return res.body
end
return
end
#Get variables
def get_variables
uri = URI('https://api.inumet.gub.uy/variables')
req = Net::HTTP::Get.new(uri)
req['accept'] = 'application/json'
req['x-access-token'] = @token
req_options = {
use_ssl: uri.scheme == 'https'
}
res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(req)
end
if res.is_a?(Net::HTTPSuccess)
return res.body
end
return
end
#{ "mensaje": "No tiene permisos para acceder a '/alertas_incendios/'"}
def get_datos(estaciones, variables, from, to)
uri = URI('https://api.inumet.gub.uy/datos')
params = {
:fechaDesde => from, #'2023-01-01 00:00',
:fechaHasta => to,
:idsEstaciones => estaciones,#['159', '39'],
:idsVariables => variables,#['159', '39'],
}
p params.inspect
uri.query = URI.encode_www_form(params)
req = Net::HTTP::Get.new(uri)
req['accept'] = 'application/json'
req['x-access-token'] = @token
req_options = {
use_ssl: uri.scheme == 'https'
}
res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(req)
end
if res.is_a?(Net::HTTPSuccess)
return res.body
end
return
end
end