Class: Codat::Models::Company

Inherits:
BaseModel show all
Defined in:
lib/codat/models/company.rb

Overview

Companies for a given company.

Constant Summary collapse

ENDPOINTS =
{
  collection: '/companies',
  single: '/companies/:company_id'
}.freeze

Class Method Summary collapse

Methods inherited from BaseModel

attributes, #format_url, format_url, get, #get, #initialize, post, #post, successful_response?

Constructor Details

This class inherits a constructor from Codat::BaseModel

Class Method Details

.all(params = {}) ⇒ Object

Returns the list of companies in the Codat account.



18
19
20
21
22
23
24
# File 'lib/codat/models/company.rb', line 18

def self.all(params = {})
  result = get(ENDPOINTS[:collection], params)

  return [] unless successful_response?(result)

  result.body[:results].map { |company| new(json: company) }
end

.create(params = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/codat/models/company.rb', line 36

def self.create(params = {})
  result = post(ENDPOINTS[:collection], params)

  return { error: 'An error occured.' } unless successful_response?(result)

  new(json: result.body)
end

.find(company_id) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/codat/models/company.rb', line 26

def self.find(company_id)
  url = format_url(ENDPOINTS[:single], company_id:)

  result = get(url)

  return nil unless successful_response?(result)

  new(json: result.body)
end