Created
September 14, 2015 22:54
-
-
Save cmar/6d548dfacad7fdb94ee5 to your computer and use it in GitHub Desktop.
Typhoeus multi threaded request for Star Wars Vehicles from all Films
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
# RubyLoCo Star Wars Hack Night | |
# Documentation https://swapi.co/documentation | |
require 'json' | |
require 'bundler/inline' | |
# true to install gems | |
gemfile ENV['INSTALL'] do | |
source 'https://rubygems.org' | |
gem 'typhoeus' | |
end | |
hydra = Typhoeus::Hydra.new | |
films_request = Typhoeus::Request.new("http://swapi.co/api/films/") | |
films_request.on_complete do |response| | |
films = JSON.parse(response.body)['results'] | |
films.each do |film| | |
title = film['title'] | |
film['vehicles'].each do |vehicle_url| | |
vehicle_request = Typhoeus::Request.new(vehicle_url) | |
vehicle_request.on_complete do |response| | |
vehicle = JSON.parse response.body | |
p "#{title} - #{vehicle['name']}" | |
end | |
hydra.queue vehicle_request | |
end | |
end | |
end | |
hydra.queue films_request | |
hydra.run # this is a blocking call that returns once all requests are complete | |
p 'All Done!' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment