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.



8
9
10
11
# File 'lib/samanage/url_builder.rb', line 8

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

Instance Attribute Details

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

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



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
59
60
61
62
63
64
# File 'lib/samanage/url_builder.rb', line 14

def map_path(path: nil, options: nil)
  url = ""
  parameters = ""
  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 /solution/
    url += "solutions"
  when /contract/
    url += "contracts"
  when /problem/
    url += "problems"
  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
  url
end