Class: JIRA::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/jira/client.rb

Overview

This class is the main access point for all JIRA::Resource instances.

The client must be initialized with a consumer_key and consumer secret, and an optional hash of extra configuration options. The available options are:

:site               => 'http://localhost:2990',
:context_path       => '/jira',
: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",
:private_key_file   => "rsakey.pem",
:rest_base_path     => "/rest/api/2"

See the JIRA::Base class methods for all of the available methods on these accessor objects.

Defined Under Namespace

Classes: UninitializedAccessTokenError

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :site               => 'http://localhost:2990',
  :context_path       => '/jira',
  :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",
  :private_key_file   => "rsakey.pem",
  :rest_base_path     => "/rest/api/2"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(consumer_key, consumer_secret, options = {}) ⇒ Client

Returns a new instance of Client.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jira/client.rb', line 57

def initialize(consumer_key, consumer_secret, options={})
  options = DEFAULT_OPTIONS.merge(options)

  # prepend the context path to all authorization and rest paths
  options[:request_token_path] = options[:context_path] + options[:request_token_path]
  options[:authorize_path] = options[:context_path] + options[:authorize_path]
  options[:access_token_path] = options[:context_path] + options[:access_token_path]
  options[:rest_base_path] = options[:context_path] + options[:rest_base_path]

  @options = options
  @options.freeze
  @consumer = OAuth::Consumer.new(consumer_key,consumer_secret,options)
end

Instance Attribute Details

#consumerObject

The OAuth::Consumer instance used by this client



39
40
41
# File 'lib/jira/client.rb', line 39

def consumer
  @consumer
end

#optionsObject (readonly)

The configuration options for this client instance



42
43
44
# File 'lib/jira/client.rb', line 42

def options
  @options
end

Instance Method Details

#access_tokenObject

Returns the current access token. Raises an JIRA::Client::UninitializedAccessTokenError exception if it is not set.



139
140
141
142
# File 'lib/jira/client.rb', line 139

def access_token
  raise UninitializedAccessTokenError.new unless @access_token
  @access_token
end

#AttachmentObject

:nodoc:



103
104
105
# File 'lib/jira/client.rb', line 103

def Attachment # :nodoc:
  JIRA::Resource::AttachmentFactory.new(self)
end

#CommentObject

:nodoc:



99
100
101
# File 'lib/jira/client.rb', line 99

def Comment # :nodoc:
  JIRA::Resource::CommentFactory.new(self)
end

#ComponentObject

:nodoc:



79
80
81
# File 'lib/jira/client.rb', line 79

def Component # :nodoc:
  JIRA::Resource::ComponentFactory.new(self)
end

#delete(path, headers = {}) ⇒ Object

HTTP methods without a body



145
146
147
# File 'lib/jira/client.rb', line 145

def delete(path, headers = {})
  request(:delete, path,  merge_default_headers(headers))
end

#get(path, headers = {}) ⇒ Object



148
149
150
# File 'lib/jira/client.rb', line 148

def get(path, headers = {})
  request(:get, path, merge_default_headers(headers))
end

#head(path, headers = {}) ⇒ Object



151
152
153
# File 'lib/jira/client.rb', line 151

def head(path, headers = {})
  request(:head, path, merge_default_headers(headers))
end

#init_access_token(params) ⇒ Object

Initialises and returns a new access token from the params hash returned by the OAuth transaction.



128
129
130
# File 'lib/jira/client.rb', line 128

def init_access_token(params)
  @access_token = request_token.get_access_token(params)
end

#IssueObject

:nodoc:



75
76
77
# File 'lib/jira/client.rb', line 75

def Issue # :nodoc:
  JIRA::Resource::IssueFactory.new(self)
end

#IssuetypeObject

:nodoc:



87
88
89
# File 'lib/jira/client.rb', line 87

def Issuetype # :nodoc:
  JIRA::Resource::IssuetypeFactory.new(self)
end

#post(path, body = '', headers = {}) ⇒ Object

HTTP methods with a body



156
157
158
159
# File 'lib/jira/client.rb', line 156

def post(path, body = '', headers = {})
  headers = {'Content-Type' => 'application/json'}.merge(headers)
  request(:post, path, body, merge_default_headers(headers))
end

#PriorityObject

:nodoc:



91
92
93
# File 'lib/jira/client.rb', line 91

def Priority # :nodoc:
  JIRA::Resource::PriorityFactory.new(self)
end

#ProjectObject

:nodoc:



71
72
73
# File 'lib/jira/client.rb', line 71

def Project # :nodoc:
  JIRA::Resource::ProjectFactory.new(self)
end

#put(path, body = '', headers = {}) ⇒ Object



160
161
162
163
# File 'lib/jira/client.rb', line 160

def put(path, body = '', headers = {})
  headers = {'Content-Type' => 'application/json'}.merge(headers)
  request(:put, path, body, merge_default_headers(headers))
end

#request(http_method, path, *arguments) ⇒ Object

Sends the specified HTTP request to the REST API through the OAuth token.

Returns the response if the request was successful (HTTP::2xx) and raises a JIRA::HTTPError if it was not successful, with the response attached.

Raises:



171
172
173
174
175
# File 'lib/jira/client.rb', line 171

def request(http_method, path, *arguments)
  response = access_token.request(http_method, path, *arguments)
  raise HTTPError.new(response) unless response.kind_of?(Net::HTTPSuccess)
  response
end

#request_tokenObject

Returns the current request token if it is set, else it creates and sets a new token.



117
118
119
# File 'lib/jira/client.rb', line 117

def request_token
  @request_token ||= get_request_token
end

#set_access_token(token, secret) ⇒ Object

Sets the access token from a preexisting token and secret.



133
134
135
# File 'lib/jira/client.rb', line 133

def set_access_token(token, secret)
  @access_token = OAuth::AccessToken.new(@consumer, token, secret)
end

#set_request_token(token, secret) ⇒ Object

Sets the request token from a given token and secret.



122
123
124
# File 'lib/jira/client.rb', line 122

def set_request_token(token, secret)
  @request_token = OAuth::RequestToken.new(@consumer, token, secret)
end

#StatusObject

:nodoc:



95
96
97
# File 'lib/jira/client.rb', line 95

def Status # :nodoc:
  JIRA::Resource::StatusFactory.new(self)
end

#UserObject

:nodoc:



83
84
85
# File 'lib/jira/client.rb', line 83

def User # :nodoc:
  JIRA::Resource::UserFactory.new(self)
end

#VersionObject

:nodoc:



111
112
113
# File 'lib/jira/client.rb', line 111

def Version # :nodoc:
  JIRA::Resource::VersionFactory.new(self)
end

#WorklogObject

:nodoc:



107
108
109
# File 'lib/jira/client.rb', line 107

def Worklog # :nodoc:
  JIRA::Resource::WorklogFactory.new(self)
end