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
Public Class Methods
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
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
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
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
@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
@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
@return [String, nil] The object's ID
# File lib/billomat/models/base.rb, line 81 def id @data['id'] || nil end
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'
# 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
Necessary for method_missing
@param [Symbol] method The method name @param [TrueClass, FalseClass] include_privat @return [TrueClass, FalseClass]
# 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
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
@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
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