module Eol::Resource
Attributes
attributes[RW]
response[R]
url[RW]
Public Class Methods
new(attributes = {})
click to toggle source
# File lib/eol/resource.rb, line 16 def initialize(attributes = {}) @attributes = Utils.normalize_hash(attributes) @filters = [] @query = [] end
Public Instance Methods
delete()
click to toggle source
# File lib/eol/resource.rb, line 74 def delete return nil unless id? Eol.delete(basic_identifier_uri) end
find()
click to toggle source
# File lib/eol/resource.rb, line 42 def find return nil unless id? response = get(uri([:id])) response&.results&.first end
find_all(options = {})
click to toggle source
# File lib/eol/resource.rb, line 26 def find_all(options = {}) @order_by = options[:order_by] @select = options[:select] response = get(uri(%i[order select])) response&.results end
find_by(options = {})
click to toggle source
Pass filters in an array, for example 'filters: [:id, :name]'
# File lib/eol/resource.rb, line 34 def find_by(options = {}) @filters = options[:filters] @order_by = options[:order_by] @select = options[:select] response = get(uri(%i[order select filters])) response&.results end
get(uri = self.uri)
click to toggle source
Normally use the url method (which applies the filters) but sometimes you only want to use the base path or other paths
# File lib/eol/resource.rb, line 49 def get(uri = self.uri) @response = Eol.get(URI.unescape(uri.to_s)) end
id()
click to toggle source
# File lib/eol/resource.rb, line 22 def id @attributes[:id] end
id?()
click to toggle source
# File lib/eol/resource.rb, line 59 def id? !@attributes[:id].nil? end
method_missing(method, *args) { || ... }
click to toggle source
Getter/Setter for resource
# File lib/eol/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
save()
click to toggle source
# File lib/eol/resource.rb, line 63 def save attributes_to_submit = sanitize if valid? return @response = Eol.post(base_path, params: attributes_to_submit) unless id? return @response = Eol.put(basic_identifier_uri, params: attributes_to_submit) else Eol.error("Invalid Resource #{self.class.name}, attributes: #{@attributes.inspect}") Eol::Response.new(Faraday::Response.new(status: 400, body: "Invalid Request")) end end
valid?()
click to toggle source
# File lib/eol/resource.rb, line 53 def valid? mandatory_attributes.all? do |attribute| @attributes.key? attribute end end
Private Instance Methods
set_attribute(attribute, value)
click to toggle source
# File lib/eol/resource.rb, line 92 def set_attribute(attribute, value) @attributes[attribute.to_sym] = value if valid_attribute?(attribute) end
valid_attribute?(attribute)
click to toggle source
# File lib/eol/resource.rb, line 96 def valid_attribute?(attribute) valid_attributes.include?(attribute.to_sym) end
valid_attributes()
click to toggle source
# File lib/eol/resource.rb, line 100 def valid_attributes @valid_attributes ||= mandatory_attributes.inject(other_attributes, :<<) end