Class: WebFetch::Client
- Inherits:
-
Object
- Object
- WebFetch::Client
- Includes:
- ClientHttp
- Defined in:
- lib/web_fetch/client.rb
Overview
Client to be used in application code. Capable of spawning a server and interacting with it to gather requests and retrieve them
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Class Method Summary collapse
- .build_process(host, port, options) ⇒ Object
- .create(host, port, options = {}) ⇒ Object
- .cwd ⇒ Object
- .standard_start_command ⇒ Object
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #fetch(uid, options = {}) ⇒ Object
- #find_by_uid(uid) ⇒ Object
- #gather(requests) ⇒ Object
-
#initialize(host, port, options = {}) ⇒ Client
constructor
A new instance of Client.
- #retrieve_by_uid(uid) ⇒ Object
- #stop ⇒ Object
Methods included from ClientHttp
Constructor Details
#initialize(host, port, options = {}) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 |
# File 'lib/web_fetch/client.rb', line 11 def initialize(host, port, = {}) @host = host @port = port @process = [:process] end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
9 10 11 |
# File 'lib/web_fetch/client.rb', line 9 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
9 10 11 |
# File 'lib/web_fetch/client.rb', line 9 def port @port end |
Class Method Details
.build_process(host, port, options) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/web_fetch/client.rb', line 73 def build_process(host, port, ) command = .fetch(:start_command, standard_start_command) args = ['--host', host, '--port', port.to_s] args += ['--log', [:log]] unless [:log].nil? args.push('--daemonize') if [:daemonize] Subprocess.popen(command + args, cwd: cwd) end |
.create(host, port, options = {}) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/web_fetch/client.rb', line 17 def self.create(host, port, = {}) # Will block until process is responsive process = build_process(host, port, ) client = new(host, port, process: process) sleep 0.1 until client.alive? client end |
.cwd ⇒ Object
81 82 83 |
# File 'lib/web_fetch/client.rb', line 81 def cwd File.join(File.dirname(__dir__), '..') end |
.standard_start_command ⇒ Object
85 86 87 |
# File 'lib/web_fetch/client.rb', line 85 def standard_start_command %w[bundle exec ./bin/web_fetch_control run --] end |
Instance Method Details
#alive? ⇒ Boolean
33 34 35 36 37 38 39 40 41 42 |
# 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 = {}) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/web_fetch/client.rb', line 55 def fetch(uid, = {}) block = .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) ⇒ Object
68 69 70 |
# File 'lib/web_fetch/client.rb', line 68 def find_by_uid(uid) fetch_nonblocking(uid)[:request] end |
#gather(requests) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# 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) ⇒ Object
64 65 66 |
# File 'lib/web_fetch/client.rb', line 64 def retrieve_by_uid(uid) fetch_blocking(uid)[:request] end |
#stop ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/web_fetch/client.rb', line 25 def stop return if @process.nil? @process.terminate # Will block until process dies @process.wait end |