Class: Peaty::Base

Inherits:
Object show all
Defined in:
lib/peaty/base.rb

Direct Known Subclasses

Iteration, Project, Story, User

Constant Summary collapse

FILTERS =
[
  :id, :type, :state, :label, :has_attachment,
  :created_since, :modified_since,
  :requester, :owner,
  :mywork,
  :integration, :external_id, :has_external_id,
  :includedone
]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
# File 'lib/peaty/base.rb', line 15

def initialize(attrs)
  raise ArgumentError unless attrs.is_a?(Hash)
  @connection = self.class.connection
  # if we get a hash like {"item"=>{...}}, pull out the attributes
  @attributes = if attrs.key?(self.class.element);  attrs.dup.delete(self.class.element) 
                else                                attrs.dup
                end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/peaty/base.rb', line 28

def method_missing(method, *args)
  method.to_s =~ /^([^\?\=]+)(\?|\=)?$/
  method_name, predicate = $1, $2
  
  case predicate
  when '?'; return self.attributes[method_name].present?
  when '='; return self.attributes[method_name] = args.first
  else
    return self.attributes[method_name] if respond_to?(method_name)
  end
  
  super
end

Class Attribute Details

.connectionObject

Returns the value of attribute connection.



67
68
69
# File 'lib/peaty/base.rb', line 67

def connection
  @connection
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/peaty/base.rb', line 4

def attributes
  @attributes
end

#connectionObject

Returns the value of attribute connection.



4
5
6
# File 'lib/peaty/base.rb', line 4

def connection
  @connection
end

#errorObject

Returns the value of attribute error.



4
5
6
# File 'lib/peaty/base.rb', line 4

def error
  @error
end

Class Method Details

.all(options = {}) ⇒ Object



99
100
101
102
# File 'lib/peaty/base.rb', line 99

def all(options = {})
  self.parse(self.connection[self.collection_path(options)].get(:params => self.filter_options(options)).body, self.element).
    each { |e| e.connection = self.connection }
end

.build(attrs = {}) ⇒ Object



24
25
26
# File 'lib/peaty/base.rb', line 24

def self.build(attrs = {})
  new(attrs)
end

.filter_options(options = {}, filter = []) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/peaty/base.rb', line 108

def filter_options(options = {}, filter = [])
  options = options.dup # make sure we're working on a copy
  # and delete any keys not supported for queries
  options.each { |(k,_)| options.delete(k) unless FILTERS.include?(k.to_sym) }
  
  FILTERS.each do |term|
    value = Array.wrap(options.delete(term))
    filter << "%s:%s" % [ term,
                          value.map do |v|
                            v = %("%s") % v if v.to_s =~ /\s/
                            v
                          end.join(',') ] unless value.empty?
  end
  
  # handle the rest of the filter strings
  Array.wrap(options.delete(:rest)).each do |value|
    value = %("%s") % value if value.to_s =~ /\s/
    filter << value
  end
  
  return options if filter.empty?
  options.merge(:filter => filter.join(" "))
end

.find(*args) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/peaty/base.rb', line 81

def find(*args)
  options = args.extract_options!
  selection = args.shift
  case selection
  when :first;  self.first(options)
  when :all;    self.all(options)
  when Array;   selection.map{ |s| self.find_by_id(s, options) }
  when Numeric; self.find_by_id(selection, options)
  else          self.find_by_id(selection, options)
  end
end

.find_by_id(id, options = {}) ⇒ Object



93
94
95
96
97
# File 'lib/peaty/base.rb', line 93

def find_by_id(id, options = {})
  self.parse(self.connection[self.member_path(id, options)].get(:params => self.filter_options(options)).body, self.element).
    first.
    tap{ |e| e.connection = self.connection }
end

.first(options = {}) ⇒ Object



104
105
106
# File 'lib/peaty/base.rb', line 104

def first(options = {})
  self.all(options).first
end

.parse(response, element) ⇒ Object

Takes the XML result, transforms to JSON, parses to objects, and returns an array of results, regardless of one or many results.



76
77
78
79
# File 'lib/peaty/base.rb', line 76

def parse(response, element)
  result = JSON.parse(XmlToJson.transform(response))
  Array.wrap(result[element] || result[element.pluralize]).map{ |r| new(r) }
end

.with_connection(connection) ⇒ Object



69
70
71
72
# File 'lib/peaty/base.rb', line 69

def with_connection(connection)
  @connection = connection
  self # chaining
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/peaty/base.rb', line 53

def error?
  !!@error
end

#idObject



45
46
47
# File 'lib/peaty/base.rb', line 45

def id
  self.attributes["id"]
end

#new_record?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/peaty/base.rb', line 49

def new_record?
  id.nil? or id.to_i.zero?
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to?(method)
  super or self.attributes.key?(method.to_s)
end

#saveObject



57
58
59
60
61
62
63
64
# File 'lib/peaty/base.rb', line 57

def save
  @error = nil # reset error
  @attributes.delete_if{ |k, v| v.nil? } # ignore nil attributes
  @attributes.replace self.class.parse(self.connection[self.class.collection_path(@attributes)].post(self.class.element => @attributes).body, self.class.element).first.attributes
rescue RestClient::UnprocessableEntity => e
  @error = JSON.parse(XmlToJson.transform(e.response.body))["message"]
  false
end