Class: Connect
- Inherits:
-
Object
- Object
- Connect
- Defined in:
- lib/jirarest2/connect.rb
Overview
A Connect object encasulates the connection to jira via REST. It takes an Credentials object and returns a Jirarest2::Result object or an exception if something went wrong.
Instance Attribute Summary collapse
-
#credentials ⇒ Object
readonly
Get the credentials.
Instance Method Summary collapse
-
#check_uri ⇒ Boolean
Is the rest API really at the destination we think it is?.
-
#execute(operation, uritail, data) ⇒ Jirarest2::Result
Execute the request.
-
#heal_uri(url = @credentials.connecturl) ⇒ String
Try to be nice.
-
#heal_uri! ⇒ String
try to fix the connecturl of this instance.
-
#initialize(credentials) ⇒ Connect
constructor
Create an instance of Connect.
-
#verify_auth ⇒ Boolean
Verify that we are authenticated.
Constructor Details
#initialize(credentials) ⇒ Connect
Create an instance of Connect.
33 34 35 |
# File 'lib/jirarest2/connect.rb', line 33 def initialize(credentials) @credentials = credentials end |
Instance Attribute Details
#credentials ⇒ Object (readonly)
Get the credentials
29 30 31 |
# File 'lib/jirarest2/connect.rb', line 29 def credentials @credentials end |
Instance Method Details
#check_uri ⇒ Boolean
Is the rest API really at the destination we think it is?
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/jirarest2/connect.rb', line 121 def check_uri begin return execute("Get","dashboard","").code == "200" # TODO is the 404 really possible? rescue Jirarest2::NotFoundError return false rescue Jirarest2::BadRequestError return false end end |
#execute(operation, uritail, data) ⇒ Jirarest2::Result
Execute the request
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/jirarest2/connect.rb', line 52 def execute(operation,uritail,data) uri = nil if (uritail == "auth/latest/session" ) then # this is the exception regarding the base path uristring = @credentials.baseurl+uritail else uristring = @credentials.connecturl+uritail end uristring.gsub!(/\/\//,"/").gsub!(/^(http[s]*:)/,'\1/') uri = URI(uristring) if data != "" then if ! (operation == "Post" || operation == "Put") then # POST carries the payload in the body that's why we have to wait uri.query = URI.encode_www_form(data) end end req = nil req = Net::HTTP::const_get(operation).new(uri.request_uri) # Authentication Header is built up in a credential class @credentials.get_auth_header(req) req["Content-Type"] = "application/json;charset=UTF-8" if data != "" then if (operation == "Post" || operation == "Put") then # POST and PUT carry the payload in the body @payload = data.to_json req.body = @payload end end # Ask the server result = Net::HTTP.start(uri.host, uri.port) {|http| http.request(req) } # deal with output if ((result["x-ausername"] != @credentials.username) && (uritail != "auth/latest/session" )) then # this is not the right authentication verify_auth # make sure end case result when Net::HTTPBadRequest # 400 raise Jirarest2::BadRequestError, result.body when Net::HTTPUnauthorized # 401 No login-credentials oder wrong ones. if @credentials.instance_of?(PasswordCredentials) then raise Jirarest2::PasswordAuthenticationError, result.body elsif @credentials.instance_of?(CookieCredentials) then raise Jirarest2::CookieAuthenticationError, result.body else raise Jirarest2::AuthenticationError, result.body end when Net::HTTPForbidden # 403 if result.get_fields("x-authentication-denied-reason") && result.get_fields("x-authentication-denied-reason")[0] =~ /.*login-url=(.*)/ then #Captcha-Time raise Jirarest2::AuthentificationCaptchaError, $1 else raise Jirarest2::ForbiddenError, result.body end when Net::HTTPNotFound # 404 raise Jirarest2::NotFoundError, result.body when Net::HTTPMethodNotAllowed # 405 raise Jirarest2::MethodNotAllowedError, result.body end ret = Jirarest2::Result.new(result) @credentials.(ret.header["set.cookie"]) if @credentials.instance_of?(CookieCredentials) # Make sure cookies are always up to date if we use them. return ret end |
#heal_uri(url = @credentials.connecturl) ⇒ String
Try to be nice. Parse the URI and see if you can find a pattern to the problem
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/jirarest2/connect.rb', line 135 def heal_uri(url = @credentials.connecturl) splitURI = URI.split(url) # [Scheme,Userinfo,Host,Port,Registry,Path,Opaque,Query,Fragment] splitURI[5].gsub!(/^(.*)2$/,'\12/') splitURI[5].gsub!(/[\/]+/,'/') # get rid of duplicate / splitURI[5].gsub!(/(rest\/api\/2\/)+/,'\1') # duplicate path to rest splitURI[5].gsub!(/^(.*)\/login.jsp(\/rest\/api\/2\/)$/,'\1\2') # dedicated login page splitURI[5].gsub!(/^(.*)\/secure\/Dashboard.jspa(\/rest\/api\/2\/)$/,'\1\2') # copied the dashboard URL (or the login Page) if splitURI[3] then url = splitURI[0].to_s + "://" + splitURI[2].to_s + ":" + splitURI[3].to_s + splitURI[5].to_s else url = splitURI[0].to_s + "://" + splitURI[2].to_s + splitURI[5].to_s end return url end |
#heal_uri! ⇒ String
try to fix the connecturl of this instance
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/jirarest2/connect.rb', line 154 def heal_uri! if ! check_uri then @credentials.connecturl = heal_uri(@credentials.connecturl) end if check_uri then return @credentials.connecturl else raise Jirarest2::CouldNotHealURIError, @credentials.connecturl end end |
#verify_auth ⇒ Boolean
Verify that we are authenticated
167 168 169 170 171 |
# File 'lib/jirarest2/connect.rb', line 167 def verify_auth ret = execute("Get","auth/latest/session","") @credentials. if @credentials.instance_of?(CookieCredentials) && @credentials.autosave return ret.code == "200" end |