Module: DomoscioRails::Resource::ClassMethods

Defined in:
lib/domoscio_rails/resource.rb

Instance Method Summary collapse

Instance Method Details

#class_nameObject



8
9
10
# File 'lib/domoscio_rails/resource.rb', line 8

def class_name
  name.split('::')[-1]
end

#nesting?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/domoscio_rails/resource.rb', line 16

def nesting?
  name.split('::').size > 2
end

#parent_classObject



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

def parent_class
  name.split('::')[-2]
end

#raw_params(params = {}) ⇒ Object



46
47
48
49
50
# File 'lib/domoscio_rails/resource.rb', line 46

def raw_params(params = {})
  params.merge({
    instance_id: DomoscioRails.configuration.client_id
  })
end

#raw_url(util_name) ⇒ Object

Raises:

  • (NotImplementedError)


36
37
38
39
40
41
42
43
44
# File 'lib/domoscio_rails/resource.rb', line 36

def raw_url(util_name)
  raise NotImplementedError, 'Resource is an abstract class. Do not use it directly.' if self == Resource

  build_url = "/v#{DomoscioRails.configuration.version}"
  build_url << "/#{underscore(class_name)}s"
  build_url << "/#{util_name}" if util_name

  build_url
end

#underscore(string) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/domoscio_rails/resource.rb', line 52

def underscore(string)
  string.gsub(/::/, '/')
        .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
        .gsub(/([a-z\d])([A-Z])/, '\1_\2')
        .tr('-', '_')
        .downcase
end

#url(id, util_name, params) ⇒ Object

Raises:

  • (NotImplementedError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/domoscio_rails/resource.rb', line 20

def url(id, util_name, params)
  raise NotImplementedError, 'Resource is an abstract class. Do not use it directly.' if self == Resource

  build_url = "/v#{DomoscioRails.configuration.version}/instances/#{DomoscioRails.configuration.client_id}"
  if nesting?
    parent = underscore(parent_class)
    build_url << "/#{parent}s"
    build_url << "/#{CGI.escape(params[(parent + '_id').to_sym].to_s)}"
  end
  build_url << "/#{underscore(class_name)}s"
  build_url << "/#{util_name}" if util_name
  build_url << "/#{CGI.escape(id.to_s)}" if id

  build_url
end