Module: MultiForecast::ConversionRule

Included in:
CLI, Client
Defined in:
lib/multiforecast/conversion_rule.rb

Instance Method Summary collapse

Instance Method Details

#graph_name(path) ⇒ Object



34
35
36
37
# File 'lib/multiforecast/conversion_rule.rb', line 34

def graph_name(path)
  path = lstrip(path, '/')
  File.basename(path)
end

#id(path) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/multiforecast/conversion_rule.rb', line 46

def id(path)
  path = lstrip(path, '/')
  @mapping.each do |base_path, base_uri|
    return base_path if path.index(base_path) == 0
  end
  return @mapping.keys.first
end

#ids(path) ⇒ Object



54
55
56
57
58
59
# File 'lib/multiforecast/conversion_rule.rb', line 54

def ids(path)
  path = lstrip(path, '/')
  @mapping.map do |base_path, base_uri|
    base_path if path.index(base_path) == 0
  end.compact
end

#lstrip(string, substring) ⇒ Object



18
19
20
# File 'lib/multiforecast/conversion_rule.rb', line 18

def lstrip(string, substring)
  string.index(substring) == 0 ? string[substring.size..-1] : string
end

#path(service_name, section_name, graph_name) ⇒ Object



39
40
41
42
43
44
# File 'lib/multiforecast/conversion_rule.rb', line 39

def path(service_name, section_name, graph_name)
  return "#{service_name}/#{section_name}/#{graph_name}" unless service_name == "multiforecast"
  dirname = uri_unescape(section_name)
  basename = graph_name
  dirname == "." ? basename : "#{dirname}/#{basename}"
end

#section_name(path) ⇒ Object



28
29
30
31
32
# File 'lib/multiforecast/conversion_rule.rb', line 28

def section_name(path)
  path = lstrip(path, '/')
  return path.split('/')[1] if path.count('/') == 2
  uri_escape(File.dirname(path))
end

#service_name(path) ⇒ Object



22
23
24
25
26
# File 'lib/multiforecast/conversion_rule.rb', line 22

def service_name(path)
  path = lstrip(path, '/')
  return path.split('/')[0] if path.count('/') == 2
  'multiforecast'
end

#uri_escape(string) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/multiforecast/conversion_rule.rb', line 6

def uri_escape(string)
  # Here, we want to do CGI.escape to encode '/', but GF (Plack?) does URI.unescape when it receives.
  # Major Difference: In URI.escape, ' ' => '%20'. In CGI.escape, ' ' => '+'.
  # Thus, we need to convert as ' ' => '+', and then, '+' => '%20' so that GF can unescape '%20' => ' '.
  # '.' => '%2E' because a/./b is recognized as a/b as URL
  CGI.escape(string).gsub('+', '%20').gsub('.', '%2E') if string
end

#uri_unescape(string) ⇒ Object



14
15
16
# File 'lib/multiforecast/conversion_rule.rb', line 14

def uri_unescape(string)
  CGI.unescape(string) if string
end