Class: Jiralicious::Base

Inherits:
Hashie::Trash
  • Object
show all
Includes:
Parsers::FieldParser
Defined in:
lib/jiralicious/base.rb

Overview

The Base class encapsulates all of the default functionality necessary in order to properly manage the Hashie::Trash object within the Jiralicious framework.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parsers::FieldParser

#parse!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Overrides the default method_missing check. This override is used in lazy loading to ensure that the requested field or method is truly unavailable.

Arguments

:meth (system)

:args (system)

:block (system)



206
207
208
209
210
211
212
213
214
# File 'lib/jiralicious/base.rb', line 206

def method_missing(meth, *args, &block)
  if !loaded?
    self.loaded = true
    reload
    self.send(meth, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#loadedObject

Used to identify if the class has been loaded



18
19
20
# File 'lib/jiralicious/base.rb', line 18

def loaded
  @loaded
end

Class Method Details

.endpoint_nameObject

Generates the endpoint_name based on the current inheritance class.



72
73
74
# File 'lib/jiralicious/base.rb', line 72

def endpoint_name
  self.name.split('::').last.downcase
end

.fetch(options = {}) ⇒ Object

uses the options to build the URI options necessary to handle the request. Some options are defaulted if not explicit while others are only necessary under specific conditions.

Arguments

:key (optional) key of object to fetch

:method (optional) limited to the standard request types default of :get

:parent (optional) boolean will the parent object be used

:parent_key (optional) parent’s key (must set :parent to use)

:body (optional) fields to be sent with the fetch

:body_override (optional) corrects issues in :body if set

:body_to_params (optional) forces body to be appended to URI

:url (optional) overrides auto generated URI with custom URI



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jiralicious/base.rb', line 107

def fetch(options = {})
  options[:method] = :get unless [:get, :post, :put, :delete].include?(options[:method])
  options[:parent_uri] = "#{parent_name}/#{options[:parent_key]}/" unless options[:parent].nil?
  if !options[:body_override]
    options[:body_uri] = (options[:body].is_a? Hash) ? options[:body] : {:body => options[:body]}
  else
    options[:body_uri] = options[:body]
  end
  if options[:body_to_params]
    options[:params_uri] = "?#{options[:body].to_params}" unless options[:body].nil? || options[:body].empty?
    options[:body_uri] = nil
  end
  options[:url_uri] = options[:url].nil? ? "#{Jiralicious.rest_path}/#{options[:parent_uri]}#{endpoint_name}/#{options[:key]}#{options[:params_uri]}" : "#{options[:url]}#{options[:params_uri]}"
  Jiralicious.session.request(options[:method], options[:url_uri], :handler => handler, :body => options[:body_uri].to_json)
end

.find(key, options = {}) ⇒ Object

Finds the specified key in relation to the current class. This is based on the inheritance and will create an error if called from the Base Class directly.

Arguments

:key (required) object key to find

:reload (required) is object reloading forced



49
50
51
52
53
54
55
56
# File 'lib/jiralicious/base.rb', line 49

def find(key, options = {})
  response = fetch({:key => key})
  if options[:reload] == true
    response
  else
    new(response.parsed_response)
  end
end

.find_allObject Also known as: all

Searches for all objects of the inheritance class. This method can create very large datasets and is not recommended for any request that could slow down either Jira or the Ruby application.



64
65
66
67
# File 'lib/jiralicious/base.rb', line 64

def find_all
  response = fetch()
  new(response)
end

.handlerObject

Configures the default handler. This can be overridden in the child class to provide additional error handling.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/jiralicious/base.rb', line 127

def handler
  Proc.new do |response|
    case response.code
    when 200..204
      response
    when 400
      raise Jiralicious::TransitionError.new(response.inspect)
    when 404
      raise Jiralicious::IssueNotFound.new(response.inspect)
    else
      raise Jiralicious::JiraError.new(response.inspect)
    end
  end
end

.issueKey_test(key, no_throw = false) ⇒ Object

Validates that the provided key is not malformed



146
147
148
149
150
151
152
# File 'lib/jiralicious/base.rb', line 146

def issueKey_test(key, no_throw = false)
  if key.nil? || !(/^[A-Z]+-[0-9]+$/i =~ key)
    raise Jiralicious::JiraError.new("The key #{key} is invalid") unless no_throw
    return false
  end
  return true
end

.parent_nameObject

Generates the parent_name based on the current inheritance class.



79
80
81
82
# File 'lib/jiralicious/base.rb', line 79

def parent_name
  arr = self.name.split('::')
  arr[arr.length-2].downcase
end

Instance Method Details

#allObject

Searches for all objects of the inheritance class. This method can create very large datasets and is not recommended for any request that could slow down either Jira or the Ruby application.



176
177
178
# File 'lib/jiralicious/base.rb', line 176

def all
  self.class.all
end

#endpoint_nameObject

Generates the endpoint_name based on the current inheritance class.



160
161
162
# File 'lib/jiralicious/base.rb', line 160

def endpoint_name
  self.class.endpoint_name
end

#loaded?Boolean

Returns the the logical form of the loaded member. This used to determine if the object is loaded and ready for usage.

Returns:

  • (Boolean)


184
185
186
# File 'lib/jiralicious/base.rb', line 184

def loaded?
  !!self.loaded
end

#numeric?(object) ⇒ Boolean

Validates if the provided object is a numeric value

Arguments

:object (required) object to be tested

Returns:

  • (Boolean)


222
223
224
# File 'lib/jiralicious/base.rb', line 222

def numeric?(object)
  true if Float(object) rescue false
end

#parent_nameObject

Generates the parent_name based on the current inheritance class.



167
168
169
# File 'lib/jiralicious/base.rb', line 167

def parent_name
  self.class.parent_name
end

#properties_from_hash(hash) ⇒ Object

Trash Extention properties_from_hash Adds an underscore (_) before a numeric field. This ensures that numeric fields will be treated as strings.

Arguments

:hash (required) hash to be added to properties



28
29
30
31
32
33
34
35
36
# File 'lib/jiralicious/base.rb', line 28

def properties_from_hash(hash)
  hash.inject({}) do |newhash, (k, v)|
    k = k.gsub("-", "_")
    k = "_#{k.to_s}" if k =~ /^\d/
    self.class.property :"#{k}"
    newhash[k] = v
    newhash
  end
end

#reloadObject

Default reload method is blank. For classes that implement lazy loading this method will be overridden with the necessary functionality.



192
193
# File 'lib/jiralicious/base.rb', line 192

def reload
end