Class: FluidCLI::CompanySwitcher

Inherits:
Object
  • Object
show all
Defined in:
lib/fluid_cli/company_switcher.rb

Defined Under Namespace

Classes: Error, Timeout

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctx:, company: nil, switched_after_login: false) ⇒ CompanySwitcher

Returns a new instance of CompanySwitcher.



10
11
12
13
14
# File 'lib/fluid_cli/company_switcher.rb', line 10

def initialize(ctx:, company: nil, switched_after_login: false)
  @ctx = ctx
  @identifier = company
  @switched_after_login = 
end

Instance Attribute Details

#ctxObject

Returns the value of attribute ctx.



8
9
10
# File 'lib/fluid_cli/company_switcher.rb', line 8

def ctx
  @ctx
end

#identifierObject

Returns the value of attribute identifier.



8
9
10
# File 'lib/fluid_cli/company_switcher.rb', line 8

def identifier
  @identifier
end

#switched_after_loginObject

Returns the value of attribute switched_after_login.



8
9
10
# File 'lib/fluid_cli/company_switcher.rb', line 8

def 
  @switched_after_login
end

Instance Method Details

#companiesObject



55
56
57
58
59
60
# File 'lib/fluid_cli/company_switcher.rb', line 55

def companies
  @companies ||= begin
    _status, body = FluidCLI::API.new(@ctx).get(path: "/me")
    body["companies"]
  end
end

#find_companyObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fluid_cli/company_switcher.rb', line 21

def find_company
  if 
    if identifier
      company = companies.find { |c| c["subdomain"] == identifier }
      company ||= select_company
    else
      if companies.size == 1
        company = companies.first
      else
        company = select_company
      end
    end
  else
    if identifier
      company = companies.find { |c| c["subdomain"] == identifier }
      unless company
        @ctx.abort("Company with subdomain '#{identifier}' not found.")
      end
    else
      company = select_company
    end
  end
  company
end

#select_companyObject



46
47
48
49
50
51
52
53
# File 'lib/fluid_cli/company_switcher.rb', line 46

def select_company
  company_id = CLI::UI::Prompt.ask("Select a company") do |handler|
                companies.each do |c|
                  handler.option("#{c["name"]} <#{c["subdomain"]}>") { c["id"] }
                end
              end
  companies.find { |c| c["id"] == company_id }
end

#switchObject



16
17
18
19
# File 'lib/fluid_cli/company_switcher.rb', line 16

def switch
  company = find_company
  switch_company(company)
end

#switch_company(company) ⇒ Object



62
63
64
65
66
67
# File 'lib/fluid_cli/company_switcher.rb', line 62

def switch_company(company)
  _status, body  = FluidCLI::API.new(@ctx).put(path: "/authentication/company/#{company['id']}/switch")
  FluidCLI::DB.set(jwt: body.dig("company", "jwt"))
  IdentityAuth.new(ctx: @ctx).reauthenticate
  company
end