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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c859f16c16ef81060943f21b0d506cd239c1712c2a76420fa93f46ccef47f18d
4
- data.tar.gz: 1f691366dfb05e221b1efa18702998536cb7cbd394efb0e48b422f4a7fce584d
3
+ metadata.gz: 7728ecd48316ab130211f869f6c55819e2ead6797b1a471ef6f77391c3cc8737
4
+ data.tar.gz: 27dbd592a4026c441a218e91516e9741a0f8f319b2caadb348d29efa60cb8739
5
5
  SHA512:
6
- metadata.gz: d5b87300db3674a74e170fc6c4a1c71a5094065e4f3196aa614859e26663b60b3c5c3644d3ad022a61ad6b693d9d3185cb67889d2416f04ea6c62abde2c5b8aa
7
- data.tar.gz: 4f82e38de329457884b8dab1c4baa73e3014dd791d231b6af27dc802878824686a92728391dc5de298cdcaacb965f61474901e577acb48a202c4e1590a5a924b
6
+ metadata.gz: 670b1debe8d681879f9e0e15057b2071579a3686393950e70a0b2eec7bb68deb058b507a5a27164cdab3c628bea738bee02ca384eb8a554be60ac740b311c041
7
+ data.tar.gz: beb1d2e14ced9b4a33322cce52f1efaf27f4c644baeb42c7fb6a4f7336a8383e2ed7579e8afdd261f9f8a3fb073c0305709792c5f8809c685a2dcabe2d5afed8
data/README.md CHANGED
@@ -58,6 +58,8 @@ The client then gives you access to the endpoints
58
58
 
59
59
  ### Region
60
60
 
61
+ Search by region: Africa, Americas, Asia, Europe, Oceania
62
+
61
63
  $ client.region(region)
62
64
 
63
65
  ### Subregions
@@ -1,5 +1,5 @@
1
1
  require "faraday"
2
- # require "faraday_middleware"
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
- response = connection.get("name/#{country_name}?fullText=true")
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
- response = connection.get("capital/#{capital}")
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
- response = connection.get("region/#{region}")
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
- response = connection.get("subregion/#{subregion}")
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
- if response.success?
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 "Error fetching data: #{response.status}"
124
+ raise "Unexpected response: #{response.status}"
106
125
  end
107
126
  end
108
127
  end
@@ -1,3 +1,6 @@
1
1
  module RestCountries
2
2
  class Error < StandardError; end
3
+ class NotFoundError < Error; end
4
+ class ClientError < Error; end
5
+ class ServerError < Error; end
3
6
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RestCountries
4
- VERSION = "1.0.0"
4
+ VERSION = "1.0.1"
5
5
  end
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.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