Created
December 24, 2016 19:23
-
-
Save bleonard/85709a2b4d321fa013d2cac5688ca5ed to your computer and use it in GitHub Desktop.
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
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 |
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
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