Class: CountryDetails::Get
- Inherits:
-
Object
- Object
- CountryDetails::Get
- Defined in:
- lib/country_details.rb
Constant Summary collapse
- @@all_countries =
[]
Instance Method Summary collapse
- #all ⇒ Object
- #details_by_code(code, options = {}) ⇒ Object
- #details_by_name(country_name, options = {}) ⇒ Object
-
#initialize ⇒ Get
constructor
A new instance of Get.
Constructor Details
#initialize ⇒ Get
Returns a new instance of Get.
5 6 7 8 9 |
# File 'lib/country_details.rb', line 5 def initialize url = 'https://restcountries.eu/rest/v2/all' rest_client_response = RestClient.get(url) @@all_countries = JSON.parse(rest_client_response.body) end |
Instance Method Details
#all ⇒ Object
11 12 13 |
# File 'lib/country_details.rb', line 11 def all @@all_countries end |
#details_by_code(code, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/country_details.rb', line 21 def details_by_code(code, = {}) code = code.upcase if code.length == 2 detail = @@all_countries.map{|detail| detail if detail["alpha2Code"]==code}.compact elsif code.length == 3 detail = all.map{|detail| detail if detail["alpha3Code"]==code}.compact end return {'success': 'false', 'Error': 'Invalid code!'} unless detail.present? return detail end |
#details_by_name(country_name, options = {}) ⇒ Object
15 16 17 18 19 |
# File 'lib/country_details.rb', line 15 def details_by_name(country_name, = {}) detail = @@all_countries.map{|detail| detail if detail["name"]==country_name.capitalize}.compact return {'success': 'false', 'Error': 'Invalid country name!'} unless detail.present? return detail end |