Module: Elmas::Resource
- Includes:
- Sanitizer, UriMethods
- Included in:
- Account, AgingReceivablesList, BankAccount, BankEntry, BankEntryLine, Contact, Costcenter, Costunit, Document, DocumentAttachment, GLAccount, GeneralJournalEntry, GeneralJournalEntryLine, Item, ItemGroup, Journal, Layout, Mailbox, PaymentCondition, PrintedSalesInvoice, Project, PurchaseEntry, PurchaseEntryLine, ReceivablesList, SalesEntry, SalesEntryLine, SalesInvoice, SalesInvoiceLine, SalesItemPrices, SalesOrder, SalesOrderLine, TimeTransaction, Transaction, TransactionLine, 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
78 79 80 81 82 83 84 85 86 |
# File 'lib/elmas/resource.rb', line 78 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.
11 12 13 |
# File 'lib/elmas/resource.rb', line 11 def attributes @attributes end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
12 13 14 |
# File 'lib/elmas/resource.rb', line 12 def response @response end |
#url ⇒ Object
Returns the value of attribute url.
11 12 13 |
# File 'lib/elmas/resource.rb', line 11 def url @url end |
Instance Method Details
#delete ⇒ Object
72 73 74 75 |
# File 'lib/elmas/resource.rb', line 72 def delete return nil unless id? Elmas.delete(basic_identifier_uri) end |
#find ⇒ Object
40 41 42 43 44 |
# File 'lib/elmas/resource.rb', line 40 def find return nil unless id? response = get(uri([:id])) response.results.first if response end |
#find_all(options = {}) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/elmas/resource.rb', line 24 def find_all( = {}) @order_by = [:order_by] @select = [:select] response = get(uri([:order, :select])) response.results if response end |
#find_by(options = {}) ⇒ Object
Pass filters in an array, for example ‘filters: [:id, :name]’
32 33 34 35 36 37 38 |
# File 'lib/elmas/resource.rb', line 32 def find_by( = {}) @filters = [:filters] @order_by = [:order_by] @select = [:select] response = get(uri([:order, :select, :filters])) response.results if response 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
47 48 49 |
# File 'lib/elmas/resource.rb', line 47 def get(uri = self.uri) @response = Elmas.get(URI.unescape(uri.to_s)) end |
#id ⇒ Object
20 21 22 |
# File 'lib/elmas/resource.rb', line 20 def id @attributes[:id] end |
#id? ⇒ Boolean
57 58 59 |
# File 'lib/elmas/resource.rb', line 57 def id? !@attributes[:id].nil? end |
#initialize(attributes = {}) ⇒ Object
14 15 16 17 18 |
# File 'lib/elmas/resource.rb', line 14 def initialize(attributes = {}) @attributes = Utils.normalize_hash(attributes) @filters = [] @query = [] end |
#save ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/elmas/resource.rb', line 61 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
51 52 53 54 55 |
# File 'lib/elmas/resource.rb', line 51 def valid? mandatory_attributes.all? do |attribute| @attributes.key? attribute end end |