Last active
April 14, 2019 06:46
-
-
Save prashanth-sams/e3d3a8bedbd07985875926f68e7707fa to your computer and use it in GitHub Desktop.
TestRail Ruby API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# TestRail API binding for Ruby (API v2, available since TestRail 3.0) | |
# | |
# Learn more: | |
# | |
# http://docs.gurock.com/testrail-api2/start | |
# http://docs.gurock.com/testrail-api2/accessing | |
# | |
# Copyright Gurock Software GmbH. See license.md for details. | |
# | |
require 'net/http' | |
require 'net/https' | |
require 'uri' | |
require 'json' | |
module TestRail | |
class APIClient | |
@url = '' | |
@user = '' | |
@password = '' | |
attr_accessor :user | |
attr_accessor :password | |
def initialize(base_url) | |
if !base_url.match(/\/$/) | |
base_url += '/' | |
end | |
@url = base_url + 'index.php?/api/v2/' | |
end | |
def send_get(uri) | |
_send_request('GET', uri, nil) | |
end | |
def send_post(uri, data) | |
_send_request('POST', uri, data) | |
end | |
private | |
def _send_request(method, uri, data) | |
url = URI.parse(@url + uri) | |
if method == 'POST' | |
request = Net::HTTP::Post.new(url.path + '?' + url.query) | |
request.body = JSON.dump(data) | |
else | |
request = Net::HTTP::Get.new(url.path + '?' + url.query) | |
end | |
request.basic_auth(@user, @password) | |
request.add_field('Content-Type', 'application/json') | |
conn = Net::HTTP.new(url.host, url.port) | |
if url.scheme == 'https' | |
conn.use_ssl = true | |
conn.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
end | |
response = conn.request(request) | |
if response.body && !response.body.empty? | |
result = JSON.parse(response.body) | |
else | |
result = {} | |
end | |
if response.code != '200' | |
if result && result.key?('error') | |
error = '"' + result['error'] + '"' | |
else | |
error = 'No additional error message received' | |
end | |
raise APIError.new('TestRail API returned HTTP %s (%s)' % | |
[response.code, error]) | |
end | |
result | |
end | |
end | |
class APIError < StandardError | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment