Module: BusinessCentral::ApiMethods
- Included in:
- Base
- Defined in:
- lib/business_central/api_methods.rb
Instance Method Summary collapse
-
#create(*args) ⇒ Object
Performs the POST operation on the supplied args.
-
#delete(*args) ⇒ Object
Performs the DELETE operation on the supplied args.
-
#get(*args) ⇒ Object
Determines if the GET operation is for all remote objects, a single object, or a single object with optional parameters being supplied.
-
#get_child(parent_id, child_id = nil) ⇒ Object
Gets a child object, or array of child objects.
-
#method_missing(method_name, *args, &block) ⇒ Object
The method_missing method is used to determine if a called method exists in the defined constant for each local object - SUPPORTED_METHODS.
-
#query(*args) ⇒ Object
Performs a GET operation with a supplied filter string, to search for remote objects.
- #query_child(parent_id, query) ⇒ Object
-
#update(*args) ⇒ Object
Performs the PATCH operation on the supplied args.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
The method_missing method is used to determine if a called method exists in the defined constant for each local object - SUPPORTED_METHODS. This ensures that only supported operations can be called.
15 16 17 18 19 20 21 |
# File 'lib/business_central/api_methods.rb', line 15 def method_missing(method_name, *args, &block) if supported_method?(method_name.to_sym) self.send(method_name.to_sym, args) else "#{method_name} is not supported" end end |
Instance Method Details
#create(*args) ⇒ Object
Performs the POST operation on the supplied args
105 106 107 108 |
# File 'lib/business_central/api_methods.rb', line 105 def create(*args) return if args.length == 0 post(args[0]).first end |
#delete(*args) ⇒ Object
Performs the DELETE operation on the supplied args
Expects arguments (id, etag)
127 128 129 130 |
# File 'lib/business_central/api_methods.rb', line 127 def delete(*args) return if args.length == 0 destroy(args[0], args[1]) end |
#get(*args) ⇒ Object
Determines if the GET operation is for all remote objects, a single object, or a single object with optional parameters being supplied
When no parameters are supplied, the GET will be for all remote objects eg
.get()
When one parameter is supplied, the GET will be for the single object eg
.get("1234")
When more than one parameter is supplied, the GET will be for the single object, and then for the supplied optional parameters
An example of the options is
.get("1234", "$extended=customerFinancialDetails")
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/business_central/api_methods.rb', line 42 def get(*args) case args.length when 0 find_collection when 1 find_by_id(args[0]).first else (*args).first end end |
#get_child(parent_id, child_id = nil) ⇒ Object
Gets a child object, or array of child objects
When only the parent_id is supplied, the GET will return all child objects of that parent eg
get_child(1234)
when both paremeters are present, the GET will return the single child object eg
get_child(1234, 4321)
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/business_central/api_methods.rb', line 68 def get_child(parent_id, child_id = nil) url = build_url(parent_id, child_id) response = @client.get(url) handle_error(response) results = process(response) if results.is_a?(Array) return results if child_id.nil? || results.length > 1 return results.first end end |
#query(*args) ⇒ Object
Performs a GET operation with a supplied filter string, to search for remote objects.
eg
.get("'lastModifiedDate lt '2019-01-01T01:01:01+08:00'")
89 90 91 92 93 |
# File 'lib/business_central/api_methods.rb', line 89 def query(*args) response = @client.get("/#{api_object}?$filter=#{args[0]}") handle_error(response) process(response) end |
#query_child(parent_id, query) ⇒ Object
95 96 97 98 99 |
# File 'lib/business_central/api_methods.rb', line 95 def query_child(parent_id, query) response = @client.get("/#{api_object_parent}(#{parent_id})/#{api_object}?$filter=#{query}") handle_error(response) process(response) end |
#update(*args) ⇒ Object
Performs the PATCH operation on the supplied args
Expects arguments (id, etag, data)
116 117 118 119 |
# File 'lib/business_central/api_methods.rb', line 116 def update(*args) return if args.length == 0 patch(args[0], args[1], args[2]).first end |