Module: ApiTools

Included in:
ActiveRecord::Base
Defined in:
lib/api_tools.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/api_tools.rb', line 3

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#as_json(_options = {}) ⇒ Object

TODO: Add relationships for object



29
30
31
# File 'lib/api_tools.rb', line 29

def as_json(_options = {})
  { json_root => self.class.render_class.to_hash(self), 'lims' => configatron.amqp.lims_id! }
end

#for_api(_options = {}) ⇒ Object



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

def for_api(_options = {})
  self.class.render_class.to_hash(self)
end

#json_rootObject



37
38
39
# File 'lib/api_tools.rb', line 37

def json_root
  self.class.to_s.underscore
end

#list_json(_options = {}) ⇒ Object



24
25
26
# File 'lib/api_tools.rb', line 24

def list_json(_options = {})
  self.class.render_class.to_hash_for_list(self)
end

#to_xml(options = {}) ⇒ Object



17
18
19
20
21
22
# File 'lib/api_tools.rb', line 17

def to_xml(options = {})
  renamed_keys =
    for_api.inject({}) { |renamed_keys, (key, value)| renamed_keys.tap { renamed_keys[key.underscore] = value } }
  options.reverse_merge!(root: self.class.to_s.underscore, skip_types: true)
  renamed_keys.to_xml(options)
end

#to_yaml(options = {}) ⇒ Object



33
34
35
# File 'lib/api_tools.rb', line 33

def to_yaml(options = {})
  for_api.to_yaml(options)
end