Last active
December 21, 2015 04:49
-
-
Save ismasan/6252382 to your computer and use it in GitHub Desktop.
Using Celluloid futures to make concurrent HTTP requests
This file contains hidden or 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
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