Class: Jiralicious::Base
- Inherits:
-
Hashie::Trash
- Object
- Hashie::Trash
- Jiralicious::Base
- 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.
Direct Known Subclasses
Avatar, CustomFieldOption, Field, Issue, Issue::Comment, Issue::Transitions, Issue::Watchers, Project, Project::Avatar, User, User::Avatar
Instance Attribute Summary collapse
-
#loaded ⇒ Object
Used to identify if the class has been loaded.
Class Method Summary collapse
-
.endpoint_name ⇒ Object
Generates the endpoint_name based on the current inheritance class.
-
.fetch(options = {}) ⇒ Object
uses the options to build the URI options necessary to handle the request.
-
.find(key, options = {}) ⇒ Object
Finds the specified key in relation to the current class.
-
.find_all ⇒ Object
(also: all)
Searches for all objects of the inheritance class.
-
.handler ⇒ Object
Configures the default handler.
-
.issueKey_test(key, no_throw = false) ⇒ Object
Validates that the provided key is not malformed.
-
.parent_name ⇒ Object
Generates the parent_name based on the current inheritance class.
Instance Method Summary collapse
-
#all ⇒ Object
Searches for all objects of the inheritance class.
-
#endpoint_name ⇒ Object
Generates the endpoint_name based on the current inheritance class.
-
#loaded? ⇒ Boolean
Returns the the logical form of the loaded member.
-
#method_missing(meth, *args, &block) ⇒ Object
Overrides the default method_missing check.
-
#numeric?(object) ⇒ Boolean
Validates if the provided object is a numeric value.
-
#parent_name ⇒ Object
Generates the parent_name based on the current inheritance class.
-
#properties_from_hash(hash) ⇒ Object
Trash Extention properties_from_hash Adds an underscore (_) before a numeric field.
-
#reload ⇒ Object
Default reload method is blank.
Methods included from Parsers::FieldParser
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
#loaded ⇒ Object
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_name ⇒ Object
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( = {}) [:method] = :get unless [:get, :post, :put, :delete].include?([:method]) [:parent_uri] = "#{parent_name}/#{[:parent_key]}/" unless [:parent].nil? if ![:body_override] [:body_uri] = ([:body].is_a? Hash) ? [:body] : {:body => [:body]} else [:body_uri] = [:body] end if [:body_to_params] [:params_uri] = "?#{[:body].to_params}" unless [:body].nil? || [:body].empty? [:body_uri] = nil end [:url_uri] = [:url].nil? ? "#{Jiralicious.rest_path}/#{[:parent_uri]}#{endpoint_name}/#{[:key]}#{[:params_uri]}" : "#{[:url]}#{[:params_uri]}" Jiralicious.session.request([:method], [:url_uri], :handler => handler, :body => [: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, = {}) response = fetch({:key => key}) if [:reload] == true response else new(response.parsed_response) end end |
.find_all ⇒ Object 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 |
.handler ⇒ Object
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_name ⇒ Object
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
#all ⇒ Object
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_name ⇒ Object
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.
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
222 223 224 |
# File 'lib/jiralicious/base.rb', line 222 def numeric?(object) true if Float(object) rescue false end |
#parent_name ⇒ Object
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 |
#reload ⇒ Object
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 |