Class: Dashbeautiful::Organization

Inherits:
Object
  • Object
show all
Defined in:
lib/dashbeautiful/organization.rb

Overview

description TODO

Constant Summary collapse

ATTRIBUTES =
%i[id name url].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, **attributes) ⇒ Organization

Returns a new instance of Organization.



30
31
32
33
34
35
# File 'lib/dashbeautiful/organization.rb', line 30

def initialize(api, **attributes)
  @api = api
  @id = attributes[:id]
  @name = attributes[:name]
  @url = attributes[:url]
end

Instance Attribute Details

#apiObject

Returns the value of attribute api.



4
5
6
# File 'lib/dashbeautiful/organization.rb', line 4

def api
  @api
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/dashbeautiful/organization.rb', line 5

def id
  @id
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/dashbeautiful/organization.rb', line 5

def name
  @name
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/dashbeautiful/organization.rb', line 5

def url
  @url
end

Class Method Details

.all(api_key, api: API.new(api_key)) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
# File 'lib/dashbeautiful/organization.rb', line 9

def self.all(api_key, api: API.new(api_key))
  raise ArgumentError, 'api_key is nil. Either initialize Organization or pass a key' if api_key.nil?

  api.organizations.map { |org| Organization.new(api, **org) }
end

.find_by(attribute, value, api_key, api: API.new(api_key)) ⇒ Object



15
16
17
18
19
20
# File 'lib/dashbeautiful/organization.rb', line 15

def self.find_by(attribute, value, api_key, api: API.new(api_key))
  all(api_key, api: api).each do |org|
    return org if org.send(attribute) == value
  end
  nil
end

.init(organization:, api_key:, api: API.new(api_key)) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
# File 'lib/dashbeautiful/organization.rb', line 22

def self.init(organization:, api_key:, api: API.new(api_key))
  ATTRIBUTES.each do |attribute|
    org = find_by(attribute, organization, api_key, api: api)
    return org unless org.nil?
  end
  raise ArgumentError, "Could not find organization: #{organization}"
end

Instance Method Details

#api_keyObject



42
43
44
# File 'lib/dashbeautiful/organization.rb', line 42

def api_key
  api.key
end

#networksObject



46
47
48
# File 'lib/dashbeautiful/organization.rb', line 46

def networks
  @networks ||= api.networks(id).map { |network| Network.create(self, **network) }
end

#networks!Object



50
51
52
53
# File 'lib/dashbeautiful/organization.rb', line 50

def networks!
  @networks = nil
  networks
end