class UrlTracker::Client

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

Public Class Methods

new(socket_file = '/tmp/_ut.sock') click to toggle source
# 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

Public Instance Methods

list() click to toggle source

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

# File lib/url_tracker/client.rb, line 29
def list
  write('list')
  next_message.split(',')
end
release(url) click to toggle source

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

# File lib/url_tracker/client.rb, line 36
def release(url)
  write("release #{url}")
  next_message == 'ok'
end
run(params) click to toggle source

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.

# 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
shutdown() click to toggle source

Tells the server to shutdown

# File lib/url_tracker/client.rb, line 42
def shutdown
  write('shutdown')
end
track(url) click to toggle source

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

"track {{URL}}"
# File lib/url_tracker/client.rb, line 22
def track(url)
  write("track #{url}")
  next_message == 'ok'
end

Private Instance Methods

parse(argv) click to toggle source
# File lib/url_tracker/client.rb, line 67
def parse(argv)
  return OpenStruct.new(argv) if argv.kind_of?(Hash)

  options = OpenStruct.new
  options.action = :nothing

  opts = OptionParser.new do |opts|
    opts.banner = 'Usage: ut [options]'
    opts.separator ''
    opts.separator 'Available options:'

    opts.on('-t', '--track URL', 'Start tracking URL') do |url|
      options.url    = prepare_url(url)
      options.action = :track
    end

    opts.on('-l', '--list', 'List currently tracked URLs') do |list|
      options.action = :list
    end

    opts.on('-r', '--release URL', 'Release URL, not tracking it any more') do |url|
      options.url    = url
      options.action = :release
    end

    opts.on_tail('-h', '--help', 'Show this message') do
      puts opts
      exit(0)
    end
  end

  opts.parse!(argv)

  options
end
prepare_url(url) click to toggle source
# File lib/url_tracker/client.rb, line 103
def prepare_url(url)
  url.tap { url.prepend('http://') unless url.start_with?('http://') }
end