Module: Elmas::Resource
- Includes:
- Sanitizer, UriMethods
- Included in:
- Account, Address, AgingReceivablesList, BankAccount, BankEntry, BankEntryLine, BaseEntryLine, CashEntry, CashEntryLine, Contact, Costcenter, Costunit, Division, Document, DocumentAttachment, GLAccount, GeneralJournalEntry, GeneralJournalEntryLine, GoodsDelivery, GoodsDeliveryLine, Item, ItemGroup, Journal, Layout, Mailbox, PaymentCondition, PrintedSalesInvoice, Project, PurchaseEntry, PurchaseEntryLine, ReceivablesList, SalesEntry, SalesEntryLine, SalesInvoice, SalesInvoiceLine, SalesItemPrices, SalesOrder, SalesOrderLine, TimeTransaction, Transaction, TransactionLine, User, VatCode
- Defined in:
- lib/elmas/uri.rb,
lib/elmas/resource.rb,
lib/elmas/sanitizer.rb
Defined Under Namespace
Modules: Sanitizer, UriMethods
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #delete ⇒ Object
- #find ⇒ Object
- #find_all(options = {}) ⇒ Object
-
#find_by(options = {}) ⇒ Object
Pass filters in an array, for example ‘filters: [:id, :name]’.
-
#get(uri = self.uri) ⇒ Object
Normally use the url method (which applies the filters) but sometimes you only want to use the base path or other paths.
- #id ⇒ Object
- #id? ⇒ Boolean
- #initialize(attributes = {}) ⇒ Object
-
#method_missing(method, *args, &block) ⇒ Object
Getter/Setter for resource.
- #save ⇒ Object
- #valid? ⇒ Boolean
Methods included from Sanitizer
#sanitize, #sanitize_date_time, #sanitize_has_many, #sanitize_relationship
Methods included from UriMethods
#apply_filters, #apply_order, #apply_select, #base_filter, #base_path, #basic_identifier_uri, #query_attribute, #sanitize_value, #uri
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Getter/Setter for resource
80 81 82 83 84 85 86 87 88 |
# File 'lib/elmas/resource.rb', line 80 def method_missing(method, *args, &block) yield if block if /^(\w+)=$/ =~ method set_attribute($1, args[0]) else nil unless @attributes[method.to_sym] end @attributes[method.to_sym] end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
13 14 15 |
# File 'lib/elmas/resource.rb', line 13 def attributes @attributes end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
14 15 16 |
# File 'lib/elmas/resource.rb', line 14 def response @response end |
#url ⇒ Object
Returns the value of attribute url.
13 14 15 |
# File 'lib/elmas/resource.rb', line 13 def url @url end |
Instance Method Details
#delete ⇒ Object
74 75 76 77 |
# File 'lib/elmas/resource.rb', line 74 def delete return nil unless id? Elmas.delete(basic_identifier_uri) end |
#find ⇒ Object
42 43 44 45 46 |
# File 'lib/elmas/resource.rb', line 42 def find return nil unless id? response = get(uri([:id])) response&.results&.first end |
#find_all(options = {}) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/elmas/resource.rb', line 26 def find_all( = {}) @order_by = [:order_by] @select = [:select] response = get(uri(%i[order select])) response&.results end |
#find_by(options = {}) ⇒ Object
Pass filters in an array, for example ‘filters: [:id, :name]’
34 35 36 37 38 39 40 |
# File 'lib/elmas/resource.rb', line 34 def find_by( = {}) @filters = [:filters] @order_by = [:order_by] @select = [:select] response = get(uri(%i[order select filters])) response&.results end |
#get(uri = self.uri) ⇒ Object
Normally use the url method (which applies the filters) but sometimes you only want to use the base path or other paths
49 50 51 |
# File 'lib/elmas/resource.rb', line 49 def get(uri = self.uri) @response = Elmas.get(URI.unescape(uri.to_s)) end |
#id ⇒ Object
22 23 24 |
# File 'lib/elmas/resource.rb', line 22 def id @attributes[:id] end |
#id? ⇒ Boolean
59 60 61 |
# File 'lib/elmas/resource.rb', line 59 def id? !@attributes[:id].nil? end |
#initialize(attributes = {}) ⇒ Object
16 17 18 19 20 |
# File 'lib/elmas/resource.rb', line 16 def initialize(attributes = {}) @attributes = Utils.normalize_hash(attributes) @filters = [] @query = [] end |
#save ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/elmas/resource.rb', line 63 def save attributes_to_submit = sanitize if valid? return @response = Elmas.post(base_path, params: attributes_to_submit) unless id? return @response = Elmas.put(basic_identifier_uri, params: attributes_to_submit) else Elmas.error("Invalid Resource #{self.class.name}, attributes: #{@attributes.inspect}") Elmas::Response.new(Faraday::Response.new(status: 400, body: "Invalid Request")) end end |
#valid? ⇒ Boolean
53 54 55 56 57 |
# File 'lib/elmas/resource.rb', line 53 def valid? mandatory_attributes.all? do |attribute| @attributes.key? attribute end end |