Skip to content

Commit

Permalink
Reto mouredev#10 - Ruby version
Browse files Browse the repository at this point in the history
  • Loading branch information
test0n3 committed Mar 7, 2023
1 parent a7ab9ab commit fdd135a
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Retos/Reto #10 - LA API [Media]/ruby/test0n3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

# APIs source: https://github.com/public-apis/public-apis
# source: https://www.twilio.com/blog/5-ways-make-http-requests-ruby

require 'uri'
require 'net/http'

# class GetAPI with random selection of APIs
class GetAPI
URL = { 0 => { 'title' => 'Cats API', 'url' => 'https://cat-fact.herokuapp.com/facts' },
1 => { 'title' => 'Fish API', 'url' => 'https://www.fishwatch.gov/api/species' },
2 => { 'title' => 'Bored API', 'url' => 'https://www.boredapi.com/api/activity/' } }.freeze

def start
@selected_api = choose_api
@selected_url = @selected_api['url']
@selected_title = @selected_api['title']
print_api_resp
end

def print_api_resp
# puts "title: #{@selected_title}, url: #{@selected_url}"
uri = URI(@selected_url)
res = Net::HTTP.get_response(uri)
puts "API name #{@selected_title}"
puts res.body if res.is_a?(Net::HTTPSuccess)
end

def choose_api
URL[URL.keys.sample]
end
end

GetAPI.new.start

0 comments on commit fdd135a

Please sign in to comment.