Module: Grom::Helpers

Included in:
Base
Defined in:
lib/grom/helpers.rb

Instance Method Summary collapse

Instance Method Details

#all_base_url_builder(class_name, *options) ⇒ Object


25
26
27
28
29
30
31
# File 'lib/grom/helpers.rb', line 25

def all_base_url_builder(class_name, *options)
  endpoint = "#{API_ENDPOINT}/#{create_plural_property_name(class_name)}"
    options.each do |option|
      endpoint += "/#{option}" unless option.nil?
    end
  endpoint
end

#associations_url_builder(owner_object, associated_class_name, options = {}) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/grom/helpers.rb', line 6

def associations_url_builder(owner_object, associated_class_name, options={})
  id = owner_object.id
  associated_class_name = options[:single].nil? ? create_plural_property_name(associated_class_name) : create_property_name(associated_class_name)
  endpoint = "#{find_base_url_builder(owner_object.class.name, id)}/#{associated_class_name}"
  if options[:optional].nil?
    endpoint += '.ttl'
  else
    options[:optional].each do |option|
      endpoint += "/#{option}" unless option.nil?
    end
    endpoint += '.ttl'
  end
  endpoint
end

#create_class_name(plural_name) ⇒ Object


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

def create_class_name(plural_name)
  ActiveSupport::Inflector.camelize(ActiveSupport::Inflector.singularize(plural_name.to_s).capitalize)
end

#create_plural_property_name(class_name) ⇒ Object


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

def create_plural_property_name(class_name)
  ActiveSupport::Inflector.pluralize(create_property_name(class_name))
end

#create_property_name(class_name) ⇒ Object


41
42
43
# File 'lib/grom/helpers.rb', line 41

def create_property_name(class_name)
  ActiveSupport::Inflector.underscore(class_name).downcase
end

#find_base_url_builder(class_name, id) ⇒ Object


21
22
23
# File 'lib/grom/helpers.rb', line 21

def find_base_url_builder(class_name, id)
  "#{API_ENDPOINT}/#{create_plural_property_name(class_name)}/#{id}"
end

#json_ld(data) ⇒ Object


58
59
60
61
62
63
64
65
66
# File 'lib/grom/helpers.rb', line 58

def json_ld(data)
  data = [data] unless data.is_a?(Array)
  json_ld = {}
  json_ld["@context"] = data.first.class.context
  json_ld["@graph"] = data.map do |object|
    json_ld_object_mapper(object)
  end
  json_ld.to_json
end

#json_ld_object_mapper(object) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/grom/helpers.rb', line 68

def json_ld_object_mapper(object)
  hash = { "@type": object.class.type }
  object.instance_variables.each do |variable_name|
    getter = "#{variable_name}".tr('@', '')
    string_variable_name = variable_name.to_s
    if string_variable_name == "@id"
      hash[string_variable_name] = "#{object.class.id_prefix}#{(object.send(getter))}"
    else
      property_name = object.class.property_translator.key(getter)
      hash[property_name] = object.send(getter) unless property_name.nil?
    end
  end
  hash
end

#order_list(arr, *parameters) ⇒ Object


45
46
47
48
49
50
51
52
# File 'lib/grom/helpers.rb', line 45

def order_list(arr, *parameters)
  rejected = []
  arr.delete_if{ |obj| rejected << obj if parameters.any?{ |param| obj.send(param).nil? } }
  sorted_arr = arr.sort_by do |obj|
    parameters.map{ |param| obj.send(param) }.select{ |p| not p.nil? }
  end
  rejected.concat(sorted_arr)
end

#order_list_by_through(arr, through_association, property) ⇒ Object


54
55
56
# File 'lib/grom/helpers.rb', line 54

def order_list_by_through(arr, through_association, property)
  arr.map{ |obj| obj.send(through_association) }.flatten.sort{ |a, b| a[property] <=> b[property] }
end