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



78
79
80
81
82
83
84
# File 'lib/web_fetch/client.rb', line 78

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



86
87
88
# File 'lib/web_fetch/client.rb', line 86

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

.standard_start_commandObject



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

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



54
55
56
57
58
59
60
61
# File 'lib/web_fetch/client.rb', line 54

def fetch(uid, options = {})
  block = options.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

#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