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
78 79 80 81 82 83 84 |
# File 'lib/web_fetch/client.rb', line 78 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
86 87 88 |
# File 'lib/web_fetch/client.rb', line 86 def cwd File.join(File.dirname(__dir__), '..') end |
.standard_start_command ⇒ Object
90 91 92 |
# File 'lib/web_fetch/client.rb', line 90 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
54 55 56 57 58 59 60 61 |
# File 'lib/web_fetch/client.rb', line 54 def fetch(uid, = {}) block = .fetch(:wait, true) outcome = block ? retrieve_by_uid(uid) : find_by_uid(uid) no_request_error(uid) if outcome.nil? new_response(outcome) end |
#find_by_uid(uid) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/web_fetch/client.rb', line 70 def find_by_uid(uid) response = get("find/#{uid}") return nil unless response.success? JSON.parse(response.body, symbolize_names: true) end |
#gather(requests) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# 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
63 64 65 66 67 68 |
# File 'lib/web_fetch/client.rb', line 63 def retrieve_by_uid(uid) response = get("retrieve/#{uid}") return nil unless response.success? JSON.parse(response.body, symbolize_names: true) 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 |