# frozen_string_literal: true require 'codat/base_model' module Codat module Models # Companies for a given company. class Company < BaseModel ENDPOINTS = { collection: '/companies', single: '/companies/:company_id' }.freeze attributes :id, :name, :platform, :redirect, :status, :last_sync, :data_connections attributes :integration_id, :source_id, :platform_name, :link_url # Returns the list of companies in the Codat account. def self.all(params = {}) result = get(ENDPOINTS[:collection], params) return [] unless successful_response?(result) result.body[:results].map { |company| new(json: company) } end def self.find(company_id) url = format_url(ENDPOINTS[:single], company_id: company_id) result = get(url) return nil unless successful_response?(result) new(json: result.body) end def self.create(params = {}) result = post(ENDPOINTS[:collection], params) return { error: 'An error occured.' } unless successful_response?(result) new(json: result.body) end end end end