Module: Elmas::Resource

Defined Under Namespace

Modules: Sanitizer, UriMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#attributesObject

Returns the value of attribute attributes.



13
14
15
# File 'lib/elmas/resource.rb', line 13

def attributes
  @attributes
end

#responseObject (readonly)

Returns the value of attribute response.



14
15
16
# File 'lib/elmas/resource.rb', line 14

def response
  @response
end

#urlObject

Returns the value of attribute url.



13
14
15
# File 'lib/elmas/resource.rb', line 13

def url
  @url
end

Instance Method Details

#deleteObject



74
75
76
77
# File 'lib/elmas/resource.rb', line 74

def delete
  return nil unless id?
  Elmas.delete(basic_identifier_uri)
end

#findObject



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(options = {})
  @order_by = options[:order_by]
  @select = options[: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(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) ⇒ 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

#idObject



22
23
24
# File 'lib/elmas/resource.rb', line 22

def id
  @attributes[:id]
end

#id?Boolean

Returns:

  • (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

#saveObject



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

Returns:

  • (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