rest_countries 1.0.0 → 1.0.1
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 +4 -4
- data/README.md +2 -0
- data/lib/rest_countries/client.rb +27 -8
- data/lib/rest_countries/error.rb +3 -0
- data/lib/rest_countries/version.rb +1 -1
- data/rest_countries-1.0.0.gem +0 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7728ecd48316ab130211f869f6c55819e2ead6797b1a471ef6f77391c3cc8737
|
4
|
+
data.tar.gz: 27dbd592a4026c441a218e91516e9741a0f8f319b2caadb348d29efa60cb8739
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 670b1debe8d681879f9e0e15057b2071579a3686393950e70a0b2eec7bb68deb058b507a5a27164cdab3c628bea738bee02ca384eb8a554be60ac740b311c041
|
7
|
+
data.tar.gz: beb1d2e14ced9b4a33322cce52f1efaf27f4c644baeb42c7fb6a4f7336a8383e2ed7579e8afdd261f9f8a3fb073c0305709792c5f8809c685a2dcabe2d5afed8
|
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require "faraday"
|
2
|
-
|
2
|
+
require "uri"
|
3
3
|
|
4
4
|
module RestCountries
|
5
5
|
class Client
|
@@ -7,14 +7,22 @@ module RestCountries
|
|
7
7
|
|
8
8
|
attr_reader :adapter
|
9
9
|
|
10
|
-
def initialize(adapter: Faraday.default_adapter)
|
10
|
+
def initialize(adapter: Faraday.default_adapter, max_retries: 3, retry_interval: 1, timeout: 10, open_timeout: 5)
|
11
11
|
@adapter = adapter
|
12
|
+
@max_retries = max_retries
|
13
|
+
@retry_interval = retry_interval
|
14
|
+
@timeout = timeout
|
15
|
+
@open_timeout = open_timeout
|
12
16
|
end
|
13
17
|
|
14
18
|
def connection
|
15
19
|
@connection ||= Faraday.new(BASE_URL) do |conn|
|
16
20
|
conn.request :json
|
17
21
|
conn.response :json, content_type: "application/json"
|
22
|
+
conn.request :retry, max: @max_retries, interval: @retry_interval,
|
23
|
+
exceptions: [Faraday::ConnectionFailed, Faraday::TimeoutError]
|
24
|
+
conn.options.timeout = @timeout
|
25
|
+
conn.options.open_timeout = @open_timeout
|
18
26
|
conn.adapter adapter
|
19
27
|
end
|
20
28
|
end
|
@@ -32,7 +40,8 @@ module RestCountries
|
|
32
40
|
|
33
41
|
# Search by country’s full name. It can be the common or official value
|
34
42
|
def fullname(country_name)
|
35
|
-
|
43
|
+
encoded_name = URI.encode_www_form_component(country_name)
|
44
|
+
response = connection.get("name/#{encoded_name}?fullText=true")
|
36
45
|
handle_response(response)
|
37
46
|
end
|
38
47
|
|
@@ -68,19 +77,22 @@ module RestCountries
|
|
68
77
|
|
69
78
|
# Search by capital city
|
70
79
|
def capital(capital)
|
71
|
-
|
80
|
+
encoded_capital = URI.encode_www_form_component(capital)
|
81
|
+
response = connection.get("capital/#{encoded_capital}")
|
72
82
|
handle_response(response)
|
73
83
|
end
|
74
84
|
|
75
85
|
# Search by region
|
76
86
|
def region(region)
|
77
|
-
|
87
|
+
encoded_region = URI.encode_www_form_component(region)
|
88
|
+
response = connection.get("region/#{encoded_region}")
|
78
89
|
handle_response(response)
|
79
90
|
end
|
80
91
|
|
81
92
|
# Search by subregions
|
82
93
|
def subregion(subregion)
|
83
|
-
|
94
|
+
encoded_subregion = URI.encode_www_form_component(subregion)
|
95
|
+
response = connection.get("subregion/#{encoded_subregion}")
|
84
96
|
handle_response(response)
|
85
97
|
end
|
86
98
|
|
@@ -99,10 +111,17 @@ module RestCountries
|
|
99
111
|
private
|
100
112
|
|
101
113
|
def handle_response(response)
|
102
|
-
|
114
|
+
case response.status
|
115
|
+
when 200...300
|
103
116
|
response.body
|
117
|
+
when 404
|
118
|
+
raise NotFoundError, "Country not found"
|
119
|
+
when 400...500
|
120
|
+
raise ClientError, "Client error: #{response.status}"
|
121
|
+
when 500...600
|
122
|
+
raise ServerError, "Server error: #{response.status}"
|
104
123
|
else
|
105
|
-
raise "
|
124
|
+
raise "Unexpected response: #{response.status}"
|
106
125
|
end
|
107
126
|
end
|
108
127
|
end
|
data/lib/rest_countries/error.rb
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest_countries
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mae Ann Sacedon Cimafranca
|
@@ -44,6 +44,7 @@ files:
|
|
44
44
|
- lib/rest_countries/client.rb
|
45
45
|
- lib/rest_countries/error.rb
|
46
46
|
- lib/rest_countries/version.rb
|
47
|
+
- rest_countries-1.0.0.gem
|
47
48
|
- sig/rest_countries.rbs
|
48
49
|
- ting API Requests
|
49
50
|
homepage: https://github.com/cimafrancamae/rest_countries
|