Skip to content

Instantly share code, notes, and snippets.

@ismasan
Last active December 21, 2015 04:49
Show Gist options
  • Save ismasan/6252382 to your computer and use it in GitHub Desktop.
Save ismasan/6252382 to your computer and use it in GitHub Desktop.
Using Celluloid futures to make concurrent HTTP requests
require "open-uri"
require 'json'
require 'celluloid/autostart'
def json(url)
JSON.parse(open(url, 'Authorization' => 'Bearer API TOKEN').read)
end
class Resource
def initialize(url)
@url = url
@future = Celluloid::Future.new { json(@url) }
# @future = json(@url)
end
def links
document['items'].map {|pr| pr['links']['self']['href']}
end
def title
document['properties']['title']
end
def description
document['properties']['description']
end
private
def document
@future.value
end
end
class Index
def initialize(url)
@resource = Resource.new(url)
end
def urls
@resource.links
end
end
class Crawler
def initialize(index)
@index = index
end
def crawl
resources.each do |page|
Outputter.new(page).output
end
end
private
def resources
@index.urls.map do |url|
# future { Resource.new(url).get }
Resource.new(url)
end
end
end
require "highline"
class Outputter
OUTPUT_WIDTH = 79
def initialize(page)
@page = page
end
def output
highline.say("-" * OUTPUT_WIDTH)
highline.say(@page.title)
highline.say("-" * OUTPUT_WIDTH)
highline.say("\n\n")
end
private
def highline
@highline ||= HighLine.new($stdin, $stdout, OUTPUT_WIDTH)
end
end
Crawler.new(Index.new("https://api.bootic.net/v1/products.json")).crawl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment