Skip to content

Instantly share code, notes, and snippets.

@cmar
Created September 14, 2015 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmar/6d548dfacad7fdb94ee5 to your computer and use it in GitHub Desktop.
Save cmar/6d548dfacad7fdb94ee5 to your computer and use it in GitHub Desktop.
Typhoeus multi threaded request for Star Wars Vehicles from all Films
#! /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