require 'faastruby/api' module FaaStRuby class Workspace < BaseObject ##### Class methods def self.create(name:, email: nil, provider: nil) api = API.new workspace = Workspace.new(name: name, email: email, errors: [], provider: provider) response = api.create_workspace(workspace_name: name, email: email, provider: provider) workspace.status_code = response.code if response.errors.any? workspace.errors += response.errors return workspace end case response.code when 422 workspace.errors += ['(422) Unprocessable Entity', response.body] when 200, 201 workspace.credentials = response.body['credentials'] workspace.runners_max = response.body['runners_max'].to_i if response.body['runners_max'] else workspace.errors << "(#{response.code}) Error" end return workspace end ################### ##### Instance methods attr_accessor :name, :errors, :functions, :email, :object, :credentials, :updated_at, :created_at, :status_code, :provider, :runners_max, :runners_current, :static_metadata def make_request response = yield @status_code = response.code @errors += response.errors if response.errors.any? response end def destroy make_request { @api.destroy_workspace(@name) } end def deploy(package_file_name, root_to: false, catch_all: false, context: false) make_request { @api.deploy(workspace_name: @name, package: package_file_name, root_to: root_to, catch_all: catch_all, context: context) } self end def refresh_credentials response = make_request { @api.refresh_credentials(@name) } @credentials = response.body[@name] unless response.body.nil? self end def update_runners(value) response = make_request { @api.update_runners(workspace_name: @name, runners_max: value) } @runners_max = response.body['runners_max'].to_i if response.body['runners_max'] rescue nil self end def fetch response = make_request { @api.get_workspace_info(@name) } parse_attributes(response.body) unless response.errors.any? self end def upload_file(package_file_name, relative_path:) make_request { @api.upload_file(workspace_name: @name, package: package_file_name, relative_path: relative_path) } self end def delete_file(relative_path:) make_request { @api.delete_file(workspace_name: @name, relative_path: relative_path) } self end def static_metadata response = make_request { @api.get_static_metadata(@name) } @static_metadata = response.body['metadata'] self end def parse_attributes(attributes) @functions = attributes['functions'] @email = attributes['email'] @object = attributes['object'] @updated_at = attributes['updated_at'] @created_at = attributes['created_at'] @provider = attributes['provider'] @runners_max = attributes['runners_max'].to_i if attributes['runners_max'] rescue nil @runners_current = attributes['runners_current'].to_i rescue nil end end end