class BusinessCentral::Object::Base

Attributes

client[R]
company_id[W]
id[W]
parent_path[R]
path[R]

Public Class Methods

new(client, args = {}) click to toggle source
# File lib/business_central/object/base.rb, line 12
def initialize(client, args = {})
  @client = client
  @id = id(args)
  @company_id = company_id(args)
  @parent_path = @company_id.nil? ? [] : [
    {
      path: 'companies',
      id: @company_id
    }
  ]
end

Public Instance Methods

all()
Alias for: find_all
create(params = {}) click to toggle source
# File lib/business_central/object/base.rb, line 64
def create(params = {})
  if !method_supported?(:post)
    raise BusinessCentral::NoSupportedMethod.new(:post, object_methods)
  end

  Validation.new(object_validation, params).valid?
  Request.post(@client, build_url(parent_path: @parent_path, child_path: object_name), params)
end
destroy(id) click to toggle source
# File lib/business_central/object/base.rb, line 92
def destroy(id)
  if !method_supported?(:delete)
    raise BusinessCentral::NoSupportedMethod.new(:delete, object_methods)
  end

  object = find_by_id(id)
  Request.delete(
    @client,
    build_url(
      parent_path: @parent_path,
      child_path: object_name,
      child_id: id
    ),
    object[:etag]
  )
end
find(id)
Alias for: find_by_id
find_all() click to toggle source
# File lib/business_central/object/base.rb, line 24
def find_all
  if !method_supported?(:get)
    raise BusinessCentral::NoSupportedMethod.new(:get, object_methods)
  end

  Request.get(@client, build_url(parent_path: @parent_path, child_path: object_name))
end
Also aliased as: all
find_by_id(id) click to toggle source
# File lib/business_central/object/base.rb, line 33
def find_by_id(id)
  if !method_supported?(:get)
    raise BusinessCentral::NoSupportedMethod.new(:get, object_methods)
  end

  Request.get(
    @client,
    build_url(
      parent_path: @parent_path,
      child_path: object_name,
      child_id: id
    )
  )
end
Also aliased as: find
update(id, params = {}) click to toggle source
# File lib/business_central/object/base.rb, line 73
def update(id, params = {})
  if !method_supported?(:patch)
    raise BusinessCentral::NoSupportedMethod.new(:patch, object_methods)
  end

  object = find_by_id(id).merge(params)
  Validation.new(object_validation, object).valid?
  Request.patch(
    @client,
    build_url(
      parent_path: @parent_path,
      child_path: object_name,
      child_id: id
    ),
    object[:etag],
    params
  )
end
where(query = '', *values) click to toggle source
# File lib/business_central/object/base.rb, line 49
def where(query = '', *values)
  if !method_supported?(:get)
    raise BusinessCentral::NoSupportedMethod.new(:get, object_methods)
  end

  Request.get(
    @client,
    build_url(
      parent_path: @parent_path,
      child_path: object_name,
      filter: FilterQuery.sanitize(query, values)
    )
  )
end

Protected Instance Methods

build_url(parent_path: [], child_path: '', child_id: '', filter: '') click to toggle source
# File lib/business_central/object/base.rb, line 117
def build_url(parent_path: [], child_path: '', child_id: '', filter: '')
  URLBuilder.new(
    base_url: client.url,
    parent_path: parent_path,
    child_path: child_path,
    child_id: child_id,
    filter: filter
  ).build
end
valid_parent?(parent) click to toggle source
# File lib/business_central/object/base.rb, line 111
def valid_parent?(parent)
  return true if object_parent_name.map(&:downcase).include?(parent.downcase)

  raise InvalidArgumentException, "parents allowed: #{object_parent_name.join(', ')}"
end

Private Instance Methods

method_supported?(method) click to toggle source
# File lib/business_central/object/base.rb, line 149
def method_supported?(method)
  return true if object_methods.include?(method)

  false
end
object_methods() click to toggle source
# File lib/business_central/object/base.rb, line 141
def object_methods
  self.class.const_get(:OBJECT_METHODS)
end
object_name() click to toggle source
# File lib/business_central/object/base.rb, line 129
def object_name
  self.class.const_get(:OBJECT)
end
object_parent_name() click to toggle source
# File lib/business_central/object/base.rb, line 145
def object_parent_name
  self.class.const_get(:OBJECT_PARENTS)
end
object_validation() click to toggle source
# File lib/business_central/object/base.rb, line 133
def object_validation
  if self.class.const_defined?(:OBJECT_VALIDATION)
    self.class.const_get(:OBJECT_VALIDATION)
  else
    []
  end
end