class Doxieland::Client

Attributes

save_path[R]

Public Class Methods

new(options) click to toggle source
# File lib/doxieland/client.rb, line 5
def initialize(options)
  @options       = options
  @save_path     = Pathname.new(options[:to] || '.').expand_path
end

Public Instance Methods

api() { |api| ... } click to toggle source
# File lib/doxieland/client.rb, line 10
def api(&block)
  Doxieland::Api.url "http://#{scanner_ip}:8080"

  if @options[:password]
    Doxieland::Api.http_basic_auth 'doxie', @options[:password]
  end

  api = Doxieland::Api.new

  begin
    yield api
  rescue AuthenticationError => e
    log.fatal e.message
    exit(false)
  end
end
create_save_path() click to toggle source
# File lib/doxieland/client.rb, line 27
def create_save_path
  raise ArgumentError, "#{@save_path} is a file" if @save_path.file?

  unless @save_path.directory? || @save_path == Pathname.new('.')
    FileUtils.mkdir_p(@save_path)
  end
end
log() click to toggle source
# File lib/doxieland/client.rb, line 39
def log
  @logger ||= Logger.new(loglevel)
end
loglevel() click to toggle source
# File lib/doxieland/client.rb, line 35
def loglevel
  @options[:verbose] ? :debug : :info
end
scanner_ip() click to toggle source
# File lib/doxieland/client.rb, line 43
def scanner_ip
  @scanner_ip ||= begin
    if @options['scanner-ip']
      @options['scanner-ip']
    elsif @options[:ap]
      '192.168.1.100'
    else
      discovered_ip = ssdp_discover

      unless discovered_ip
        log.fatal "… sorry, your scanner could not be found. Is WiFi turned on and the status light blue?"
        log.info "If you know it, you can also provide the IP address manually via the --scanner-ip flag."
        exit
      end

      discovered_ip
    end
  end
end
ssdp_discover() click to toggle source
# File lib/doxieland/client.rb, line 63
def ssdp_discover
  socket = UDPSocket.new
  socket.setsockopt Socket::SOL_SOCKET, Socket::SO_BROADCAST, true
  socket.setsockopt :IPPROTO_IP, :IP_MULTICAST_TTL, 1

  query = [
    'M-SEARCH * HTTP/1.1',
    'HOST: 239.255.255.250:1900',
    'MAN: "ssdp:discover"',
    'ST: urn:schemas-getdoxie-com:device:Scanner:1',
    # 'ST: ssdp:all',
    'MX: 3',
    '',
    ''
  ].join("\r\n")

  log.info "trying to find your scanner on the network. Here, Doxie Doxie…"

  socket.send(query, 0, '239.255.255.250', 1900)

  ready = IO.select([socket], nil, nil, 10)

  if ready
    _, message_sender = socket.recvfrom(65507)

    log.success "found the little rascal hiding at #{message_sender.last}"

    message_sender.last
  end
end