nasturtium 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,329 @@
1
+ require_relative "faraday" # !! Potential ruby 3.0 difference in module loading? relative differs from Serrano
2
+ require "faraday/follow_redirects"
3
+ require_relative "utils"
4
+
5
+ module Nasturtium
6
+
7
+ class Request
8
+ attr_accessor :endpoint
9
+ attr_accessor :q
10
+ attr_accessor :verbose
11
+
12
+ attr_accessor :options
13
+
14
+ def initialize(**args)
15
+ @endpoint = args[:endpoint]
16
+ @method = args[:method]
17
+ @verbose = args[:verbose]
18
+ @color = args[:color]
19
+ @style = args[:style]
20
+ @tile_size = args[:tile_size]
21
+ @q = args[:q]
22
+ @admin_level = args[:admin_level]
23
+ @name = args[:name]
24
+ @search_on = args[:search_on]
25
+ @sources = args[:sources]
26
+ @place_id = args[:place_id]
27
+ @preferred_place_id = args[:preferred_place_id]
28
+ @locale = args[:locale]
29
+ @is_active = args[:is_active]
30
+ @taxon_id = args[:taxon_id]
31
+ @observation_taxon_id = args[:observation_taxon_id]
32
+ @parent_id = args[:parent_id]
33
+ @rank = args[:rank]
34
+ @rank_level = args[:rank_level]
35
+ @observation_rank = args[:observation_rank]
36
+ @category = args[:category]
37
+ @id = args[:id]
38
+ @not_id = args[:not_id]
39
+ @id_above = args[:id_above]
40
+ @id_below = args[:id_below]
41
+ @only_id = args[:only_id]
42
+ @all_names = args[:all_names]
43
+ @acc = args[:acc]
44
+ @current = args[:current]
45
+ @current_taxon = args[:current_taxon]
46
+ @own_observation = args[:own_observation]
47
+ @is_change = args[:is_change]
48
+ @captive = args[:captive]
49
+ @endemic = args[:endemic]
50
+ @geo = args[:geo]
51
+ @identified = args[:identified]
52
+ @introduced = args[:introduced]
53
+ @mappable = args[:mappable]
54
+ @native = args[:native]
55
+ @out_of_range = args[:out_of_range]
56
+ @pcid = args[:pcid]
57
+ @photos = args[:photos]
58
+ @popular = args[:popular]
59
+ @sounds = args[:sounds]
60
+ @taxon_active = args[:taxon_active]
61
+ @observation_taxon_active = args[:observation_taxon_active]
62
+ @taxon_is_active = args[:taxon_is_active]
63
+ @threatened = args[:threatened]
64
+ @verifiable = args[:verifiable]
65
+ @licensed = args[:licensed]
66
+ @photo_licensed = args[:photo_licensed]
67
+ @photo_license = args[:photo_license]
68
+ @sound_license = args[:sound_license]
69
+ @ofv_datatype = args[:ofv_datatype]
70
+ @project_id = args[:project_id]
71
+ @project_type = args[:project_type]
72
+ @site_id = args[:site_id]
73
+ @without_taxon_id = args[:without_taxon_id]
74
+ @without_observation_taxon_id = args[:without_observation_taxon_id]
75
+ @taxon_name = args[:taxon_name]
76
+ @user = args[:user]
77
+ @password = args[:password]
78
+ @body = args[:body]
79
+ @authenticity_token = args[:authenticity_token]
80
+ @user_id = args[:user_id]
81
+ @user_login = args[:user_login]
82
+ @login = args[:login]
83
+ @ident_user_id = args[:ident_user_id]
84
+ @day = args[:day]
85
+ @month = args[:month]
86
+ @year = args[:year]
87
+ @term_id = args[:term_id]
88
+ @without_term_id = args[:without_term_id]
89
+ @term_value_id = args[:term_value_id]
90
+ @without_term_value_id = args[:without_term_value_id]
91
+ @acc_below = args[:acc_below]
92
+ @acc_below_or_unknown = args[:acc_below_or_unknown]
93
+ @acc_above = args[:acc_above]
94
+ @before = args[:before]
95
+ @after= args[:after]
96
+ @observed_before = args[:observed_before]
97
+ @observed_on = args[:observed_on]
98
+ @observed_after = args[:observed_after]
99
+ @created_before = args[:created_before]
100
+ @created_on = args[:created_on]
101
+ @created_after = args[:created_after]
102
+ @observation_created_after = args[:observation_created_after]
103
+ @observation_created_before = args[:observation_created_before]
104
+ @unobserved_by_user_id = args[:unobserved_by_user_id]
105
+ @apply_project_rules_for = args[:apply_project_rules_for]
106
+ @conservation_status = args[:conservation_status]
107
+ @conservation_status_authority = args[:conservation_status_authority]
108
+ @conservation_status_iucn = args[:conservation_status_iucn]
109
+ @geoprivacy = args[:geoprivacy]
110
+ @taxon_geoprivacy = args[:taxon_geoprivacy]
111
+ @rank_lowest = args[:rank_lowest]
112
+ @rank_highest = args[:rank_highest]
113
+ @observation_rank_lowest = args[:observation_rank_lowest]
114
+ @observation_rank_highest = args[:observation_rank_highest]
115
+ @iconic_taxa = args[:iconic_taxa]
116
+ @iconic_taxon_id = args[:iconic_taxon_id]
117
+ @observation_iconic_taxon_id = args[:observation_iconic_taxon_id]
118
+ @identifications = args[:identifications]
119
+ @latitude = args[:latitude]
120
+ @longitude = args[:longitude]
121
+ @radius = args[:radius]
122
+ @ne_latitude = args[:ne_latitude]
123
+ @ne_longitude = args[:ne_longitude]
124
+ @sw_latitude = args[:sw_latitude]
125
+ @sw_longitude = args[:sw_longitude]
126
+ @list_id = args[:list_id]
127
+ @not_in_project = args[:not_in_project]
128
+ @not_matching_project_rules_for = args[:not_matching_project_rules_for]
129
+ @quality_grade = args[:quality_grade]
130
+ @updated_since = args[:updated_since]
131
+ @viewer_id = args[:viewer_id]
132
+ @reviewed = args[:reviewed]
133
+ @featured = args[:featured]
134
+ @noteworthy = args[:noteworthy]
135
+ @rule_details = args[:rule_details]
136
+ @type = args[:type]
137
+ @member_id = args[:member_id]
138
+ @has_params = args[:has_params]
139
+ @has_posts = args[:has_posts]
140
+ @ttl = args[:ttl]
141
+ @page = args[:page]
142
+ @per_page = args[:per_page]
143
+ @order = args[:order]
144
+ @order_by = args[:order_by]
145
+ @options = args[:options] # TODO: not added at nasturtium.rb
146
+ @headers = args[:headers]
147
+ @override_base_url = args[:base_url]
148
+ end
149
+
150
+ # TODO: arrays are done like this source[]=users,projects but sometime without the brackets?
151
+ def perform
152
+
153
+ args = {
154
+ color: @color,
155
+ style: @style,
156
+ tile_size: @tile_size,
157
+ q: @q,
158
+ admin_level: @admin_level,
159
+ name: @name,
160
+ search_on: @search_on,
161
+ sources: @sources,
162
+ place_id: @place_id,
163
+ preferred_place_id: @preferred_place_id,
164
+ is_active: @is_active,
165
+ taxon_id: @taxon_id,
166
+ observation_taxon_id: @observation_taxon_id,
167
+ parent_id: @parent_id,
168
+ rank: @rank,
169
+ observation_rank: @observation_rank,
170
+ category: @category,
171
+ rank_level: @rank_level,
172
+ id: @id,
173
+ id_above: @id_above,
174
+ id_below: @id_below,
175
+ only_id: @only_id,
176
+ all_names: @all_names,
177
+ not_id: @not_id,
178
+ acc: @acc,
179
+ current: @current,
180
+ current_taxon: @current_taxon,
181
+ own_observation: @own_observation,
182
+ is_change: @is_change,
183
+ captive: @captive,
184
+ endemic: @endemic,
185
+ geo: @geo,
186
+ identified: @identified,
187
+ introduced: @introduced,
188
+ mappable: @mappable,
189
+ native: @native,
190
+ out_of_range: @out_of_range,
191
+ pcid: @pcid,
192
+ photos: @photos,
193
+ popular: @popular,
194
+ sounds: @sounds,
195
+ taxon_active: @taxon_active,
196
+ observation_taxon_active: @observation_taxon_active,
197
+ taxon_is_active: @taxon_is_active,
198
+ threatened: @threatened,
199
+ verifiable: @verifiable,
200
+ licensed: @licensed,
201
+ photo_licensed: @photo_licensed,
202
+ photo_license: @photo_license,
203
+ sound_license: @sound_license,
204
+ ofv_datatype: @ofv_datatype,
205
+ project_id: @project_id,
206
+ project_type: @project_type,
207
+ site_id: @site_id,
208
+ without_taxon_id: @without_taxon_id,
209
+ without_observation_taxon_id: @without_observation_taxon_id,
210
+ taxon_name: @taxon_name,
211
+ 'user[email]': @user,
212
+ 'user[password]': @password,
213
+ authenticity_token: @authenticity_token,
214
+ user_id: @user_id,
215
+ user_login: @user_login,
216
+ login: @login,
217
+ ident_user_id: @ident_user_id,
218
+ day: @day,
219
+ month: @month,
220
+ year: @year,
221
+ term_id: @term_id,
222
+ without_term_id: @without_term_id,
223
+ term_value_id: @term_value_id,
224
+ without_term_value_id: @without_term_value_id,
225
+ acc_below: @acc_below,
226
+ acc_below_or_unknown: @acc_below_or_unknown,
227
+ acc_above: @acc_above,
228
+ d1: @after,
229
+ d2: @before,
230
+ observed_d1: @observed_after,
231
+ observed_on: @observed_on,
232
+ observed_d2: @observed_before,
233
+ created_on: @created_on,
234
+ created_d1: @created_after,
235
+ created_d2: @created_before,
236
+ observation_created_d1: @observation_created_after,
237
+ observation_created_d2: @observation_created_before,
238
+ unobserved_by_user_id: @unobserved_by_user_id,
239
+ apply_project_rules_for: @apply_project_rules_for,
240
+ cs: @conservation_status,
241
+ csa: @conservation_status_authority,
242
+ csi: @conservation_status_iucn,
243
+ geoprivacy: @geoprivacy,
244
+ taxon_geoprivacy: @taxon_geoprivacy,
245
+ lrank: @rank_lowest,
246
+ hrank: @rank_highest,
247
+ observation_lrank: @observation_rank_lowest,
248
+ observation_hrank: @observation_rank_highest,
249
+ iconic_taxa: @iconic_taxa,
250
+ iconic_taxon_id: @iconic_taxon_id,
251
+ observation_iconic_taxon_id: @observation_iconic_taxon_id,
252
+ identifications: @identifications,
253
+ lat: @latitude,
254
+ lng: @longitude,
255
+ radius: @radius,
256
+ nelat: @ne_latitude,
257
+ nelng: @ne_longitude,
258
+ swlat: @sw_latitude,
259
+ swlng: @sw_longitude,
260
+ list_id: @list_id,
261
+ not_in_project: @not_in_project,
262
+ not_matching_project_rules_for: @not_matching_project_rules_for,
263
+ quality_grade: @quality_grade,
264
+ updated_since: @updated_since,
265
+ viewer_id: @viewer_id,
266
+ reviewed: @reviewed,
267
+ featured: @featured,
268
+ noteworthy: @noteworthy,
269
+ rule_details: @rule_details,
270
+ type: @type,
271
+ member_id: @member_id,
272
+ has_params: @has_params,
273
+ has_posts: @has_posts,
274
+ ttl: @ttl,
275
+ locale: @locale,
276
+ page: @page,
277
+ per_page: @per_page,
278
+ order: @order,
279
+ order_by: @order_by,
280
+ }
281
+ opts = args.delete_if { |_k, v| v.nil? }
282
+
283
+ Faraday::Utils.default_space_encoding = "+"
284
+
285
+ if @override_base_url.nil?
286
+ @base_url = Nasturtium.base_url
287
+ else
288
+ @base_url = @override_base_url
289
+ end
290
+
291
+ conn = if verbose
292
+ Faraday.new(url: @base_url) do |f|
293
+ f.response :logger
294
+ f.use Faraday::NasturtiumErrors::Middleware
295
+ f.adapter Faraday.default_adapter
296
+ end
297
+ else
298
+ Faraday.new(url: @base_url) do |f|
299
+ f.use Faraday::NasturtiumErrors::Middleware
300
+ f.adapter Faraday.default_adapter
301
+ end
302
+ end
303
+
304
+ conn.headers['Accept'] = 'application/json,*/*'
305
+ conn.headers[:user_agent] = make_user_agent
306
+ conn.headers["X-USER-AGENT"] = make_user_agent
307
+
308
+ if @method == "POST"
309
+ puts '11111111111111111111111111111111111'
310
+ conn.headers['Accept'] = 'application/json,text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8'
311
+ conn.headers['Content-Type'] = 'application/x-www-form-urlencoded'
312
+ res = conn.post(endpoint, @body)
313
+ else
314
+ res = conn.get(endpoint, opts)
315
+ end
316
+
317
+ if @headers
318
+ res.headers
319
+ else
320
+ # Handles endpoints that do not return JSON
321
+ begin
322
+ MultiJson.load(res.body)
323
+ rescue MultiJson::ParseError
324
+ res.body
325
+ end
326
+ end
327
+ end
328
+ end
329
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ # From https://raw.githubusercontent.com/sckott/serrano/main/lib/serrano/utils.rb
4
+
5
+ def make_user_agent
6
+ requa = "Faraday/v" + Faraday::VERSION
7
+ habua = "Nasturtium/v" + Nasturtium::VERSION
8
+ ua = requa + " " + habua
9
+ if Nasturtium.mailto
10
+ ua += " (mailto:%s)" % Nasturtium.mailto
11
+ end
12
+ # ua += format(' (mailto:%s)', Nasturtium.mailto) if Nasturtium.mailto
13
+ ua
14
+ end
15
+
16
+ def field_query_handler(x)
17
+ tmp = x.keep_if { |z| z.match(/query_/) }
18
+ rename_query_filters(tmp)
19
+ end
20
+
21
+ def rename_query_filters(foo)
22
+ foo = foo.tostrings
23
+ foo = foo.map { |x, y| [x.to_s.sub("container_title", "container-title"), y] }.to_h
24
+ foo.map { |x, y| [x.to_s.sub("query_", "query."), y] }.to_h
25
+ end
26
+
27
+ class Hash
28
+ def tostrings
29
+ map { |(k, v)| [k.to_s, v] }.to_h
30
+ end
31
+ end
32
+
33
+ class Hash
34
+ def tosymbols
35
+ map { |(k, v)| [k.to_sym, v] }.to_h
36
+ end
37
+ end
38
+
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nasturtium
4
+ VERSION = "0.1.0"
5
+ end