Class: Client

Inherits:
Object
  • Object
show all
Defined in:
lib/text_tunnel/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(port, file_path) ⇒ Client

Establishes initial connection to text_tunneld server



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/text_tunnel/client.rb', line 27

def initialize(port, file_path)
  @file_path = file_path

  file_name = File.basename(@file_path)
  file_data = File.exist?(@file_path) ? File.read(@file_path) : ""

  response = RestClient.post "http://localhost:#{port}/files",
    :name => file_name,
    :data => file_data
  raise UnexpectedResponseError.new(response) unless response.code == 201
  @location = response.headers[:location]
  @etag = response.headers[:etag]
end

Instance Method Details

#cleanupObject



53
54
55
56
57
# File 'lib/text_tunnel/client.rb', line 53

def cleanup
  # This call can fail, especially if text_tunnel is terminating because of
  # a previous error. So swallow any errors.
  RestClient.delete(@location) rescue nil
end

#pollObject

Returns a truthy value if a change was made



42
43
44
45
46
47
48
49
50
51
# File 'lib/text_tunnel/client.rb', line 42

def poll
  response = RestClient.get(@location, :if_none_match => @etag)
  raise UnexpectedResponseError.new(response) unless response.code == 200

  @etag = response.headers[:etag]

  File.write(@file_path, response.body)
rescue RestClient::NotModified
  false
end