class TestdroidApi::Client

Constants

CLOUD_URL
USERS_URL

Public Class Methods

new(username, password) click to toggle source

Initialize a Client object with TestDroid credentials

@param username [String] Username @param password [String] Password

# File lib/testdroid-api/client.rb, line 10
def initialize(username, password)
  @username = username
  @password = password
  @api_key  = ""
end

Public Instance Methods

authenticate!() click to toggle source

Authenticate client and retrieve apiKey @return [String] token

# File lib/testdroid-api/client.rb, line 18
def authenticate!
  response = post(USERS_URL, { "email" => @username, "password" => @password})
  raise 'Could not authenticate, are you sure you have the right credentials?' if !response['secretApiKey']

  @api_key = response['secretApiKey']
end
create_project(name, description) click to toggle source

Create new project @param name [String] name of the project @param description [String] project's description @return [TestdroidApi::Client::Project]

# File lib/testdroid-api/client.rb, line 38
def create_project(name, description)
  config  = post_api_request('projects', { :name => name, :description => description })

  Project.new(self, config)
end
device_groups(name = nil) click to toggle source

Get user’s clusters @param name [String] if given only matching device clusters will be returned @return [Array<TestdroidApi::Client::DeviceGroup>]

# File lib/testdroid-api/client.rb, line 47
def device_groups(name = nil)
  devices = get_api_request('clusters')

  name ? find_device_groups_by(name, devices) : create_device_groups_from(devices)
end
get_api_request(endpoint, resource_name = endpoint) click to toggle source

@api private

# File lib/testdroid-api/client.rb, line 60
def get_api_request(endpoint, resource_name = endpoint)
  check_api_key
  get(get_endpoint(endpoint), get_auth_header(resource_name) )
end
get_file(endpoint, resource_name = endpoint) click to toggle source

@api private

# File lib/testdroid-api/client.rb, line 66
def get_file(endpoint, resource_name = endpoint)
  check_api_key
  RestClient.get(get_endpoint(endpoint), get_auth_header(resource_name))
end
post_api_request(endpoint, params = nil, resource_name = endpoint, extra_headers = {}) click to toggle source

@api private

# File lib/testdroid-api/client.rb, line 54
def post_api_request(endpoint, params = nil, resource_name = endpoint, extra_headers = {})
  check_api_key
  post(get_endpoint(endpoint), params, get_auth_header(resource_name).merge(extra_headers))
end
projects(name = nil) click to toggle source

List all projects @param name [String] project name to match @return [Array<TestdroidApi::Client::Project>]

# File lib/testdroid-api/client.rb, line 28
def projects(name = nil)
  configs = get_api_request('projects')

  name ? find_projects_by(name, configs) : create_projects_from(configs)
end

Private Instance Methods

check_api_key() click to toggle source
# File lib/testdroid-api/client.rb, line 119
def check_api_key
  raise("Are you sure you've authenticated?") if @api_key.empty?
end
create_device_groups_from(devices) click to toggle source
# File lib/testdroid-api/client.rb, line 113
def create_device_groups_from(devices)
  devices.map{|group|
    DeviceGroup.new(self, group)
  }
end
create_projects_from(configs) click to toggle source
# File lib/testdroid-api/client.rb, line 95
def create_projects_from(configs)
  configs.map{|project_config|
    Project.new(self, project_config)
  }
end
find_device_groups_by(name, devices) click to toggle source
# File lib/testdroid-api/client.rb, line 107
def find_device_groups_by(name, devices)
  create_device_groups_from(devices).delete_if{|group|
    group.display_name != name
  }
end
find_projects_by(name, configs) click to toggle source
# File lib/testdroid-api/client.rb, line 101
def find_projects_by(name, configs)
  create_projects_from(configs).delete_if{|project|
    project.name != name
  }
end
get(url, params=nil) click to toggle source
# File lib/testdroid-api/client.rb, line 77
def get(url, params=nil)
  JSON.parse(RestClient.get(url, params))
end
get_auth_header(resourceName) click to toggle source
# File lib/testdroid-api/client.rb, line 81
def get_auth_header(resourceName)
    nonce = get_nonce
    digestdata = @api_key + ":" + nonce + ":" + resourceName
    digest = Digest::SHA256.hexdigest(digestdata)
    {'X-Testdroid-Authentication' => @username + " " + nonce + " " + digest}
end
get_endpoint(name) click to toggle source
# File lib/testdroid-api/client.rb, line 123
def get_endpoint(name)
  "#{CLOUD_URL}/api/v1/#{name}"
end
get_nonce() click to toggle source
# File lib/testdroid-api/client.rb, line 88
def get_nonce
    chars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'
    password = ''
    6.times do password << chars[rand(chars.size)] end
    password
end
post(url, params, headers = nil) click to toggle source
# File lib/testdroid-api/client.rb, line 73
def post(url, params, headers = nil)
    JSON.parse(RestClient.post(url, params, headers))
  end