Class: BusinessCentral::Object::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/business_central/object/base.rb

Direct Known Subclasses

Attachments, Companies, Picture

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, **args) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/business_central/object/base.rb', line 10

def initialize(client, **args)
  @client = client
  @object_path = args.fetch(
    :object_path,
    [
      {
        path: 'companies',
        id: args.fetch(:company_id, client.default_company_id)
      },
      {
        path: args.fetch(:object_name, '').to_s.to_camel_case,
        id: args.fetch(:id, nil)
      }
    ]
  )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(object_name, **params) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/business_central/object/base.rb', line 56

def method_missing(object_name, **params)
  @object_path << {
    path: object_name.to_s.to_camel_case,
    id: params.fetch(:id, nil)
  }
  if BusinessCentral::Object.const_defined?(object_name.to_s.to_class_sym)
    klass = BusinessCentral::Object.const_get(object_name.to_s.to_class_sym)
    return klass.new(client, **params.merge!({ object_path: @object_path }))
  end
  self
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/business_central/object/base.rb', line 8

def client
  @client
end

Instance Method Details

#create(params = {}) ⇒ Object



41
42
43
# File 'lib/business_central/object/base.rb', line 41

def create(params = {})
  Request.post(@client, build_url, params)
end

#destroy(id) ⇒ Object Also known as: delete



50
51
52
53
# File 'lib/business_central/object/base.rb', line 50

def destroy(id)
  object = find_by_id(id)
  Request.delete(@client, build_url(object_id: id), object[:etag])
end

#find_allObject Also known as: all



27
28
29
# File 'lib/business_central/object/base.rb', line 27

def find_all
  Request.get(@client, build_url)
end

#find_by_id(id) ⇒ Object Also known as: find



32
33
34
# File 'lib/business_central/object/base.rb', line 32

def find_by_id(id)
  Request.get(@client, build_url(object_id: id))
end

#respond_to_missing?(_object_name, *_params) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/business_central/object/base.rb', line 68

def respond_to_missing?(_object_name, *_params)
  true
end

#update(id, params = {}) ⇒ Object



45
46
47
48
# File 'lib/business_central/object/base.rb', line 45

def update(id, params = {})
  object = find_by_id(id).merge(params)
  Request.patch(@client, build_url(object_id: id), object[:etag], params)
end

#where(query = '', *values) ⇒ Object



37
38
39
# File 'lib/business_central/object/base.rb', line 37

def where(query = '', *values)
  Request.get(@client, build_url(filter: FilterQuery.sanitize(query, values)))
end