lib/jiralicious/session.rb in jiralicious-0.0.6 vs lib/jiralicious/session.rb in jiralicious-0.1.0

- old
+ new

@@ -2,99 +2,42 @@ require 'jiralicious/configuration' module Jiralicious class Session include HTTParty - attr_accessor :session, :login_info + + format :json headers 'Content-Type' => 'application/json' - def alive? - @session && @login_info - end - - def login - response = perform_request(:authenticating => true) do - self.class.post('/rest/auth/latest/session', - :body => { - :username => Jiralicious.username, - :password => Jiralicious.password}.to_json) - end - - if response.code == 200 - response = JSON.parse(response.body) - @session = response["session"] - @login_info = response["loginInfo"] - self.class.cookies({self.session["name"] => self.session["value"]}) + def request(method, *options) + if options.last.is_a?(Hash) && options.last[:handler] + response_handler = options.last.delete(:handler) else - clear_session - case response.code - when 401 then - raise Jiralicious::InvalidLogin.new("Invalid login") - when 403 - raise Jiralicious::CaptchaRequired.new("Captacha is required. Try logging into Jira via the web interface") - else - # Give Net::HTTP reason - raise Jiralicious::JiraError.new(response.response.message) - end + response_handler = handler end - end - def logout - response = perform_request do - self.class.delete('/rest/auth/latest/session') - end - - if response.code == 204 - clear_session - else - case response.code - when 401 then - raise Jiralicious::NotLoggedIn.new("Not logged in") - else - # Give Net::HTTP reason - raise Jiralicious::JiraError.new(response.response.message) - end - end - end - - def perform_request(options = {}, &block) self.class.base_uri Jiralicious.uri - self.login if require_login? && !options[:authenticating] + before_request if respond_to?(:before_request) + response = self.class.send(method, *options) + after_request(response) if respond_to?(:after_request) - response = block.call - unless options[:authenticating] - if captcha_required(response) - raise Jiralicious::CaptchaRequired. - new("Captacha is required. Try logging into Jira via the web interface") - elsif cookie_invalid(response) - # Can usually be fixed by logging in again - clear_session - self.login - response = block.call - raise Jiralicious::CookieExpired if cookie_invalid(response) - end - end - response + response_handler.call(response) end private - def captcha_required(response) - response.code == 401 && - # Fakeweb lowercases headers automatically. :( - (response.headers["X-Seraph-LoginReason"] == "AUTHENTICATION_DENIED" || - response.headers["x-seraph-loginreason"] == "AUTHENTICATION_DENIED") - end - - def cookie_invalid(response) - response.code == 401 && response.body =~ /cookie/i - end - - def require_login? - !(Jiralicious.username.empty? && Jiralicious.password.empty?) && !alive? - end - - def clear_session - @session = @login_info = nil + def handler + Proc.new do |response| + case response + when 200..204 + response.body + else + message = response.body + if message.is_a?(Hash) + message = message['errorMessages'].join('\n') + end + Jiralicious::JiraError.new(message) + end + end end end end