Class: Codat::Models::FinancialSheet

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/codat/models/financial_sheet.rb

Direct Known Subclasses

BalanceSheet, ProfitAndLoss

Constant Summary collapse

DEFAULT_PERIOD_LENGTH =

Default period length (months)

12
DEFAULT_PERIODS_TO_COMPARE =

Default number of periods to compare

2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

attributes, #format_url, format_url, get, #get, post, #post

Constructor Details

#initialize(json: {}) ⇒ FinancialSheet

Returns a new instance of FinancialSheet.



69
70
71
72
73
74
75
76
77
# File 'lib/codat/models/financial_sheet.rb', line 69

def initialize(json: {})
  super

  @most_recent_available_month = Date.parse(json.dig(:mostRecentAvailableMonth))
  @earliest_available_month = Date.parse(json.dig(:earliestAvailableMonth))

  reports_json = json.fetch(:reports, {})
  @reports = reports_json.map { |report| self.class.report_class.new(json: report) }
end

Instance Attribute Details

#reportsObject (readonly)

Returns the value of attribute reports.



14
15
16
# File 'lib/codat/models/financial_sheet.rb', line 14

def reports
  @reports
end

Class Method Details

.build_query(params) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/codat/models/financial_sheet.rb', line 44

def self.build_query(params)
  options = params.dup

  query = {
    periodLength: options.fetch(:period_length, DEFAULT_PERIOD_LENGTH),
    periodsToCompare: options.fetch(:periods_to_compare, DEFAULT_PERIODS_TO_COMPARE)
  }

  options = options.map { |key, value| [Camelizer.transform(key), value] }.to_h

  query.merge(options)
end

.endpoint(endpoint = nil) ⇒ Object



63
64
65
66
67
# File 'lib/codat/models/financial_sheet.rb', line 63

def self.endpoint(endpoint = nil)
  return @endpoint unless endpoint

  @endpoint = endpoint
end

.find(params = {}) ⇒ Object

Finds the latest financial sheet for a company. This can be a balance sheet or a profit and loss document.

Params are listed below:

* company_id - the company ID from Codat
* period_length - the number of months to get
* periods_to_compare - how many periods (of period_length size) you want
* start_month (optional) - starting in which month (date)

An ArgumentError is raised unless you provide a :company_id key in the params.

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/codat/models/financial_sheet.rb', line 30

def self.find(params = {})
  company_id = params.dig(:company_id)

  raise ArgumentError, 'please provide company_id' unless company_id

  url = format_url(@endpoint, company_id: company_id.to_s.strip)

  result = get(url, build_query(params))

  return nil if result.status == 404 || result.status == 400

  new(json: result.body)
end

.inherited(other) ⇒ Object



16
17
18
# File 'lib/codat/models/financial_sheet.rb', line 16

def self.inherited(other)
  other.attributes :currency, :most_recent_available_month, :earliest_available_month
end

.report_class(report_class = nil) ⇒ Object



57
58
59
60
61
# File 'lib/codat/models/financial_sheet.rb', line 57

def self.report_class(report_class = nil)
  return @report_class unless report_class

  @report_class = report_class
end