iucnredlistrb 0.1.0
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/iucnredlistrb/assessment.rb +13 -0
- data/lib/iucnredlistrb/client.rb +66 -0
- data/lib/iucnredlistrb/resource.rb +46 -0
- data/lib/iucnredlistrb/version.rb +5 -0
- data/lib/iucnredlistrb.rb +14 -0
- metadata +53 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: fb22af6fae82248687bf68b0eed02e3ce9d27d3b80c60b39064ce53383073e28
|
|
4
|
+
data.tar.gz: 1f85da64df8bed2d75956a41210cc4c6158f5376d7aee1ac32ece6e322cae46b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b6a45115af90ed96441980cae1914f2e0ef3f1d151aada193745616de3c120ab24fab9728e6a9d5f90f2e4f294e107c4aaf8649146fce45cdaaa3fe6619c865e
|
|
7
|
+
data.tar.gz: 9659b4cd980bbb56bd07a271417f403cedb3ff75d62898e3f9b0d0e3011cba28015db39c031946b51a1c24241931354d84d51c4c80bb269507bff057424daeb6
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IUCNRedListRb
|
|
4
|
+
class Client
|
|
5
|
+
API_BASE = "https://api.iucnredlist.org/api/v4/"
|
|
6
|
+
|
|
7
|
+
RESOURCE_NAMES = %w[
|
|
8
|
+
biogeographical_realms
|
|
9
|
+
comprehensive_groups
|
|
10
|
+
conservation_actions
|
|
11
|
+
countries
|
|
12
|
+
faos
|
|
13
|
+
growth_forms
|
|
14
|
+
habitats
|
|
15
|
+
population_trends
|
|
16
|
+
red_list_categories
|
|
17
|
+
research
|
|
18
|
+
scopes
|
|
19
|
+
stresses
|
|
20
|
+
systems
|
|
21
|
+
threats
|
|
22
|
+
use_and_trade
|
|
23
|
+
].freeze
|
|
24
|
+
|
|
25
|
+
def initialize(api_key:)
|
|
26
|
+
@connection = initialize_connection_with(api_key: api_key)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def assessment
|
|
30
|
+
@assessment ||= IUCNRedListRb::Assessment.new(self)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
RESOURCE_NAMES.each do |resource_name|
|
|
34
|
+
define_method(resource_name) do
|
|
35
|
+
instance_variable_get("@#{resource_name}") ||
|
|
36
|
+
instance_variable_set(
|
|
37
|
+
"@#{resource_name}", IUCNRedListRb::Resource.new(self, resource_name)
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def get(endpoint, query = {})
|
|
43
|
+
response = @connection.get(endpoint, query)
|
|
44
|
+
|
|
45
|
+
if response.success?
|
|
46
|
+
selected_headers = response.headers.select do |key, _|
|
|
47
|
+
%w[total-count total-pages current-page page-items].include?(key)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
response.body.merge(selected_headers)
|
|
51
|
+
else
|
|
52
|
+
"Error! Status: #{response.status} #{response.body}"
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def initialize_connection_with(api_key:)
|
|
59
|
+
Faraday.new(url: API_BASE) do |connection|
|
|
60
|
+
connection.headers["accept"] = "*/*"
|
|
61
|
+
connection.headers["Authorization"] = api_key
|
|
62
|
+
connection.response :json
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IUCNRedListRb
|
|
4
|
+
class Resource
|
|
5
|
+
def initialize(client, endpoint)
|
|
6
|
+
@client = client
|
|
7
|
+
@endpoint = endpoint
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def list
|
|
11
|
+
@client.get(@endpoint)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def find(id, query = {})
|
|
15
|
+
@client.get("#{@endpoint}/#{id}", query)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def all(id, query = {}, show_progress: false, wait_time: 0.5)
|
|
19
|
+
all_responses = find(id, query)
|
|
20
|
+
total_pages = all_responses["total-pages"].to_i
|
|
21
|
+
|
|
22
|
+
return all_responses if total_pages == 1
|
|
23
|
+
|
|
24
|
+
(2..total_pages).each do |page|
|
|
25
|
+
show_progress_for(page, total_pages) if show_progress
|
|
26
|
+
|
|
27
|
+
sleep wait_time
|
|
28
|
+
paged_response = find(id, query.merge(page: page))
|
|
29
|
+
all_responses["assessments"] += paged_response["assessments"]
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
all_responses
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def show_progress_for(page, total_pages)
|
|
38
|
+
percent = (page / total_pages.to_f) * 100
|
|
39
|
+
bar_length = 30
|
|
40
|
+
filled_length = (percent / (100.0 / bar_length)).round
|
|
41
|
+
bar = "█" * filled_length + "-" * (bar_length - filled_length)
|
|
42
|
+
|
|
43
|
+
print "\r[#{bar}] #{percent.round(1).clamp(0..100)}% Complete "
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "faraday"
|
|
5
|
+
require "json"
|
|
6
|
+
|
|
7
|
+
require_relative "iucnredlistrb/assessment"
|
|
8
|
+
require_relative "iucnredlistrb/client"
|
|
9
|
+
require_relative "iucnredlistrb/resource"
|
|
10
|
+
require_relative "iucnredlistrb/version"
|
|
11
|
+
|
|
12
|
+
module IUCNRedListRb
|
|
13
|
+
class Error < StandardError; end
|
|
14
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: iucnredlistrb
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Ian Townsend
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-03-28 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: This gem provides a simple interface to interact with the IUCN Red List
|
|
14
|
+
API https://api.iucnredlist.org/.
|
|
15
|
+
email:
|
|
16
|
+
- conservation.informatics@iucn.org
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- lib/iucnredlistrb.rb
|
|
22
|
+
- lib/iucnredlistrb/assessment.rb
|
|
23
|
+
- lib/iucnredlistrb/client.rb
|
|
24
|
+
- lib/iucnredlistrb/resource.rb
|
|
25
|
+
- lib/iucnredlistrb/version.rb
|
|
26
|
+
homepage: https://github.com/IUCN-UK/iucnredlistrb
|
|
27
|
+
licenses:
|
|
28
|
+
- MIT
|
|
29
|
+
metadata:
|
|
30
|
+
allowed_push_host: https://rubygems.org
|
|
31
|
+
homepage_uri: https://github.com/IUCN-UK/iucnredlistrb
|
|
32
|
+
source_code_uri: https://github.com/IUCN-UK/iucnredlistrb
|
|
33
|
+
changelog_uri: https://github.com/IUCN-UK/iucnredlistrb/blob/main/CHANGELOG.md
|
|
34
|
+
post_install_message:
|
|
35
|
+
rdoc_options: []
|
|
36
|
+
require_paths:
|
|
37
|
+
- lib
|
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: 3.0.0
|
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
requirements: []
|
|
49
|
+
rubygems_version: 3.5.16
|
|
50
|
+
signing_key:
|
|
51
|
+
specification_version: 4
|
|
52
|
+
summary: A Ruby Gem that wraps the IUCN Red List API for fetching assessment data.
|
|
53
|
+
test_files: []
|