Class: Supso::Organization

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

Constant Summary collapse

@@current_organization =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, id) ⇒ Organization

Returns a new instance of Organization.



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

def initialize(name, id)
  @name = name
  @id = id
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.current_organizationObject



49
50
51
# File 'lib/supso/organization.rb', line 49

def self.current_organization
  @@current_organization ||= Organization.current_organization_from_file
end

.current_organization_filenameObject



29
30
31
# File 'lib/supso/organization.rb', line 29

def self.current_organization_filename
  "#{ Supso.project_supso_config_root }/current_organization.json"
end

.current_organization_from_fileObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/supso/organization.rb', line 33

def self.current_organization_from_file
  organization_data = {}
  begin
    organization_data = JSON.parse(File.read(Organization.current_organization_filename))
    organization_data = {} if !organization_data.is_a?(Object)
  rescue
    organization_data = {}
  end

  if organization_data['id']
    Organization.new(organization_data['name'], organization_data['id'])
  else
    nil
  end
end

.current_organization_or_fetchObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/supso/organization.rb', line 53

def self.current_organization_or_fetch
  org = Organization.current_organization

  if !org
    Organization.fetch_current_organization!
    org = Organization.current_organization
    if !org
      raise StandardError.new('Could not find current organization')
    else
      org
    end
  end
end

.delete_current_organization!Object



72
73
74
75
76
77
# File 'lib/supso/organization.rb', line 72

def self.delete_current_organization!
  @@current_organization = nil
  if File.exists?(Organization.current_organization_from_file)
    File.delete(Organization.current_organization_from_file)
  end
end

.fetch_current_organization!Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/supso/organization.rb', line 79

def self.fetch_current_organization!
  user = User.current_user
  data = {
      auth_token: user.auth_token,
      user_id: user.id,
  }
  response = Util.http_post("#{ Supso.supso_api_root }users/me/current_organization", data)

  if response['success']
    org = response['organization']
    Organization.set_current_organization!(org['name'], org['id'])
  else
    puts response['reason']
  end
end

.set_current_organization!(name, id) ⇒ Object



67
68
69
70
# File 'lib/supso/organization.rb', line 67

def self.set_current_organization!(name, id)
  @@current_organization = Organization.new(name, id)
  @@current_organization.save_to_file!
end

Instance Method Details

#save_to_file!Object



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

def save_to_file!
  Util.ensure_path_exists!(Organization.current_organization_filename)
  file = File.open(Organization.current_organization_filename, 'w')
  file << self.saved_data.to_json
  file.close
  Project.save_project_directory_readme!
end

#saved_dataObject



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

def saved_data
  data = {}
  data['name'] = self.name if self.name
  data['id'] = self.id if self.id
  data
end