Class: FluidCLI::CompanySwitcher
- Inherits:
-
Object
- Object
- FluidCLI::CompanySwitcher
- Defined in:
- lib/fluid_cli/company_switcher.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#ctx ⇒ Object
Returns the value of attribute ctx.
-
#identifier ⇒ Object
Returns the value of attribute identifier.
-
#switched_after_login ⇒ Object
Returns the value of attribute switched_after_login.
Instance Method Summary collapse
- #companies ⇒ Object
- #find_company ⇒ Object
-
#initialize(ctx:, company: nil, switched_after_login: false) ⇒ CompanySwitcher
constructor
A new instance of CompanySwitcher.
- #select_company ⇒ Object
- #switch ⇒ Object
- #switch_company(company) ⇒ Object
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 = switched_after_login end |
Instance Attribute Details
#ctx ⇒ Object
Returns the value of attribute ctx.
8 9 10 |
# File 'lib/fluid_cli/company_switcher.rb', line 8 def ctx @ctx end |
#identifier ⇒ Object
Returns the value of attribute identifier.
8 9 10 |
# File 'lib/fluid_cli/company_switcher.rb', line 8 def identifier @identifier end |
#switched_after_login ⇒ Object
Returns the value of attribute switched_after_login.
8 9 10 |
# File 'lib/fluid_cli/company_switcher.rb', line 8 def switched_after_login @switched_after_login end |
Instance Method Details
#companies ⇒ Object
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_company ⇒ Object
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 switched_after_login 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_company ⇒ Object
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 |
#switch ⇒ Object
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 |