class Stf::Client

Public Class Methods

new(base_url, token) click to toggle source
# File lib/stf/client.rb, line 13
def initialize(base_url, token)
  @base_url = base_url
  @token    = token
end

Public Instance Methods

add_adb_public_key(adbkeypub) click to toggle source
# File lib/stf/client.rb, line 43
def add_adb_public_key(adbkeypub)
  response = execute '/api/v1/user/adbPublicKeys', Net::HTTP::Post, { publickey: adbkeypub }.to_json
  response.success
end
add_device(serial) click to toggle source
# File lib/stf/client.rb, line 38
def add_device(serial)
  response = execute '/api/v1/user/devices', Net::HTTP::Post, { serial: serial }.to_json
  response.success
end
get_device(serial) click to toggle source
# File lib/stf/client.rb, line 23
def get_device(serial)
  response = execute "/api/v1/devices/#{serial}", Net::HTTP::Get
  response.device
end
get_devices() click to toggle source
# File lib/stf/client.rb, line 18
def get_devices
  response = execute '/api/v1/devices', Net::HTTP::Get
  response.devices
end
get_user() click to toggle source
# File lib/stf/client.rb, line 28
def get_user
  response = execute '/api/v1/user', Net::HTTP::Get
  response.user
end
get_user_devices() click to toggle source
# File lib/stf/client.rb, line 33
def get_user_devices
  response = execute '/api/v1/user/devices', Net::HTTP::Get
  response.devices
end
remove_device(serial) click to toggle source
# File lib/stf/client.rb, line 48
def remove_device(serial)
  response = execute "/api/v1/user/devices/#{serial}", Net::HTTP::Delete
  response.success
end
start_debug(serial) click to toggle source
# File lib/stf/client.rb, line 53
def start_debug(serial)
  response = execute "/api/v1/user/devices/#{serial}/remoteConnect", Net::HTTP::Post
  response
end
stop_debug(serial) click to toggle source
# File lib/stf/client.rb, line 58
def stop_debug(serial)
  response = execute "/api/v1/user/devices/#{serial}/remoteConnect", Net::HTTP::Delete
  response.success
end

Private Instance Methods

execute(relative_url, type, body = '') click to toggle source
# File lib/stf/client.rb, line 65
def execute(relative_url, type, body = '')
  execute_absolute @base_url + relative_url, type, body
end
execute_absolute(url, type, body = '', limit = 10) click to toggle source
# File lib/stf/client.rb, line 69
def execute_absolute(url, type, body = '', limit = 10)
  raise ArgumentError, 'too many HTTP redirects' if limit.zero?

  uri          = URI.parse(url)
  http         = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == 'https'
  request      = type.new(uri, 'Authorization' => "Bearer #{@token}", 'Content-Type' => 'application/json')
  request.body = body
  response     = http.request(request)

  case response
  when Net::HTTPSuccess then
    json = JSON.parse(response.body, object_class: OpenStruct)

    logger.debug "API returned #{json}"
  when Net::HTTPRedirection then
    location = response['location']
    logger.debug "redirected to #{location}"
    return execute_absolute(location, type, body, limit - 1)
  else
    logger.error "API returned #{response.value}"
  end

  json
end