Class: Samanage::UrlBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/samanage/url_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, options: nil) ⇒ UrlBuilder

Returns a new instance of UrlBuilder.



6
7
8
9
# File 'lib/samanage/url_builder.rb', line 6

def initialize(path: nil,options: nil)
	self.url =  map_path(path: path, options: options)
	return url
end

Instance Attribute Details

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#map_path(path: nil, options: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/samanage/url_builder.rb', line 12

def map_path(path: nil, options: nil)
	url = String.new
	parameters = String.new
	case path
	when /user/
		url += 'users'
	when /hardware/
		url += 'hardwares'
	when /other_asset/
		url += 'other_assets'
	when /incident/
		url += 'incidents'
	when /change/
		url += 'changes'
	when /custom_field/
		url += 'custom_fields'
	when /custom_form/
		url += 'custom_forms'
	when /mobile/
		url += 'mobiles'
	when /site/
		url += 'sites'
	when /department/
		url += 'departments'
	when /contract/
		url += 'contracts'
	when /group/
		url += 'groups'
	end

	if path.match(/(\d)+/)
		url += "/" + path.match(/(\d)+/)[0] + ".json"
		return url
	end

	options.each_pair do |field, value|
		if field.to_s == 'id' && value.to_s.match(/(\d)+/)
			url += "/#{value}.json"
			# Return. Filters not valid on an id
			return url
		end
		sub_param = "?#{field}=#{value}"
		parameters += sub_param + '&'
	end
	url += ".json" + parameters
	return url
end