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, successful_response?

Constructor Details

#initialize(json: {}) ⇒ FinancialSheet

Returns a new instance of FinancialSheet.



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

def initialize(json: {})
  super

  @most_recent_available_month = Date.parse(json[:mostRecentAvailableMonth])
  @earliest_available_month = Date.parse(json[: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



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

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.transform_keys { |key| Camelizer.transform(key) }

  query.merge(options)
end

.endpoint(endpoint = nil) ⇒ Object



65
66
67
68
69
# File 'lib/codat/models/financial_sheet.rb', line 65

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)


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

def self.find(params = {})
  company_id = params[: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 unless successful_response?(result)

  new(json: result.body)
end

.inherited(other) ⇒ Object



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

def self.inherited(other)
  super

  other.attributes :currency, :most_recent_available_month, :earliest_available_month
end

.report_class(report_class = nil) ⇒ Object



59
60
61
62
63
# File 'lib/codat/models/financial_sheet.rb', line 59

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

  @report_class = report_class
end