Class: GrowthForecast::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/growthforecast/path.rb

Constant Summary collapse

PATH_KEYS =
[:id,:service_name,:section_name,:graph_name,:complex]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Path

Returns a new instance of Path.



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

def initialize(obj)
  if obj.is_a?(String)
    obj = JSON.parse(obj)
  end
  obj.keys.each do |k|
    obj[k.to_sym] = obj.delete(k)
  end

  @id = obj[:id].to_i
  @service_name = obj[:service_name]
  @section_name = obj[:section_name]
  @graph_name = obj[:graph_name]
  @complex = obj[:complex] ? true : false
end

Instance Attribute Details

#complexObject

Returns the value of attribute complex.



7
8
9
# File 'lib/growthforecast/path.rb', line 7

def complex
  @complex
end

#graph_nameObject

Returns the value of attribute graph_name.



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

def graph_name
  @graph_name
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#section_nameObject

Returns the value of attribute section_name.



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

def section_name
  @section_name
end

#service_nameObject

Returns the value of attribute service_name.



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

def service_name
  @service_name
end

Class Method Details

.path?(obj) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
# File 'lib/growthforecast/path.rb', line 10

def self.path?(obj)
  if obj.is_a?(String)
    obj = JSON.parse(obj)
  end
  keys = obj.keys.size
  if keys >= 4 && keys <= 5 && obj.keys.map(&:to_sym).reduce(true){|r,k| r && PATH_KEYS.include?(k)}
    return true
  end
  false
end

Instance Method Details

#complex?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/growthforecast/path.rb', line 36

def complex?
  @complex
end

#to_graphObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/growthforecast/path.rb', line 47

def to_graph
  if @complex
    GrowthForecast::Complex.new({
        complex: true, id: @id, service_name: @service_name, section_name: @section_name, graph_name: @graph_name
      })
  else
    GrowthForecast::Graph.new({
        complex: false, id: @id, service_name: @service_name, section_name: @section_name, graph_name: @graph_name
      })
  end
end

#to_jsonObject



40
41
42
43
44
45
# File 'lib/growthforecast/path.rb', line 40

def to_json
  {
    'id' => @id, 'complex' => @complex,
    'service_name' => @service_name, 'section_name' => @section_name, 'graph_name' => @graph_name,
  }.to_json
end