Class: Jiralicious::Base

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

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



100
101
102
103
104
105
106
107
108
# File 'lib/jiralicious/base.rb', line 100

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

Returns the value of attribute loaded.



8
9
10
# File 'lib/jiralicious/base.rb', line 8

def loaded
  @loaded
end

Class Method Details

.endpoint_nameObject



37
38
39
# File 'lib/jiralicious/base.rb', line 37

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

.fetch(options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jiralicious/base.rb', line 46

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]
  Jiralicious.session.request(options[:method], options[:url_uri], :handler => handler, :body => options[:body_uri].to_json)
end

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



23
24
25
26
27
28
29
30
# File 'lib/jiralicious/base.rb', line 23

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



32
33
34
35
# File 'lib/jiralicious/base.rb', line 32

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

.handlerObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/jiralicious/base.rb', line 62

def handler
  Proc.new do |response|
    case response.code
    when 200..204
      response
    when 400
      raise Jiralicious::TransitionError.new(response['errorMessages'].join('\n'))
    when 404
      raise Jiralicious::IssueNotFound.new(response['errorMessages'].join('\n'))
    else
      raise Jiralicious::JiraError.new(response['errorMessages'].join('\n'))
    end
  end
end

.parent_nameObject



41
42
43
44
# File 'lib/jiralicious/base.rb', line 41

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

Instance Method Details

#allObject



89
90
91
# File 'lib/jiralicious/base.rb', line 89

def all
  self.class.all
end

#endpoint_nameObject

Instance Methods ###



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

def endpoint_name
  self.class.endpoint_name
end

#loaded?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/jiralicious/base.rb', line 93

def loaded?
  self.loaded
end

#numeric?(object) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/jiralicious/base.rb', line 110

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

#parent_nameObject



85
86
87
# File 'lib/jiralicious/base.rb', line 85

def parent_name
  self.class.parent_name
end

#properties_from_hash(hash) ⇒ Object

Trash Extention Methods ###



11
12
13
14
15
16
17
18
19
# File 'lib/jiralicious/base.rb', line 11

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



97
98
# File 'lib/jiralicious/base.rb', line 97

def reload
end