Class: WebFetch::Client

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClientHttp

#base_uri, #get, #post

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, options = {})
  @host = host
  @port = port
  @process = options[:process]
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/web_fetch/client.rb', line 9

def host
  @host
end

#portObject (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, 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 = {}) ⇒ Object



17
18
19
20
21
22
23
# 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

.cwdObject



81
82
83
# File 'lib/web_fetch/client.rb', line 81

def cwd
  File.join(File.dirname(__dir__), '..')
end

.standard_start_commandObject



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

Returns:

  • (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, 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) ⇒ 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

#stopObject



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