class Ridley::Resource

Public Class Methods

new(connection_registry) click to toggle source

@param [Celluloid::Registry] connection_registry

# File lib/ridley/resource.rb, line 29
def initialize(connection_registry)
  @connection_registry = connection_registry
end
representation() click to toggle source
# File lib/ridley/resource.rb, line 16
def representation
  return @representation if @representation
  raise RuntimeError.new("no representation set")
end
represented_by(klass) click to toggle source
# File lib/ridley/resource.rb, line 21
def represented_by(klass)
  @representation = klass
end
resource_path() click to toggle source

@return [String]

# File lib/ridley/resource.rb, line 5
def resource_path
  @resource_path ||= representation.chef_type
end
set_resource_path(path) click to toggle source

@param [String] path

@return [String]

# File lib/ridley/resource.rb, line 12
def set_resource_path(path)
  @resource_path = path
end

Public Instance Methods

all() click to toggle source

@param [Ridley::Client] client

@return [Array<Object>]

# File lib/ridley/resource.rb, line 65
def all
  request(:get, self.class.resource_path).collect do |identity, location|
    new(self.class.representation.chef_id.to_s => identity)
  end
end
connection() click to toggle source

@return [Ridley::Connection]

# File lib/ridley/resource.rb, line 58
def connection
  @connection_registry[:connection_pool]
end
create(object) click to toggle source

@param [#to_hash] object

@return [Object]

# File lib/ridley/resource.rb, line 85
def create(object)
  resource = new(object.to_hash)
  new_attributes = request(:post, self.class.resource_path, resource.to_json)
  resource.mass_assign(resource._attributes_.deep_merge(new_attributes))
  resource
end
delete(object) click to toggle source

@param [String, chef_id] object

@return [Object, nil]

# File lib/ridley/resource.rb, line 95
def delete(object)
  chef_id = object.respond_to?(:chef_id) ? object.chef_id : object
  new(request(:delete, "#{self.class.resource_path}/#{chef_id}"))
rescue AbortError => ex
  return nil if ex.cause.is_a?(Errors::HTTPNotFound)
  abort(ex.cause)
end
delete_all() click to toggle source

@return [Array<Object>]

# File lib/ridley/resource.rb, line 104
def delete_all
  all.collect { |resource| future(:delete, resource) }.map(&:value)
end
find(object) click to toggle source

@param [String, chef_id] object

@return [Object, nil]

# File lib/ridley/resource.rb, line 74
def find(object)
  chef_id = object.respond_to?(:chef_id) ? object.chef_id : object
  new(request(:get, "#{self.class.resource_path}/#{chef_id}"))
rescue AbortError => ex
  return nil if ex.cause.is_a?(Errors::HTTPNotFound)
  abort(ex.cause)
end
from_file(filename) click to toggle source

Used to build a representation from a file with the current Actor's resource

@param [String] filename

a full filename from which to build this representation (currently only supports .json files)

@return [representation.class]

# File lib/ridley/resource.rb, line 43
def from_file(filename)
  from_json(File.read(filename))
end
from_json(json) click to toggle source

Used to build a representation from a serialized json string with the current Actor's resource

@param [String] json

a representation serialized into json

@return [representation.class]

# File lib/ridley/resource.rb, line 53
def from_json(json)
  new(JSON.parse(json))
end
new(*args) click to toggle source
# File lib/ridley/resource.rb, line 33
def new(*args)
  self.class.representation.new(Actor.current, *args)
end
update(object) click to toggle source

@param [#to_hash] object

@return [Object, nil]

# File lib/ridley/resource.rb, line 111
def update(object)
  resource = new(object.to_hash)
  new(request(:put, "#{self.class.resource_path}/#{resource.chef_id}", resource.to_json))
rescue AbortError => ex
  return nil if ex.cause.is_a?(Errors::HTTPConflict)
  abort(ex.cause)
end

Private Instance Methods

raw_request(method, *args) click to toggle source

@param [Symbol] method

# File lib/ridley/resource.rb, line 127
def raw_request(method, *args)
  unless Connection::METHODS.include?(method)
    raise Errors::HTTPUnknownMethod, "unknown http method: #{method}"
  end

  connection.send(method, *args)
rescue Errors::HTTPError, Errors::ClientError => ex
  abort(ex)
end
request(method, *args) click to toggle source

@param [Symbol] method

# File lib/ridley/resource.rb, line 122
def request(method, *args)
  raw_request(method, *args).body
end