class Deploy::Resource

Attributes

attributes[RW]

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

errors[RW]

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

id[RW]

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

Public Class Methods

class_name() click to toggle source

Return the deploy class name

# File lib/deploy/resource.rb, line 71
def class_name
  self.name.to_s.split('::').last.downcase
end
collection_path(params = {}) click to toggle source

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

# File lib/deploy/resource.rb, line 61
def collection_path(params = {})
  class_name.downcase + 's'
end
find(type, params = {}) click to toggle source

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.

# File lib/deploy/resource.rb, line 24
def find(type, params = {})
  case type
  when :all then find_all(params)
  else find_single(type, params)
  end
end
find_all(params) click to toggle source

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

# File lib/deploy/resource.rb, line 32
def find_all(params)
  output = JSON.parse(Request.new(collection_path(params)).make.output)
  if output.is_a?(Hash) && output['records'] && output['pagination']
    output = output['records']
  end
  return [] unless output.is_a?(Array)
  output.map do |o|
    create_object(o, params)
  end
end
find_single(id, params = {}) click to toggle source

Find a single object and return an object for it.

# File lib/deploy/resource.rb, line 44
def find_single(id, params = {})
  o = JSON.parse(Request.new(member_path(id, params)).make.output)
  if o.is_a?(Hash)
    create_object(o, params)
  else
    raise Deploy::Errors::NotFound, "Record not found"
  end
end
member_path(id, params = {}) click to toggle source

Return the member path for the passed ID & attributes

# File lib/deploy/resource.rb, line 66
def member_path(id, params = {})
  [collection_path, id].join('/')
end
post(path) click to toggle source

Post to the specified object on the collection path

# File lib/deploy/resource.rb, line 54
def post(path)
  Request.new(path.to_s, :post).make
end

Private Class Methods

create_object(attributes, objects = []) click to toggle source

Create a new object with the specified attributes and getting and ID. Returns the newly created object

# File lib/deploy/resource.rb, line 79
def create_object(attributes, objects = [])
  o = self.new
  o.attributes = attributes
  o.id         = attributes['id']
  for key, object in objects.select{|k,v| v.kind_of?(Deploy::Resource)}
    o.attributes[key.to_s] = object
  end
  o
end

Public Instance Methods

create() click to toggle source
# File lib/deploy/resource.rb, line 113
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.attributes = new_record
    self.identifier = new_record['identifier']
    true
  else
    populate_errors(request.output)
    false
  end
end
destroy() click to toggle source

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

# File lib/deploy/resource.rb, line 101
def destroy
  Request.new(self.class.member_path(self.id, default_params), :delete).make.success?
end
method_missing(method, *params) click to toggle source

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

# File lib/deploy/resource.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
new_record?() click to toggle source
# File lib/deploy/resource.rb, line 105
def new_record?
  self.id.nil?
end
post(action, data = nil) click to toggle source

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

# File lib/deploy/resource.rb, line 92
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
save() click to toggle source
# File lib/deploy/resource.rb, line 109
def save
  new_record? ? create : update
end
update() click to toggle source

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.

# File lib/deploy/resource.rb, line 130
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

Private Instance Methods

attributes_to_post() click to toggle source

Attributes which can be passed for update & creation

# File lib/deploy/resource.rb, line 158
def attributes_to_post
  self.attributes.inject(Hash.new) do |r,(key,value)|
    r[key] = value if value.is_a?(String) || value.is_a?(Integer)
    r
  end
end
default_params() click to toggle source

An array of params which should always be sent with this instances requests

# File lib/deploy/resource.rb, line 153
def default_params
  Hash.new
end
populate_errors(json) click to toggle source

Populate the errors hash from the given raw JSON output

# File lib/deploy/resource.rb, line 144
def populate_errors(json)
  self.errors = Hash.new
  JSON.parse(json).inject(self.errors) do |r, e|
    r[e.first] = e.last
    r
  end
end