Skip to content

Instantly share code, notes, and snippets.

@bleonard
Created December 24, 2016 19:23
Show Gist options
  • Save bleonard/85709a2b4d321fa013d2cac5688ca5ed to your computer and use it in GitHub Desktop.
Save bleonard/85709a2b4d321fa013d2cac5688ca5ed to your computer and use it in GitHub Desktop.
class Main
def process_all
mutex = Mutex.new
queue = self.paths.dup
self.thread_count.times.map {
Thread.new do
while path = mutex.synchronize { queue.pop }
fetcher = ThreadedFetcher.new(path)
fetcher.fetch!
end
end
}.each(&:join)
end
end
class ThreadedFetcher
def initialize(path)
@path = path
end
def vcr_client
return @vcr_client if @vcr_client
@vcr_client = VCR::Client.new
@vcr_client.configure do |c|
c.cassette_library_dir = "fixtures/vcr_cassettes/#{@path}"
end
@vcr_client
end
def fetch!
# the same original code would probably work but I like it even more separated
# that is, move the @path into client init above
vcr_client.use_cassette('fetched') do
response = Net::HTTP.get_response(URI("http://api.example.com/#{@path}"))
process(response)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment