generated from datauy/Plantilla--NombreDeProyecto-Version-backSiAplica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatalogo.rb
221 lines (196 loc) · 5.94 KB
/
catalogo.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# catalogo.rb
require 'net/http'
require 'openssl'
require 'json'
class Catalogo
URL = 'https://test.catalogodatos.gub.uy'
@apiKey = ''
#
def initialize(api_key)
@apiKey = api_key
end
# Crear conjunto de datos
def package_create
uri = URI("#{URL}/api/3/action/package_create")
req = Net::HTTP::Post.new(uri)
req.content_type = 'application/x-www-form-urlencoded'
req['Authorization'] = @apiKey
r_parameters = {
name: "pruebacreardatasetapicurl",
title: "Prueba crear dataset API CURL",
private: "true",
author: "Catálogo Datos",
author_email: "[email protected]",
maintainer: "Catálogo Datos",
maintainer_email: "[email protected]",
type: "dataset",
license_id: "odc-uy",
notes: "Prueba crear dataset NOTAS CURL",
version: "2.0",
tag_string: "Ancap, Transparencia Activa",
owner_org: "ancap",
update_frequency: "-1",
groups: [{ name: "transparencia"}]
}
req.body = r_parameters.to_json
req_options = {
use_ssl: uri.scheme == 'https',
verify_mode: OpenSSL::SSL::VERIFY_NONE
}
res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(req)
end
end
# Actualizar conjunto de datos
def package_update
uri = URI("#{URL}/api/3/action/package_update")
req = Net::HTTP::Post.new(uri)
req.content_type = 'application/x-www-form-urlencoded'
req['Authorization'] = @apiKey
req.body = "{\n \"id\": \"pruebacreardatasetapicurl\",\n \"title\": \"Prueba actualizar dataset API CURL\",\n \"private\": \"false\",\n \"author\": \"Catálogo Datos\",\n \"author_email\": \"[email protected]\",\n \"maintainer\": \"Catálogo Datos\",\n \"maintainer_email\": \"[email protected]\",\n \"notes\": \"Prueba crear dataset NOTAS CURL\",\n \"version\": \"2.1\"\n }\n"
req_options = {
use_ssl: uri.scheme == 'https',
verify_mode: OpenSSL::SSL::VERIFY_NONE
}
res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(req)
end
end
# Eliminar recurso
def package_delete
uri = URI("#{URL}/api/3/action/package_delete")
req = Net::HTTP::Post.new(uri)
req['Authorization'] = @apiKey
req.set_form(
[
[
'id',
'pruebacreardatasetapicurl'
]
],
'multipart/form-data'
)
req_options = {
use_ssl: uri.scheme == 'https',
verify_mode: OpenSSL::SSL::VERIFY_NONE
}
res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(req)
end
end
# Obtener conjunto de datos
def package_show
uri = URI("#{URL}/api/3/action/package_show")
params = {
:id => 'pruebacreardatasetapicurl',
}
uri.query = URI.encode_www_form(params)
req = Net::HTTP::Get.new(uri)
req['Authorization'] = @apiKey
req_options = {
use_ssl: uri.scheme == 'https',
verify_mode: OpenSSL::SSL::VERIFY_NONE
}
res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(req)
end
end
# Crear recurso con URL
def resource_create
uri = URI("#{URL}/api/3/action/resource_create")
req = Net::HTTP::Post.new(uri)
req.content_type = 'application/x-www-form-urlencoded'
req['Authorization'] = @apiKey
req.body = "{\n \"package_id\": \"pruebacreardatasetapicurl\",\n \"name\": \"Recurso URL creado vía API CURL\",\n \"url\": \"https://people.sc.fsu.edu/~jburkardt/data/csv/cities.csv\",\n \"description\": \"Descripción de URL cities CURL\",\n \"format\": \"CSV\"\n }\n"
req_options = {
use_ssl: uri.scheme == 'https',
verify_mode: OpenSSL::SSL::VERIFY_NONE
}
res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(req)
end
end
# Crear recurso adjuntando archivo
def resource_create
uri = URI("#{URL}/api/3/action/resource_create")
req = Net::HTTP::Post.new(uri)
req['Authorization'] = @apiKey
req.set_form(
[
[
'package_id',
'pruebacreardatasetapicurl'
],
[
'upload',
File.open('airtravel.csv')
],
[
'format',
'CSV'
],
[
'name',
'Recurso file creado vía API CURL'
],
[
'description',
'Descripción de recurso air travel'
]
],
'multipart/form-data'
)
req_options = {
use_ssl: uri.scheme == 'https',
verify_mode: OpenSSL::SSL::VERIFY_NONE
}
res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(req)
end
end
# Actualizar recurso adjuntando archivo
def resource_update(id, format, file)
uri = URI("#{URL}/api/3/action/resource_update")
req = Net::HTTP::Post.new(uri)
req['Authorization'] = @apiKey
req.set_form(
[
[ 'format', format ],
[ 'name', "Recurso #{id} actualizado vía API CURL" ],
[ 'description', 'Descripción de recurso country' ],
[ 'id', id ],
[ 'upload', file ]
],
'multipart/form-data'
)
req_options = {
use_ssl: uri.scheme == 'https',
verify_mode: OpenSSL::SSL::VERIFY_NONE
}
res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(req)
end
end
# Eliminar recurso
def resource_delete
uri = URI("#{URL}/api/3/action/resource_delete")
req = Net::HTTP::Post.new(uri)
req['Authorization'] = @apiKey
req.set_form(
[
[
'id',
File.open('ID_RESOURCE>')
]
],
'multipart/form-data'
)
req_options = {
use_ssl: uri.scheme == 'https',
verify_mode: OpenSSL::SSL::VERIFY_NONE
}
res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(req)
end
end
end