Class: UrlTracker::Client

Inherits:
Object
  • Object
show all
Includes:
SocketCommunication
Defined in:
lib/url_tracker/client.rb

Overview

Class who deals with requesting information to the server, such as track a new URL, list all currently tracked links, stop tracking something, etc.

Constant Summary

Constants included from SocketCommunication

SocketCommunication::InvalidSocketError, SocketCommunication::MAX_CONN_QUEUE, SocketCommunication::MAX_MESSAGE_LENGTH

Instance Attribute Summary

Attributes included from SocketCommunication

#path

Instance Method Summary collapse

Methods included from SocketCommunication

#bind, #close_connection, #connect, #next_message, #wait_for_connection, #write

Constructor Details

#initialize(socket_file = '/tmp/_ut.sock') ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
# File 'lib/url_tracker/client.rb', line 11

def initialize(socket_file = '/tmp/_ut.sock')
  connect(socket_file)
rescue Errno::ENOENT
  STDERR.puts 'Connection error. Is the server running?'
  exit(1)
end

Instance Method Details

#listObject

Asks the server for all the URLs currently being tracked. Expects a string back, with URLs separated by commas.



29
30
31
32
# File 'lib/url_tracker/client.rb', line 29

def list
  write('list')
  next_message.split(',')
end

#release(url) ⇒ Object

Tells the server to stop tracking the given URL. Returns true if the operation was successful



36
37
38
39
# File 'lib/url_tracker/client.rb', line 36

def release(url)
  write("release #{url}")
  next_message == 'ok'
end

#run(params) ⇒ Object

Calls one of the methods above according to the options passed. Available options:

-t, --track URL   #=> Starts tracking URL
-l, --list        #=> List currently tracked URLs
-r, --release URL #=> Releases URL, not tracking it any more

params can also be a hash, in which case it will be considered already parsed.



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

def run(params)
  options = parse(params)

  output = case options.action
    when :track   then track(options.url)
    when :list    then list
    when :release then release(options.url)
  end
end

#shutdownObject

Tells the server to shutdown



42
43
44
# File 'lib/url_tracker/client.rb', line 42

def shutdown
  write('shutdown')
end

#track(url) ⇒ Object

Sends a message to the server asking to track a new URL. Format of the message:

"track {{URL}}"


22
23
24
25
# File 'lib/url_tracker/client.rb', line 22

def track(url)
  write("track #{url}")
  next_message == 'ok'
end