class ITGlue::Asset::Base

Attributes

attributes[RW]
id[RW]
type[RW]

Public Class Methods

asset_type() click to toggle source

Override in subclasses if required

# File lib/itglue/asset/base.rb, line 12
def asset_type
  raise ITGlueAssetError.new('no asset_type for base') if self == Base
  self.name.demodulize.pluralize.underscore.to_sym
end
client() click to toggle source
# File lib/itglue/asset/base.rb, line 68
def client
  @@client ||= Client.new
end
filter(filter) click to toggle source

Executes a get request through the top-level path, with a filter query E.g. GET '/configurations?filter=HP-01' @param filter [Hash|String] the parameters to filter by @return [Array<ITGlue::Asset>] an array of asset instances

# File lib/itglue/asset/base.rb, line 53
def filter(filter)
  raise_method_not_available(__method__, 'is nested asset') if nested_asset?
  assets = client.get(asset_type, {}, { query: { filter: filter } })
  assets.map { |data| self.new_from_payload(data) }
end
find(id) click to toggle source

Executes a get request through the top-level path for a specific asset E.g. GET '/configurations/1' @param id [Integer] the id of the asset @return [ITGlue::Asset] the asset instance

# File lib/itglue/asset/base.rb, line 63
def find(id)
  data = client.get(asset_type, id: id )
  self.new_from_payload(data)
end
get() click to toggle source

Executes a get request through the top-level path E.g. GET '/configurations' @return [Array<ITGlue::Asset>] an array of asset instances

# File lib/itglue/asset/base.rb, line 32
def get
  raise_method_not_available(__method__, 'is nested asset') if nested_asset?
  assets = client.get(asset_type)
  assets.map { |data| self.new_from_payload(data) }
end
get_nested(parent) click to toggle source

Executes a get request through the nested asset path E.g. GET 'organizations/:organization_id/relationships/configurations' @param parent [ITGlue::Asset] the parent asset @return [Array<ITGlue::Asset>] an array of asset instances

# File lib/itglue/asset/base.rb, line 42
def get_nested(parent)
  raise_method_not_available(__method__, 'is top-level asset') unless parent_type
  path_options = { parent: parent }
  assets = client.get(asset_type, path_options)
  assets.map { |data| self.new_from_payload(data) }
end
new(attributes = {}) click to toggle source
# File lib/itglue/asset/base.rb, line 82
def initialize(attributes = {})
  raise ITGlueAssetError.new('cannot instantiate base') if self == Base
  @attributes = Attributes.new(attributes)
end
new_from_payload(data) click to toggle source

Instantiates a record from data payload @param data [Hash] the data payload

E.g.: { id: 1, type: 'organizations', attributes: {name: 'Happy Frog', ...} }

@return [ITGlue::Asset] the record instance

# File lib/itglue/asset/base.rb, line 21
def new_from_payload(data)
  raise_method_not_available(__method__, 'not available for Base') if self == Base
  asset = self.new(data[:attributes])
  asset.id = data[:id]
  asset.type = data[:type]
  asset
end

Protected Class Methods

raise_method_not_available(method_name, reason) click to toggle source
# File lib/itglue/asset/base.rb, line 74
def raise_method_not_available(method_name, reason)
  error_msg = "method '#{method_name}' is not available for #{asset_type}: #{reason}"
  raise MethodNotAvailable.new(error_msg)
end

Public Instance Methods

[](attribute) click to toggle source
# File lib/itglue/asset/base.rb, line 130
def [](attribute)
  @attributes[attribute]
end
[]=(attribute, value) click to toggle source
# File lib/itglue/asset/base.rb, line 134
def []=(attribute, value)
  @attributes.assign_attribute(attribute, value)
end
asset_type() click to toggle source
# File lib/itglue/asset/base.rb, line 87
def asset_type
  self.class.asset_type
end
assign_attributes(attributes) click to toggle source
# File lib/itglue/asset/base.rb, line 103
def assign_attributes(attributes)
  raise ArgumentError.new('attributtes must be a Hash') unless attributes.is_a?(Hash)
  attributes.each do |attribute, value|
    @attributes[attribute] = value
  end
end
changed?() click to toggle source
# File lib/itglue/asset/base.rb, line 126
def changed?
  !changed_attributes.empty?
end
changed_attributes() click to toggle source
# File lib/itglue/asset/base.rb, line 110
def changed_attributes
  @attributes.changes
end
dup() click to toggle source
# File lib/itglue/asset/base.rb, line 97
def dup
  dup = self.class.new(self.attributes)
  dup.type = self.type
  dup
end
inspect() click to toggle source
# File lib/itglue/asset/base.rb, line 91
def inspect
  string = "#<#{self.class.name} id: #{self.id || 'nil'} "
  fields = @attributes.keys.map { |field| "#{field}: #{@attributes.inspect_field(field)}" }
  string << fields.join(", ") << ">"
end
method_missing(method, *args) click to toggle source
Calls superclass method
# File lib/itglue/asset/base.rb, line 138
def method_missing(method, *args)
  method_name = method.to_s
  arg_count = args.length
  if method_name.chomp!('=')
    raise ArgumentError.new("wrong number of arguments (#{arg_count} for 1)") if arg_count != 1
    @attributes.assign_attribute(method_name, args[0])
  elsif arg_count == 0
    @attributes[method]
  else
    super
  end
end
new_asset?() click to toggle source
# File lib/itglue/asset/base.rb, line 118
def new_asset?
  !self.id
end
remove_attribute(key) click to toggle source
# File lib/itglue/asset/base.rb, line 114
def remove_attribute(key)
  @attributes.remove_attribute(key)
end
save() click to toggle source
# File lib/itglue/asset/base.rb, line 122
def save
  new_asset? ? create : update
end

Private Instance Methods

create() click to toggle source
# File lib/itglue/asset/base.rb, line 153
def create
  data = self.class.client.post(asset_type, payload)
  reload_from_data(data)
end
payload() click to toggle source
# File lib/itglue/asset/base.rb, line 163
def payload
  {
    data: {
      type: asset_type,
      attributes: attributes.attributes_hash
    }
  }
end
reload_from_data(data) click to toggle source
# File lib/itglue/asset/base.rb, line 172
def reload_from_data(data)
  @attributes = Attributes.new(data[:attributes])
  self.type = data[:type]
  self.id = data[:id]
  self
end
update() click to toggle source
# File lib/itglue/asset/base.rb, line 158
def update
  data = self.class.client.patch(asset_type, payload, id: id )
  reload_from_data(data)
end