Module: Moneybird::Resource

Included in:
Administration, Contact, CustomField, DocumentStyle, Documents::GeneralDocument, Documents::GeneralJournalDocument, Documents::PurchaseInvoice, Documents::Receipt, Documents::TypelessDocument, Estimate, ExternalSalesInvoice, FinancialAccount, FinancialMutation, Generic::Event, Generic::Note, Identity, Invoice::Details, Invoice::Payment, LedgerAccount, Product, RecurringSalesInvoice, SalesInvoice, Synchronization, TaxRate, Webhook, Workflow, Webhook
Defined in:
lib/moneybird/resource.rb,
lib/moneybird/resource/contact.rb,
lib/moneybird/resource/product.rb,
lib/moneybird/resource/webhook.rb,
lib/moneybird/resource/estimate.rb,
lib/moneybird/resource/identity.rb,
lib/moneybird/resource/tax_rate.rb,
lib/moneybird/resource/workflow.rb,
lib/moneybird/resource/custom_field.rb,
lib/moneybird/resource/sales_invoice.rb,
lib/moneybird/resource/administration.rb,
lib/moneybird/resource/document_style.rb,
lib/moneybird/resource/ledger_account.rb,
lib/moneybird/resource/synchronization.rb,
lib/moneybird/resource/financial_account.rb,
lib/moneybird/resource/financial_mutation.rb,
lib/moneybird/resource/external_sales_invoice.rb,
lib/moneybird/resource/recurring_sales_invoice.rb

Defined Under Namespace

Modules: ClassMethods, Documents, Generic, Invoice Classes: Administration, Contact, CustomField, DocumentStyle, Estimate, ExternalSalesInvoice, FinancialAccount, FinancialMutation, Identity, LedgerAccount, Product, RecurringSalesInvoice, SalesInvoice, Synchronization, TaxRate, Webhook, Workflow

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject

Returns the value of attribute client.



3
4
5
# File 'lib/moneybird/resource.rb', line 3

def client
  @client
end

#errorsObject

Returns the value of attribute errors.



3
4
5
# File 'lib/moneybird/resource.rb', line 3

def errors
  @errors
end

Instance Method Details

#attributesObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/moneybird/resource.rb', line 29

def attributes
  self.class.attributes.inject({}) do |attributes, attribute|
    value = send(attribute)
    if !value.nil?
      if value.is_a?(Array) && value.first.respond_to?(:attributes)
        attributes["#{attribute}_attributes"] = value.map { |item| item.attributes }
      elsif value.respond_to?(:attributes)
        attributes["#{attribute}_attributes"] = value.attributes
      else
        unless self.class.nillable_attributes && self.class.nillable_attributes.include?(attribute)
          attributes[attribute] = value
        end
      end
    end; attributes
  end
end

#attributes=(attributes) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/moneybird/resource.rb', line 17

def attributes=(attributes)
  @attributes = attributes
  self.client ||= attributes[:client]
  attributes.each do |attribute, value|
    if respond_to?(writer = attribute.to_s + '=')
      send(writer, value)
    else
      self.class.logger.warn "#{self.class} does not have an `#{attribute}' attribute"
    end
  end
end

#custom_fields=(custom_fields) ⇒ Object



26
27
28
# File 'lib/moneybird/resource/identity.rb', line 26

def custom_fields=(custom_fields)
  @custom_fields = custom_fields.map { |custom_field| Moneybird::Resource::CustomField.build(custom_field) }
end

#initialize(attributes) ⇒ Object



5
6
7
# File 'lib/moneybird/resource.rb', line 5

def initialize(attributes)
  self.attributes = attributes
end


33
34
35
36
# File 'lib/moneybird/resource/financial_mutation.rb', line 33

def link_booking(booking)
  financial_mutation_service = Moneybird::Service::FinancialMutation.new(client, administration_id)
  financial_mutation_service.link_booking(self, booking)
end

#pathObject



13
14
15
# File 'lib/moneybird/resource.rb', line 13

def path
  persisted? ? "/#{id}" : ""
end

#persisted?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/moneybird/resource.rb', line 9

def persisted?
  !!id
end

#to_jsonObject



46
47
48
# File 'lib/moneybird/resource.rb', line 46

def to_json
  JSON.generate({self.class.resource => attributes.as_json})
end