class Doxie::Client

The client for connecting to a Doxie scanner.

Use the IP and password to connect as follows:

Doxie::Client.new(ip: '192.168.1.2', model: Doxie::Q, password: 'test')

Constants

USERNAME

Attributes

ip[RW]
model[RW]
password[RW]
port[RW]

Public Class Methods

new(options) click to toggle source
# File lib/doxie/client.rb, line 18
def initialize(options)
  @ip = options[:ip] || ''
  @password = options[:password] || ''
  @model = options[:model] || Doxie::API_V1
  @port = @model == Doxie::API_V1 ? 8080 : 80
end

Public Instance Methods

delete_scan(scan_name) click to toggle source
# File lib/doxie/client.rb, line 63
def delete_scan(scan_name)
  delete("/scans#{scan_name}") || true
end
delete_scans(scan_names) click to toggle source
# File lib/doxie/client.rb, line 67
def delete_scans(scan_names)
  post('/scans/delete.json', scan_names) || true
end
hello() click to toggle source
# File lib/doxie/client.rb, line 25
def hello
  get('/hello.json')
end
hello_extra() click to toggle source
# File lib/doxie/client.rb, line 29
def hello_extra
  if model == Doxie::API_V2
    raise Doxie::Client::Error, 'Method does not exist for this model'
  end

  get('/hello_extra.json')
end
recent_scans() click to toggle source
# File lib/doxie/client.rb, line 51
def recent_scans
  get('/scans/recent.json') || []
end
restart() click to toggle source
# File lib/doxie/client.rb, line 37
def restart
  get('/restart.json') || true
end
scan(scan_name, file_name = nil) click to toggle source
# File lib/doxie/client.rb, line 55
def scan(scan_name, file_name = nil)
  file "/scans#{scan_name}", file_name
end
scans() click to toggle source
# File lib/doxie/client.rb, line 41
def scans
  get('/scans.json')
rescue Doxie::Client::Error => e
  # a 404 is thrown on the Doxie Q and
  # Doxie GO SE when there are no scans
  raise e if model == Doxie::API_V1

  []
end
thumbnail(scan_name, file_name = nil) click to toggle source
# File lib/doxie/client.rb, line 59
def thumbnail(scan_name, file_name = nil)
  file "/thumbnails#{scan_name}", file_name
end

Private Instance Methods

delete(path) click to toggle source
# File lib/doxie/client.rb, line 86
def delete(path)
  uri = uri_for(path)
  message = Net::HTTP::Delete.new(uri.request_uri)
  parse(request(uri, message))
end
file(scan_name, file_name) click to toggle source
# File lib/doxie/client.rb, line 121
def file(scan_name, file_name)
  body = get(scan_name)
  if file_name
    File.open(file_name, 'wb') { |file| file.write(body) }
    true
  else
    body
  end
end
get(path) click to toggle source
# File lib/doxie/client.rb, line 73
def get(path)
  uri = uri_for(path)
  message = Net::HTTP::Get.new(uri.request_uri)
  parse(request(uri, message))
end
parse(response) click to toggle source
# File lib/doxie/client.rb, line 102
def parse(response)
  case response
  when Net::HTTPNoContent
    nil
  when Net::HTTPSuccess
    parse_json(response)
  else
    raise Error, response
  end
end
parse_json(response) click to toggle source
# File lib/doxie/client.rb, line 113
def parse_json(response)
  if response['Content-Type'].split(';').first == 'application/json'
    JSON.parse(response.body)
  else
    response.body
  end
end
post(path, params) click to toggle source
# File lib/doxie/client.rb, line 79
def post(path, params)
  uri = uri_for(path)
  message = Net::HTTP::Post.new(uri.request_uri)
  message.body = JSON.generate(params)
  parse(request(uri, message))
end
request(uri, message) click to toggle source
# File lib/doxie/client.rb, line 96
def request(uri, message)
  message.basic_auth USERNAME, password if password && !password.empty?
  http = Net::HTTP.new(uri.host, uri.port)
  http.request(message)
end
uri_for(path) click to toggle source
# File lib/doxie/client.rb, line 92
def uri_for(path)
  URI("http://#{ip}:#{port}#{path}")
end