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



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

#attributesObject

Returns the value of attribute attributes.



11
12
13
# File 'lib/elmas/resource.rb', line 11

def attributes
  @attributes
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#urlObject

Returns the value of attribute url.



11
12
13
# File 'lib/elmas/resource.rb', line 11

def url
  @url
end

Instance Method Details

#deleteObject



72
73
74
75
# File 'lib/elmas/resource.rb', line 72

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

#findObject



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

#idObject



20
21
22
# File 'lib/elmas/resource.rb', line 20

def id
  @attributes[:id]
end

#id?Boolean

Returns:

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

#saveObject



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

Returns:

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