class Billomat::Models::Base

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

Attributes

data[RW]

Public Class Methods

find(id) click to toggle source

Tries to find the resource for the given id

@param [String] id The resource id @return [Billomat::Models::Base, nil] The found resource or nil

# File lib/billomat/models/base.rb, line 18
def self.find(id)
  return nil if id.nil?
  resp = Billomat::Gateway.new(:get, "#{base_path}/#{id}").run
  new(resp[resource_name])
end
new(data = {}) click to toggle source

Initializes a new model

@param [Hash] data The attributes of the object @return [Billomat::Models::Base] The record as an object

# File lib/billomat/models/base.rb, line 38
def initialize(data = {})
  @data = OpenStruct.new(data)
end
where(hash = {}) click to toggle source

Allows to query for a record

@param [Hash] hash The query parameters @return [Array<Billomat::Models::Base>] The found records

# File lib/billomat/models/base.rb, line 29
def self.where(hash = {})
  Billomat::Search.new(self, hash).run
end

Public Instance Methods

as_json(_options = nil) click to toggle source

Returns the object with the right JSON structure

@return [Hash] The objects data

# File lib/billomat/models/base.rb, line 101
def as_json(_options = nil)
  @data.to_h
end
create() click to toggle source

@return [TrueClass]

# File lib/billomat/models/base.rb, line 53
def create
  resp = Billomat::Gateway.new(
    :post, self.class.base_path, wrapped_data
  ).run

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

  true
end
delete() click to toggle source

@return [TrueClass]

# File lib/billomat/models/base.rb, line 73
def delete
  path = "#{self.class.base_path}/#{id}"
  Billomat::Gateway.new(:delete, path).run

  true
end
id() click to toggle source

@return [String, nil] The object's ID

# File lib/billomat/models/base.rb, line 81
def id
  @data['id'] || nil
end
method_missing(method, *args, &block) click to toggle source

All values in the @data hash can be accessed like a 'normal' method

@example

invoice = Billomat::Models::Invoice.new(invoice_number: '123')
invoice.invoice_number
#=> '123'
Calls superclass method
# File lib/billomat/models/base.rb, line 112
def method_missing(method, *args, &block)
  return @data[method] if @data.to_h.keys.include?(method)
  super
end
respond_to_missing?(method, include_privat = false) click to toggle source

Necessary for method_missing

@param [Symbol] method The method name @param [TrueClass, FalseClass] include_privat @return [TrueClass, FalseClass]

Calls superclass method
# File lib/billomat/models/base.rb, line 123
def respond_to_missing?(method, include_privat = false)
  @data.to_h.keys.include?(method.to_s) || super
end
save() click to toggle source

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

@return [TrueClass]

# File lib/billomat/models/base.rb, line 47
def save
  return create if id.nil?
  update
end
update() click to toggle source

@return [TrueClass]

# File lib/billomat/models/base.rb, line 64
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_data() click to toggle source

Wraps the data so the API accepts the request

@example

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

@return [Hash] The wrapped data

# File lib/billomat/models/base.rb, line 93
def wrapped_data
  { self.class.resource_name => @data.to_h }
end