class Swiftrail::Testrail::Api::Client

Attributes

conn[R]
password[R]
run_id[R]
username[R]

Public Class Methods

new(base_url, username, password, run_id) click to toggle source
# File lib/swiftrail/testrail/api/client.rb, line 12
def initialize(base_url, username, password, run_id)
  @conn = connection(URI.parse(base_url))
  @run_id = run_id
  @username = username
  @password = password
end

Public Instance Methods

all_tests() click to toggle source

@return [TestRailTest]

# File lib/swiftrail/testrail/api/client.rb, line 30
def all_tests
  response = conn.request(get_request(tests_for_run_path))

  raise Error::InvalidRequest.new(response) if response.code == '400'
  raise Error::NoPermission(response) if response.code == '403'
  raise Error::Unknown(response), response.code unless response.code == '200'

  JSON.parse(response.body).map do |item|
    TestRailTest.from_json(item)
  end
end
publish_results(results) click to toggle source
# File lib/swiftrail/testrail/api/client.rb, line 19
def publish_results(results)
  response = conn.request(post_request(publish_result_path, results))

  raise Error::InvalidRequest.new(response) if response.code == '400'
  raise Error::NoPermission.new(response) if response.code == '403'
  raise Error::Unknown.new(response), response.code unless response.code == '200'

  true
end

Private Instance Methods

connection(url) click to toggle source
# File lib/swiftrail/testrail/api/client.rb, line 46
def connection(url)
  conn = Net::HTTP.new(url.host, url.port)
  conn.use_ssl = true
  conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
  conn
end
get_request(path) click to toggle source
# File lib/swiftrail/testrail/api/client.rb, line 61
def get_request(path)
  request = Net::HTTP::Get.new(path)
  request.basic_auth(username, password)
  request.add_field('Content-Type', 'application/json')
  request
end
post_json(results) click to toggle source
# File lib/swiftrail/testrail/api/client.rb, line 68
def post_json(results)
  { results: results.map(&:to_json) }
end
post_request(path, results) click to toggle source
# File lib/swiftrail/testrail/api/client.rb, line 53
def post_request(path, results)
  request = Net::HTTP::Post.new(path)
  request.body = JSON.dump(post_json(results))
  request.basic_auth(username, password)
  request.add_field('Content-Type', 'application/json')
  request
end
publish_result_path() click to toggle source
# File lib/swiftrail/testrail/api/client.rb, line 72
def publish_result_path
  "/index.php?/api/v2/add_results_for_cases/#{run_id}"
end
tests_for_run_path() click to toggle source
# File lib/swiftrail/testrail/api/client.rb, line 76
def tests_for_run_path
  "/index.php?/api/v2/get_tests/#{run_id}"
end