Class: RestCountries::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rest_countries/client.rb

Constant Summary collapse

BASE_URL =
"https://restcountries.com/v3.1/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter: Faraday.default_adapter, max_retries: 3, retry_interval: 1, timeout: 10, open_timeout: 5) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
# File 'lib/rest_countries/client.rb', line 10

def initialize(adapter: Faraday.default_adapter, max_retries: 3, retry_interval: 1, timeout: 10, open_timeout: 5)
  @adapter = adapter
  @max_retries = max_retries
  @retry_interval = retry_interval
  @timeout = timeout
  @open_timeout = open_timeout
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



8
9
10
# File 'lib/rest_countries/client.rb', line 8

def adapter
  @adapter
end

Instance Method Details

#allObject



30
31
32
33
# File 'lib/rest_countries/client.rb', line 30

def all
  response = connection.get("all")
  handle_response(response)
end

#capital(capital) ⇒ Object

Search by capital city



79
80
81
82
83
# File 'lib/rest_countries/client.rb', line 79

def capital(capital)
  encoded_capital = URI.encode_www_form_component(capital)
  response = connection.get("capital/#{encoded_capital}")
  handle_response(response)
end

#code(code) ⇒ Object

Search by cca2, ccn3, cca3 or cioc country code



49
50
51
52
# File 'lib/rest_countries/client.rb', line 49

def code(code)
  response = connection.get("alpha/#{code}")
  handle_response(response)
end

#codes(codes) ⇒ Object

List of codes



55
56
57
58
# File 'lib/rest_countries/client.rb', line 55

def codes(codes)
  response = connection.get("alpha?codes=#{codes.join(",")}")
  handle_response(response)
end

#connectionObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rest_countries/client.rb', line 18

def connection
  @connection ||= Faraday.new(BASE_URL) do |conn|
    conn.request :json
    conn.response :json, content_type: "application/json"
    conn.request :retry, max: @max_retries, interval: @retry_interval,
      exceptions: [Faraday::ConnectionFailed, Faraday::TimeoutError]
    conn.options.timeout = @timeout
    conn.options.open_timeout = @open_timeout
    conn.adapter adapter
  end
end

#currency(currency) ⇒ Object

Search by currency code or name



61
62
63
64
# File 'lib/rest_countries/client.rb', line 61

def currency(currency)
  response = connection.get("currency/#{currency}")
  handle_response(response)
end

#demonym(demonym) ⇒ Object

Search by how a citizen is called



67
68
69
70
# File 'lib/rest_countries/client.rb', line 67

def demonym(demonym)
  response = connection.get("demonym/#{demonym}")
  handle_response(response)
end

#filter(service, fields) ⇒ Object

Filter the output of your request to include only the specified fields.



106
107
108
109
# File 'lib/rest_countries/client.rb', line 106

def filter(service, fields)
  response = connection.get("#{service}?fields=#{fields.join(",")}")
  handle_response(response)
end

#fullname(country_name) ⇒ Object

Search by country’s full name. It can be the common or official value



42
43
44
45
46
# File 'lib/rest_countries/client.rb', line 42

def fullname(country_name)
  encoded_name = URI.encode_www_form_component(country_name)
  response = connection.get("name/#{encoded_name}?fullText=true")
  handle_response(response)
end

#language(language) ⇒ Object

Search by language code or name



73
74
75
76
# File 'lib/rest_countries/client.rb', line 73

def language(language)
  response = connection.get("lang/#{language}")
  handle_response(response)
end

#name(country_name) ⇒ Object

Search by country name. If you want to get an exact match, use the next endpoint. It can be the common or official value



36
37
38
39
# File 'lib/rest_countries/client.rb', line 36

def name(country_name)
  response = connection.get("name/#{country_name}")
  handle_response(response)
end

#region(region) ⇒ Object

Search by region



86
87
88
89
90
# File 'lib/rest_countries/client.rb', line 86

def region(region)
  encoded_region = URI.encode_www_form_component(region)
  response = connection.get("region/#{encoded_region}")
  handle_response(response)
end

#subregion(subregion) ⇒ Object

Search by subregions



93
94
95
96
97
# File 'lib/rest_countries/client.rb', line 93

def subregion(subregion)
  encoded_subregion = URI.encode_www_form_component(subregion)
  response = connection.get("subregion/#{encoded_subregion}")
  handle_response(response)
end

#translation(translation) ⇒ Object

Search by any translation name



100
101
102
103
# File 'lib/rest_countries/client.rb', line 100

def translation(translation)
  response = connection.get("translation/#{translation}")
  handle_response(response)
end