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

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
25
26
# File 'lib/codat/models/company.rb', line 18

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

  return [] if result.status == 404

  return result.body if result.status == 400

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

.create(params = {}) ⇒ Object



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

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

  return { error: 'An error occured.' } if result.status == 404 || result.status == 400

  new(json: result.body)
end

.find(company_id) ⇒ Object



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

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

  result = get(url)

  return nil if result.status == 404

  new(json: result.body)
end