Class JIRA::Base
In: lib/jira/base.rb
Parent: Object

This class provides the basic object <-> REST mapping for all JIRA::Resource subclasses, i.e. the Create, Retrieve, Update, Delete lifecycle methods.

Lifecycle methods

Note that not all lifecycle methods are available for all resources, for example some resources cannot be updated or deleted.

Retrieving all resources

  client.Resource.all

Retrieving a single resource

  client.Resource.find(id)

Creating a resource

  resource = client.Resource.build({'name' => '')
  resource.save

Updating a resource

  resource = client.Resource.find(id)
  resource.save('updated_attribute' => 'new value')

Deleting a resource

  resource = client.Resource.find(id)
  resource.delete

Nested resources

Some resources are not defined in the top level of the URL namespace within the JIRA API, but are always nested under the context of another resource. For example, a JIRA::Resource::Comment always belongs to a JIRA::Resource::Issue.

These resources must be indexed and built from an instance of the class they are nested under:

  issue = client.Issue.find(id)
  comments = issue.comments
  new_comment = issue.comments.build

Methods

Constants

QUERY_PARAMS_FOR_SINGLE_FETCH = Set.new [:expand, :fields]
QUERY_PARAMS_FOR_SEARCH = Set.new [:expand, :fields, :startAt, :maxResults]

External Aliases

expanded -> expanded?
deleted -> deleted?

Attributes

attrs  [RW]  The hash of attributes belonging to this instance. An exact representation of the JSON returned from the JIRA API
client  [R]  A reference to the JIRA::Client used to initialize this resource.
deleted  [RW]  Returns true if this instance has been deleted from the server
expanded  [RW]  Returns true if this instance has been fetched from the server

Public Class methods

The class methods are never called directly, they are always invoked from a BaseFactory subclass instance.

Builds a new instance of the resource with the given attributes. These attributes will be posted to the JIRA Api if save is called.

Returns the full path for a collection of this resource. E.g.

  JIRA::Resource::Issue.collection_path
    # => /jira/rest/api/2/issue

Returns the name of this resource for use in URL components. E.g.

  JIRA::Resource::Issue.endpoint_name
    # => issue

Finds and retrieves a resource with the given ID.

Declares that this class contains a collection of another resource within the JSON returned from the JIRA API.

  class Example < JIRA::Base
    has_many :children
  end

  example = client.Example.find(1)
  example.children # Returns an instance of Jira::Resource::HasManyProxy,
                   # which behaves exactly like an array of
                   # Jira::Resource::Child

The following options can be used to override the default behaviour of the relationship:

:attribute_key
The relationship will by default reference a JSON key on the object with the same name as the relationship.
  has_many :children # => {"id":"123",{"children":[{"id":"456"},{"id":"789"}]}}

Use this option if the key in the JSON is named differently.

  # Respond to resource.children, but return the value of resource.attrs['kids']
  has_many :children, :attribute_key => 'kids' # => {"id":"123",{"kids":[{"id":"456"},{"id":"789"}]}}
:class
The class of the child instance will be inferred from the name of the relationship. E.g. has_many :children will return an instance of JIRA::Resource::HasManyProxy containing the collection of JIRA::Resource::Child. Use this option to override the inferred class.
  has_many :children, :class => JIRA::Resource::Kid
:nested_under
In some cases, the JSON return from JIRA is nested deeply for particular relationships. This option allows the nesting to be specified.
  # Specify a single depth of nesting.
  has_many :children, :nested_under => 'foo'
    # => Looks for {"foo":{"children":{}}}
  # Specify deeply nested JSON
  has_many :children, :nested_under => ['foo', 'bar', 'baz']
    # => Looks for {"foo":{"bar":{"baz":{"children":{}}}}}

Declares that this class contains a singular instance of another resource within the JSON returned from the JIRA API.

  class Example < JIRA::Base
    has_one :child
  end

  example = client.Example.find(1)
  example.child # Returns a JIRA::Resource::Child

The following options can be used to override the default behaviour of the relationship:

:attribute_key
The relationship will by default reference a JSON key on the object with the same name as the relationship.
  has_one :child # => {"id":"123",{"child":{"id":"456"}}}

Use this option if the key in the JSON is named differently.

  # Respond to resource.child, but return the value of resource.attrs['kid']
  has_one :child, :attribute_key => 'kid' # => {"id":"123",{"kid":{"id":"456"}}}
:class
The class of the child instance will be inferred from the name of the relationship. E.g. has_one :child will return a JIRA::Resource::Child. Use this option to override the inferred class.
  has_one :child, :class => JIRA::Resource::Kid
:nested_under
In some cases, the JSON return from JIRA is nested deeply for particular relationships. This option allows the nesting to be specified.
  # Specify a single depth of nesting.
  has_one :child, :nested_under => 'foo'
    # => Looks for {"foo":{"child":{}}}
  # Specify deeply nested JSON
  has_one :child, :nested_under => ['foo', 'bar', 'baz']
    # => Looks for {"foo":{"bar":{"baz":{"child":{}}}}}

Returns the attribute name of the attribute used for find. Defaults to :id unless overridden.

Returns the singular path for the resource with the given key. E.g.

  JIRA::Resource::Issue.singular_path('123')
    # => /jira/rest/api/2/issue/123

If a prefix parameter is provided it will be injected between the base path and the endpoint. E.g.

  JIRA::Resource::Comment.singular_path('456','/issue/123/')
    # => /jira/rest/api/2/issue/123/comment/456

Protected Class methods

Public Instance methods

Sends a delete request to the JIRA Api and sets the deleted instance variable on the object to true.

Fetches the attributes for the specified resource from JIRA unless the resource is already expanded and the optional force reload flag is not set

Each resource has a unique key attribute, this method returns the value of that key for this instance.

Overrides method_missing to check the attribute hash for resources matching method_name and proxies the call to the superclass if no match is found.

Determines if the resource is newly created by checking whether its key_value is set. If it is nil, the record is new and the method will return true.

This method fixes issue that there is no / prefix in url. It is happened when we call for instance Looks like this issue is actual only in case if you use atlassian sdk your app path is not root (like /jira in example below) issue.save() for existing resource. As a result we got error 400 from JIRA API:

07/Jun/2015:15:32:19 +0400
"PUT jira/rest/api/2/issue/10111 HTTP/1.1" 400 -

After applying this fix we have normal response:

07/Jun/2015:15:17:18 +0400
"PUT /jira/rest/api/2/issue/10111 HTTP/1.1" 204 -

This returns the URL path component that is specific to this instance, for example for Issue id 123 it returns ’/issue/123’. For an unsaved issue it returns ’/issue‘

Checks if method_name is set in the attributes hash and returns true when found, otherwise proxies the call to the superclass.

Saves the specified resource attributes by sending either a POST or PUT request to JIRA, depending on resource.new_record?

Accepts an attributes hash of the values to be saved. Will return false if the request fails.

Saves the specified resource attributes by sending either a POST or PUT request to JIRA, depending on resource.new_record?

Accepts an attributes hash of the values to be saved. Will throw a JIRA::HTTPError if the request fails (response is not HTTP 2xx).

Set the current attributes from a hash. If clobber is true, any existing hash values will be clobbered by the new hash, otherwise the hash will be deeply merged into attrs. The target paramater is for internal use only and should not be used.

Sets the attributes hash from a HTTPResponse object from JIRA if it is not nil or is not a json response.

Returns a JSON representation of the current attributes hash.

Returns a symbol for the given instance, for example JIRA::Resource::Issue returns :issue

Protected Instance methods

This allows conditional lookup of possibly nested attributes. Example usage:

  maybe_nested_attribute('foo')                 # => @attrs['foo']
  maybe_nested_attribute('foo', 'bar')          # => @attrs['bar']['foo']
  maybe_nested_attribute('foo', ['bar', 'baz']) # => @attrs['bar']['baz']['foo']

[Validate]