Module: APIURLGenerator

Defined in:
lib/api_url_generator.rb,
lib/api_url_generator/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.dectect_polymorphic(object) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/api_url_generator.rb', line 14

def self.dectect_polymorphic(object)
  object.instance_variables.each do |variable|
    if variable.to_s[-9..-1] == "able_type"
      return {
        name: {variable.to_s[1..-1].to_sym => object.instance_eval(variable.to_s)},
        id: {(variable.to_s[1..-5] + "id").to_sym => object.instance_eval(variable.to_s[1..-5] + "id")}
      }
    end
  end

  return false

end

.generate_url(object, nest = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/api_url_generator.rb', line 29

def self.generate_url(object, nest = nil)
  polymorphous = dectect_polymorphic(object)
  if polymorphous && nest
    nested_id = "/#{object.instance_eval(polymorphous[:name].keys[0].to_s).tableize.downcase}/#{object.instance_eval("#{polymorphous[:id].keys[0]}")}"
    object_id = "/#{object.class.to_s.tableize.downcase}/#{object.id}"
    if object.instance_eval(polymorphous[:name].keys[0].to_s).downcase != nest
      raise ArgumentError, "Invalid nest param"
    end
    return "#{@@url}#{nested_id}#{object_id}"
  elsif nest
    begin
      nested_id = "/#{nest.pluralize}/#{object.instance_eval("#{nest}_id")}"
      object_id = "/#{object.class.to_s.tableize.downcase}/#{object.id}"
      return "#{@@url}#{nested_id}#{object_id}"
    rescue NameError
      raise ArgumentError, "Invalid nest param"
    end
  else
    return "#{@@url}/#{object.class.to_s.tableize.downcase}/#{object.id}"
  end
end

.get_urlObject



10
11
12
# File 'lib/api_url_generator.rb', line 10

def self.get_url
  @@url
end

.url(x) ⇒ Object



6
7
8
# File 'lib/api_url_generator.rb', line 6

def self.url(x)
  @@url = x
end