class WebFetch::Client
Client
to be used in application code. Capable of spawning a server and interacting with it to gather requests and retrieve them
Attributes
host[R]
port[R]
Public Class Methods
build_process(host, port, options)
click to toggle source
# File lib/web_fetch/client.rb, line 73 def build_process(host, port, options) command = options.fetch(:start_command, standard_start_command) args = ['--host', host, '--port', port.to_s] args += ['--log', options[:log]] unless options[:log].nil? args.push('--daemonize') if options[:daemonize] Subprocess.popen(command + args, cwd: cwd) end
create(host, port, options = {})
click to toggle source
# File lib/web_fetch/client.rb, line 17 def self.create(host, port, options = {}) # Will block until process is responsive process = build_process(host, port, options) client = new(host, port, process: process) sleep 0.1 until client.alive? client end
cwd()
click to toggle source
# File lib/web_fetch/client.rb, line 81 def cwd File.join(File.dirname(__dir__), '..') end
new(host, port, options = {})
click to toggle source
# File lib/web_fetch/client.rb, line 11 def initialize(host, port, options = {}) @host = host @port = port @process = options[:process] end
standard_start_command()
click to toggle source
# File lib/web_fetch/client.rb, line 85 def standard_start_command %w[bundle exec ./bin/web_fetch_control run --] end
Public Instance Methods
alive?()
click to toggle source
# File lib/web_fetch/client.rb, line 33 def alive? begin response = get('') rescue ClientError return false end return false unless response.success? JSON.parse(response.body)['application'] == 'WebFetch' end
fetch(uid, options = {})
click to toggle source
# File lib/web_fetch/client.rb, line 55 def fetch(uid, options = {}) block = options.fetch(:wait, true) outcome = block ? fetch_blocking(uid) : fetch_nonblocking(uid) no_request_error(uid) if outcome.nil? Response.new(outcome.merge(uid: uid)) end
find_by_uid(uid)
click to toggle source
# File lib/web_fetch/client.rb, line 68 def find_by_uid(uid) fetch_nonblocking(uid)[:request] end
gather(requests)
click to toggle source
# File lib/web_fetch/client.rb, line 44 def gather(requests) json = JSON.dump(requests: requests.map(&:to_h)) response = post('gather', json) handle_error(JSON.parse(response.body)['error']) unless response.success? requests = JSON.parse(response.body, symbolize_names: true)[:requests] promises(requests) end
retrieve_by_uid(uid)
click to toggle source
# File lib/web_fetch/client.rb, line 64 def retrieve_by_uid(uid) fetch_blocking(uid)[:request] end
stop()
click to toggle source
# File lib/web_fetch/client.rb, line 25 def stop return if @process.nil? @process.terminate # Will block until process dies @process.wait end
Private Instance Methods
decode_response(response)
click to toggle source
# File lib/web_fetch/client.rb, line 92 def decode_response(response) return response unless response[:command] == 'retrieve' response[:request][:response][:body] = Base64.decode64( response[:request][:response][:body] ) response end
fetch_blocking(uid)
click to toggle source
# File lib/web_fetch/client.rb, line 101 def fetch_blocking(uid) response = get("retrieve/#{uid}") return nil unless response.success? decode_response(JSON.parse(response.body, symbolize_names: true)) end
fetch_nonblocking(uid)
click to toggle source
# File lib/web_fetch/client.rb, line 108 def fetch_nonblocking(uid) response = get("find/#{uid}") return nil unless response.success? decode_response(JSON.parse(response.body, symbolize_names: true)) end
handle_error(error)
click to toggle source
# File lib/web_fetch/client.rb, line 115 def handle_error(error) raise WebFetch::ClientError, error end
no_request_error(uid)
click to toggle source
# File lib/web_fetch/client.rb, line 119 def no_request_error(uid) raise RequestNotFoundError, [I18n.t('no_request', uid: uid)] end
promises(requests)
click to toggle source
# File lib/web_fetch/client.rb, line 123 def promises(requests) requests.map do |request| Promise.new(self, uid: request[:uid], request: request[:request]) end end