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',
:signature_method   => 'RSA-SHA1',
:request_token_path => "/jira/plugins/servlet/oauth/request-token",
:authorize_path     => "/jira/plugins/servlet/oauth/authorize",
:access_token_path  => "/jira/plugins/servlet/oauth/access-token",
:private_key_file   => "rsakey.pem",
:rest_base_path     => "/jira/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',
  :signature_method   => 'RSA-SHA1',
  :request_token_path => "/jira/plugins/servlet/oauth/request-token",
  :authorize_path     => "/jira/plugins/servlet/oauth/authorize",
  :access_token_path  => "/jira/plugins/servlet/oauth/access-token",
  :private_key_file   => "rsakey.pem",
  :rest_base_path     => "/jira/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.



55
56
57
58
59
60
61
# File 'lib/jira/client.rb', line 55

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

  @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



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

def consumer
  @consumer
end

#optionsObject (readonly)

The configuration options for this client instance



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

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.



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

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

#AttachmentObject

:nodoc:



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

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

#CommentObject

:nodoc:



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

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

#ComponentObject

:nodoc:



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

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

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

HTTP methods without a body



137
138
139
# File 'lib/jira/client.rb', line 137

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

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



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

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

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



143
144
145
# File 'lib/jira/client.rb', line 143

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.



120
121
122
# File 'lib/jira/client.rb', line 120

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

#IssueObject

:nodoc:



67
68
69
# File 'lib/jira/client.rb', line 67

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

#IssuetypeObject

:nodoc:



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

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

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

HTTP methods with a body



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

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

#PriorityObject

:nodoc:



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

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

#ProjectObject

:nodoc:



63
64
65
# File 'lib/jira/client.rb', line 63

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

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



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

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:



163
164
165
166
167
# File 'lib/jira/client.rb', line 163

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.



109
110
111
# File 'lib/jira/client.rb', line 109

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.



125
126
127
# File 'lib/jira/client.rb', line 125

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.



114
115
116
# File 'lib/jira/client.rb', line 114

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

#StatusObject

:nodoc:



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

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

#UserObject

:nodoc:



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

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

#VersionObject

:nodoc:



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

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

#WorklogObject

:nodoc:



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

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