-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_calls.rb
32 lines (24 loc) · 886 Bytes
/
api_calls.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Ruby gems required
require 'rest-client' # https://github.com/rest-client/rest-client
require 'json'
# Define url and assign response variable
api_url = 'https://restcountries.eu/rest/v2/'
response = RestClient.get(api_url)
# response = RestClient.get(api_url, { params: { key1: 'info 1' } })
rb = JSON.parse(response.body)
# -----------------------------------------
# Human readable JSON data in console
puts JSON.pretty_generate(rb)
# Assign data from call to working vraiables (from first indexed entry)
capital = rb[0]['capital']
currencies = rb[0]['currencies'][0]['name']
puts capital
puts currencies
# -----------------------------------------
# Iterate through JSON data until desired varlue reached then puts
length = rb.length.to_i - 1
(0..length).each do |i|
capital = rb[i]['capital']
next unless capital == 'London'
puts rb[i]['currencies'][0]['name']
end