Class: ITGlue::Asset::Base

Inherits:
Object
  • Object
show all
Extended by:
Relatable
Defined in:
lib/itglue/asset/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Relatable

children, parent

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.

Raises:



82
83
84
85
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# 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

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



80
81
82
# File 'lib/itglue/asset/base.rb', line 80

def attributes
  @attributes
end

#idObject

Returns the value of attribute id.



80
81
82
# File 'lib/itglue/asset/base.rb', line 80

def id
  @id
end

#typeObject

Returns the value of attribute type.



80
81
82
# File 'lib/itglue/asset/base.rb', line 80

def type
  @type
end

Class Method Details

.asset_typeObject

Override in subclasses if required

Raises:



12
13
14
15
# 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

.clientObject



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

def client
  @@client ||= Client.new
end

.filter(filter) ⇒ Array<ITGlue::Asset>

Executes a get request through the top-level path, with a filter query E.g. GET ‘/configurations?filter=HP-01’

Parameters:

  • filter (Hash|String)

    the parameters to filter by

Returns:



53
54
55
56
57
# 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) ⇒ ITGlue::Asset

Executes a get request through the top-level path for a specific asset E.g. GET ‘/configurations/1’

Parameters:

  • id (Integer)

    the id of the asset

Returns:



63
64
65
66
# File 'lib/itglue/asset/base.rb', line 63

def find(id)
  data = client.get(asset_type, id: id )
  self.new_from_payload(data)
end

.getArray<ITGlue::Asset>

Executes a get request through the top-level path E.g. GET ‘/configurations’

Returns:



32
33
34
35
36
# 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) ⇒ Array<ITGlue::Asset>

Executes a get request through the nested asset path E.g. GET ‘organizations/:organization_id/relationships/configurations’

Parameters:

Returns:



42
43
44
45
46
47
# 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_from_payload(data) ⇒ ITGlue::Asset

Instantiates a record from data payload

Parameters:

  • data (Hash)

    the data payload E.g.: { id: 1, type: ‘organizations’, attributes: ‘Happy Frog’, … }

Returns:



21
22
23
24
25
26
27
# 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

Instance Method Details

#[](attribute) ⇒ Object



130
131
132
# File 'lib/itglue/asset/base.rb', line 130

def [](attribute)
  @attributes[attribute]
end

#[]=(attribute, value) ⇒ Object



134
135
136
# File 'lib/itglue/asset/base.rb', line 134

def []=(attribute, value)
  @attributes.assign_attribute(attribute, value)
end

#asset_typeObject



87
88
89
# File 'lib/itglue/asset/base.rb', line 87

def asset_type
  self.class.asset_type
end

#assign_attributes(attributes) ⇒ Object

Raises:

  • (ArgumentError)


103
104
105
106
107
108
# 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?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/itglue/asset/base.rb', line 126

def changed?
  !changed_attributes.empty?
end

#changed_attributesObject



110
111
112
# File 'lib/itglue/asset/base.rb', line 110

def changed_attributes
  @attributes.changes
end

#dupObject



97
98
99
100
101
# File 'lib/itglue/asset/base.rb', line 97

def dup
  dup = self.class.new(self.attributes)
  dup.type = self.type
  dup
end

#inspectObject



91
92
93
94
95
# 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

#new_asset?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/itglue/asset/base.rb', line 118

def new_asset?
  !self.id
end

#remove_attribute(key) ⇒ Object



114
115
116
# File 'lib/itglue/asset/base.rb', line 114

def remove_attribute(key)
  @attributes.remove_attribute(key)
end

#saveObject



122
123
124
# File 'lib/itglue/asset/base.rb', line 122

def save
  new_asset? ? create : update
end