Class: Billomat::Models::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/billomat/models/base.rb

Overview

This class is the base for all other models (resources). It handles the communication with the gateway to talk to the API.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Billomat::Models::Base

Initializes a new model.

Parameters:

  • data (Hash) (defaults to: {})

    the attributes of the object



33
34
35
# File 'lib/billomat/models/base.rb', line 33

def initialize(data = {})
  @data = OpenStruct.new(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

All values in the @data hash can be accessed like a ‘normal’ method.

Examples:

invoice = Billomat::Models::Invoice.new(invoice_number: '123')
invoice.invoice_number
#=> '123'


120
121
122
123
124
# File 'lib/billomat/models/base.rb', line 120

def method_missing(method, *args, &)
  return @data[method] if @data.to_h.key?(method)

  super
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/billomat/models/base.rb', line 8

def data
  @data
end

Class Method Details

.find(id) ⇒ Billomat::Models::Base?

Tries to find the resource for the given id.

Parameters:

  • id (String)

    the resource id

Returns:



14
15
16
17
18
19
# File 'lib/billomat/models/base.rb', line 14

def self.find(id)
  return nil if id.nil?

  resp = Billomat::Gateway.new(:get, "#{base_path}/#{id}").run
  new(resp[resource_name])
end

.where(hash = {}) ⇒ Array<Billomat::Models::Base>

Allows to query for a record.

Parameters:

  • hash (Hash) (defaults to: {})

    the query parameters

Returns:



25
26
27
# File 'lib/billomat/models/base.rb', line 25

def self.where(hash = {})
  Billomat::Search.new(self, hash).run
end

Instance Method Details

#as_json(_options = nil) ⇒ Hash

Returns the object with the right JSON structure.

Returns:

  • (Hash)

    the objects data



110
111
112
# File 'lib/billomat/models/base.rb', line 110

def as_json(_options = nil)
  @data.to_h
end

#createTrueClass Also known as: create!

rubocop:disable Naming/PredicateMethod – because this method performs

an action, not a predicate check (bool is for error signaling)

Returns:

  • (TrueClass)


52
53
54
55
56
57
58
59
60
# File 'lib/billomat/models/base.rb', line 52

def create
  resp = Billomat::Gateway.new(
    :post, self.class.base_path, wrapped_data
  ).run

  @data = OpenStruct.new(resp[self.class.resource_name])

  true
end

#deleteTrueClass Also known as: delete!

rubocop:disable Naming/PredicateMethod – because this method performs

an action, not a predicate check (bool is for error signaling)

Returns:

  • (TrueClass)


82
83
84
85
86
87
# File 'lib/billomat/models/base.rb', line 82

def delete
  path = "#{self.class.base_path}/#{id}"
  Billomat::Gateway.new(:delete, path).run

  true
end

#idString?

Returns the object’s ID.

Returns:

  • (String, nil)

    the object’s ID



92
93
94
# File 'lib/billomat/models/base.rb', line 92

def id
  @data['id'] || nil
end

#respond_to_missing?(method, include_privat = false) ⇒ TrueClass, FalseClass

Necessary for method_missing.

Parameters:

  • method (Symbol)

    The method name

  • include_privat (TrueClass, FalseClass) (defaults to: false)

Returns:

  • (TrueClass, FalseClass)


131
132
133
# File 'lib/billomat/models/base.rb', line 131

def respond_to_missing?(method, include_privat = false)
  @data.to_h.key?(method.to_s) || super
end

#saveTrueClass Also known as: save!

Persists the current object in the API. When record is new it calls create, otherwise it saves the object.

Returns:

  • (TrueClass)


41
42
43
44
45
# File 'lib/billomat/models/base.rb', line 41

def save
  return create if id.nil?

  update
end

#updateTrueClass Also known as: update!

rubocop:disable Naming/PredicateMethod – because this method performs

an action, not a predicate check (bool is for error signaling)

Returns:

  • (TrueClass)


68
69
70
71
72
73
74
# File 'lib/billomat/models/base.rb', line 68

def update
  path = "#{self.class.base_path}/#{id}"
  resp = Billomat::Gateway.new(:put, path, wrapped_data).run
  @data = resp[self.class.resource_name]

  true
end

#wrapped_dataHash

Wraps the data so the API accepts the request.

Examples:

some_invoice.wrapped_data
#=> { "invoice" => { "id" => "12345"  } }

Returns:

  • (Hash)

    the wrapped data



103
104
105
# File 'lib/billomat/models/base.rb', line 103

def wrapped_data
  { self.class.resource_name => @data.to_h }
end