Class: Jiralicious::OauthSession

Inherits:
OAuth::AccessToken
  • Object
show all
Defined in:
lib/jiralicious/oauth_session.rb

Overview

The OauthSesion class extends the default OAuth::AccessToken The functions herein convert between the current Jiralicious and HTTParty formats and those required by OAuth. This is a Bidirectional conversion.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token = nil, secret = nil) ⇒ OauthSession

Initializer extends the functionality of the basic OAuth::AccessToken However provides the base functionality to handle the initial root generation for the custom Jiralicious authentication to JIRA



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jiralicious/oauth_session.rb', line 28

def initialize(token = nil, secret = nil)
	self.option = {
		:signature_method => 'RSA-SHA1',
		:request_token_path => '/plugins/servlet/oauth/request-token',
		:authorize_path => "/plugins/servlet/oauth/authorize",
		:access_token_path => '/plugins/servlet/oauth/access-token',
		:site => "http://rome:8080"
	}
	if (token.nil? || secret.nil?)
		consumer = OAuth::Consumer.new(Jiralicious.oauth_consumer_key, OpenSSL::PKey::RSA.new(get_secret, Jiralicious.oauth_pass_phrase.to_s), self.option)
		request_token = consumer.get_request_token
		## request access confirmation ##
		bs = Jiralicious::BasicSession.new
		bsr = bs.request(:get, request_token.authorize_url)
		bsp = HTTParty::Parser.new(bsr.message, :html)
		bsb = Nokogiri::HTML(bsp.body)
		## Parse confirm page and send form ##
		a = {}
		bsb.xpath('//input').each do |input|
			if (input.get_attribute('name') != 'deny' && !input.get_attribute('name').nil?)
				a.merge!({input.get_attribute('name').to_sym => input.get_attribute('value')})
			end
		end
		urip = "#{request_token.authorize_url.split('?')[0]}?#{build_body(a)}"
		bsr = bs.request(bsb.xpath('//form')[0].get_attribute('method').downcase.to_sym, urip)
		## Parse response for access ##
		bss = bsr.message.split("'") # brute force method don't know a better way to do this
		crt = request_token.consumer.token_request(request_token.consumer.http_method, (request_token.consumer.access_token_url? ? request_token.consumer.access_token_url : request_token.consumer.access_token_path), request_token, {:oauth_verifier => bss[3]})
		super(request_token.consumer, crt[:oauth_token], crt[:oauth_token_secret])
		self.params = crt
	else
		super(token, secret)
	end
end

Instance Attribute Details

#optionObject

Returns the value of attribute option.



12
13
14
# File 'lib/jiralicious/oauth_session.rb', line 12

def option
  @option
end

Instance Method Details

#after_request(response) ⇒ Object

After Request reprocesses the response provided in to the HTTParty::Response format that is expected by the rest of the gem.



19
20
21
# File 'lib/jiralicious/oauth_session.rb', line 19

def after_request(response)
	@response = HTTParty::Response.new(self, response, lambda { HTTParty::Parser.new(response.body, Jiralicious::Session.format).parse }, :body => response.body)
end

#request(method, *options) ⇒ Object

Main access method to request data from the Jira API

Arguments

:method (required) http method type

:options (required) request specific options



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jiralicious/oauth_session.rb', line 71

def request(method, *options)
	if options.last.is_a?(Hash) && options.last[:handler]
		response_handler = options.last.delete(:handler)
	else
		response_handler = handler
	end
	path = options.first
	options = options.last
	before_request if respond_to?(:before_request)
	super(method, path, *options)
	after_request(response) if respond_to?(:after_request)

	response_handler.call(response)
end