Class: FileRpc::Transport::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/file_rpc/transport/file_system.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue_name:, base_path: FileRpc.configuration.base_path) ⇒ FileSystem

Returns a new instance of FileSystem.



8
9
10
11
12
# File 'lib/file_rpc/transport/file_system.rb', line 8

def initialize(queue_name:, base_path: FileRpc.configuration.base_path)
  @queue_name = queue_name
  @base_path = base_path
  ensure_directories!
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



6
7
8
# File 'lib/file_rpc/transport/file_system.rb', line 6

def base_path
  @base_path
end

#queue_nameObject (readonly)

Returns the value of attribute queue_name.



6
7
8
# File 'lib/file_rpc/transport/file_system.rb', line 6

def queue_name
  @queue_name
end

Instance Method Details

#cleanup(request_id) ⇒ Object



36
37
38
39
40
# File 'lib/file_rpc/transport/file_system.rb', line 36

def cleanup(request_id)
  [request_file_path(request_id), response_file_path(request_id)].each do |path|
    File.delete(path) if File.exist?(path)
  end
end

#read_response(request_id) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/file_rpc/transport/file_system.rb', line 24

def read_response(request_id)
  response_file = response_file_path(request_id)
  return nil unless File.exist?(response_file)

  begin
    data = JSON.parse(File.read(response_file))
    Message::Response.new(data)
  rescue JSON::ParserError
    nil
  end
end

#write_request(request) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/file_rpc/transport/file_system.rb', line 14

def write_request(request)
  request_file = request_file_path(request.id)
  temp_file = "#{request_file}.tmp"

  File.write(temp_file, request.to_json)
  File.rename(temp_file, request_file)

  request_file
end