Class: Point::Base

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

Direct Known Subclasses

Zone, ZoneRecord

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params) ⇒ Object

 Pass any methods via. the attributes hash to see if they exist  before resuming normal method_missing behaviour



9
10
11
12
13
14
15
16
17
18
# File 'lib/point/base.rb', line 9

def method_missing(method, *params)
  set = method.to_s.include?('=')
  key = method.to_s.sub('=', '')
  self.attributes = Hash.new unless self.attributes.is_a?(Hash)
  if set
    self.attributes[key] = params.first
  else
    self.attributes[key]
  end
end

Instance Attribute Details

#attributesObject

 Store all attributes for the model we’re working with.



5
6
7
# File 'lib/point/base.rb', line 5

def attributes
  @attributes
end

#errorsObject

 Store all attributes for the model we’re working with.



5
6
7
# File 'lib/point/base.rb', line 5

def errors
  @errors
end

#idObject

 Store all attributes for the model we’re working with.



5
6
7
# File 'lib/point/base.rb', line 5

def id
  @id
end

Class Method Details

.class_nameObject

Return the point class name



68
69
70
# File 'lib/point/base.rb', line 68

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

.collection_path(params = {}) ⇒ Object

 Return the collection path for this model. Very lazy pluralizion here  at the moment, nothing in Point needs to be pluralized with anything  other than just adding an ‘s’.



58
59
60
# File 'lib/point/base.rb', line 58

def collection_path(params = {})
  class_name.downcase + 's'
end

.find(type, params = {}) ⇒ Object

Find a record or set of records. Passing :all will return all records and passing an integer will return the individual record for the ID passed.



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

def find(type, params = {})
  case type
  when :all then find_all(params)
  when Integer then find_single(type, params)
  when String then find_all(params).select {|r| r.attributes["name"] == type}.first
  else raise Point::Error, "Find requires :all, a string or an integer."
  end
end

.find_all(params) ⇒ Object

 Find all objects and return an array of objects with the attributes set.



34
35
36
37
38
# File 'lib/point/base.rb', line 34

def find_all(params)
  JSON.parse(Request.new(collection_path(params)).make.output).map do |o|
    create_object(o[class_name.downcase], params)
  end
end

.find_single(id, params = {}) ⇒ Object

Find a single object and return an object for it.



41
42
43
44
45
46
47
48
# File 'lib/point/base.rb', line 41

def find_single(id, params = {})
  o = JSON.parse(Request.new(member_path(id, params)).make.output)
  if o[class_name.downcase]
    create_object(o[class_name.downcase], params)
  else
    raise Point::Errors::NotFound, "Record not found"
  end
end

.member_path(id, params = {}) ⇒ Object

Return the member path for the passed ID & attributes



63
64
65
# File 'lib/point/base.rb', line 63

def member_path(id, params = {})
  [collection_path, id].join('/')
end

.post(path) ⇒ Object

 Post to the specified object on the collection path



51
52
53
# File 'lib/point/base.rb', line 51

def post(path)
  Request.new(path.to_s, :post).make
end

Instance Method Details

#createObject



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

def create
  request = Request.new(self.class.collection_path(default_params), :post)
  request.data = {self.class.class_name.downcase.to_sym => attributes_to_post}
  if request.make && request.success?
    new_record = JSON.parse(request.output)[self.class.class_name]
    self.id = new_record['id']
    true
  else
    populate_errors(request.output)
    false
  end
end

#destroyObject

Delete this record from the remote service. Returns true or false depending on the success status of the destruction.



98
99
100
# File 'lib/point/base.rb', line 98

def destroy
  Request.new(self.class.member_path(self.id, default_params), :delete).make.success?
end

#new_record?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/point/base.rb', line 102

def new_record?
  self.id.nil?
end

#post(action, data = nil) ⇒ Object

Run a post on the member path. Returns the ouput from the post, false if a conflict or raises a Point::Error. Optionally, pass a second ‘data’ parameter to send data to the post action.



89
90
91
92
93
94
# File 'lib/point/base.rb', line 89

def post(action, data = nil)
  path = self.class.member_path(self.id, default_params) + "/" + action.to_s
  request = Request.new(path, :post)
  request.data = data
  request.make
end

#saveObject



106
107
108
# File 'lib/point/base.rb', line 106

def save
  new_record? ? create : update
end

#updateObject

Push the updated attributes to the remote. Returns true if the record was saved successfully  other false if not. If not saved successfully, the errors hash will be updated with an array of all errors with the submission.



126
127
128
129
130
131
132
133
134
135
# File 'lib/point/base.rb', line 126

def update
  request = Request.new(self.class.member_path(self.id, default_params), :put)
  request.data = {self.class.class_name.downcase.to_sym => attributes_to_post}
  if request.make && request.success?
    true
  else
    populate_errors(request.output)
    false
  end
end