Class: UrlTracker::Client
- Inherits:
-
Object
- Object
- UrlTracker::Client
- 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
Instance Method Summary collapse
-
#initialize(socket_file = '/tmp/_ut.sock') ⇒ Client
constructor
A new instance of Client.
-
#list ⇒ Object
Asks the server for all the URLs currently being tracked.
-
#release(url) ⇒ Object
Tells the server to stop tracking the given URL.
-
#run(params) ⇒ Object
Calls one of the methods above according to the options passed.
-
#shutdown ⇒ Object
Tells the server to shutdown.
-
#track(url) ⇒ Object
Sends a message to the server asking to track a new URL.
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
#list ⇒ Object
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') .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}") == '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) = parse(params) output = case .action when :track then track(.url) when :list then list when :release then release(.url) end end |
#shutdown ⇒ Object
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}") == 'ok' end |