Class: Pennylane::Resources::Base
- Defined in:
- lib/pennylane/resources/base.rb
Direct Known Subclasses
Category, CategoryGroup, Customer, CustomerInvoice, Product, Supplier
Class Method Summary collapse
- .client ⇒ Object
- .execute_resource_request(method, path, params = {}, opts = {}, usage = []) ⇒ Object
- .normalize_filters(filters) ⇒ Object
- .object_name ⇒ Object
- .object_name_plural ⇒ Object
- .request_pennylane_object(method:, path:, params: {}, opts: {}, usage: [], with: {}) ⇒ Object
Instance Method Summary collapse
-
#[](k) ⇒ Object
object happens to be nil when the object is the nested object.
-
#id ⇒ Object
object happens to be nil when the object is in a list.
-
#method_missing(method_name, *args, &block) ⇒ Object
So we can call directly method on the object rather than going through his key Pennylane::Customer.retrieve(‘any-id’).name == Pennylane::Customer.retrieve(‘any-id’).customer.name.
- #object ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#update(attributes) ⇒ Object
def inspect id_string = respond_to?(:id) && !id.nil? ? “ id=##id” : “” “#<#selfself.class:0x#object_idobject_id.to_s(16)#id_string> JSON: ” + JSON.pretty_generate(object.instance_variable_get(:@values) || @values) end.
Methods inherited from Object
build_from, descendants, #initialize_from_response, objects, protected_fields
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
So we can call directly method on the object rather than going through his key Pennylane::Customer.retrieve(‘any-id’).name == Pennylane::Customer.retrieve(‘any-id’).customer.name
76 77 78 79 |
# File 'lib/pennylane/resources/base.rb', line 76 def method_missing(method_name, *args, &block) raise NoMethodError, "undefined method `#{method_name}` for #{self.class}.\nMethods available : #{@values.keys}" unless object object.send(method_name, *args, &block) end |
Class Method Details
.client ⇒ Object
34 35 36 37 |
# File 'lib/pennylane/resources/base.rb', line 34 def client @client ||= {} @client[Pennylane.api_key] ||= Pennylane::Client.new(Pennylane.api_key) end |
.execute_resource_request(method, path, params = {}, opts = {}, usage = []) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/pennylane/resources/base.rb', line 24 def execute_resource_request(method, path, params = {}, opts = {}, usage = []) resp = client.request( method, path, params: params, opts: opts ) [JSON.parse(resp.read_body || "{}"), opts] # in case body is nil ew return an empty hash end |
.normalize_filters(filters) ⇒ Object
39 40 41 42 |
# File 'lib/pennylane/resources/base.rb', line 39 def normalize_filters(filters) filters[:filter] = filters[:filter].to_json if filters[:filter] filters end |
.object_name ⇒ Object
7 8 9 |
# File 'lib/pennylane/resources/base.rb', line 7 def object_name name&.split('::')&.last&.downcase end |
.object_name_plural ⇒ Object
11 12 13 |
# File 'lib/pennylane/resources/base.rb', line 11 def object_name_plural "#{object_name}s" end |
.request_pennylane_object(method:, path:, params: {}, opts: {}, usage: [], with: {}) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/pennylane/resources/base.rb', line 15 def request_pennylane_object(method:, path:, params: {}, opts: {}, usage: [], with: {}) resp, opts = execute_resource_request(method, path, params, opts, usage) if resp.empty? {} else Util.convert_to_pennylane_object(Util.normalize_response(resp, with), params, opts) end end |
Instance Method Details
#[](k) ⇒ Object
object happens to be nil when the object is the nested object
53 54 55 |
# File 'lib/pennylane/resources/base.rb', line 53 def [](k) (object && object[k.to_sym]) || @values[k.to_sym] end |
#id ⇒ Object
object happens to be nil when the object is in a list
46 47 48 49 50 |
# File 'lib/pennylane/resources/base.rb', line 46 def id object.source_id rescue super end |
#object ⇒ Object
58 59 60 |
# File 'lib/pennylane/resources/base.rb', line 58 def object @values[self.class.object_name.to_sym] end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
81 82 83 |
# File 'lib/pennylane/resources/base.rb', line 81 def respond_to_missing?(method_name, include_private = false) object.respond_to?(method_name) || super end |
#update(attributes) ⇒ Object
def inspect
id_string = respond_to?(:id) && !id.nil? ? " id=#{id}" : ""
"#<#{self.class}:0x#{object_id.to_s(16)}#{id_string}> JSON: " +
JSON.pretty_generate(object.instance_variable_get(:@values) || @values)
end
68 69 70 71 72 |
# File 'lib/pennylane/resources/base.rb', line 68 def update(attributes) resp, opts = self.class.request_pennylane_object(method: :put, path: "/#{self.class.object_name_plural}/#{id}", params: { body: { self.class.object_name => attributes } }) @values = resp.instance_variable_get :@values self end |