module MultiForecast::ConversionRule

Public Instance Methods

graph_name(path) click to toggle source
# File lib/multiforecast/conversion_rule.rb, line 33
def graph_name(path)
  path = lstrip(path, '/')
  File.basename(path)
end
id(path) click to toggle source
# File lib/multiforecast/conversion_rule.rb, line 45
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) click to toggle source
# File lib/multiforecast/conversion_rule.rb, line 53
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) click to toggle source
# File lib/multiforecast/conversion_rule.rb, line 17
def lstrip(string, substring)
  string.index(substring) == 0 ? string[substring.size..-1] : string
end
path(service_name, section_name, graph_name) click to toggle source
# File lib/multiforecast/conversion_rule.rb, line 38
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) click to toggle source
# File lib/multiforecast/conversion_rule.rb, line 27
def section_name(path)
  path = lstrip(path, '/')
  return path.split('/')[1] if path.count('/') == 2
  uri_escape(File.dirname(path))
end
service_name(path) click to toggle source
# File lib/multiforecast/conversion_rule.rb, line 21
def service_name(path)
  path = lstrip(path, '/')
  return path.split('/')[0] if path.count('/') == 2
  'multiforecast'
end
uri_escape(string) click to toggle source
# File lib/multiforecast/conversion_rule.rb, line 5
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) click to toggle source
# File lib/multiforecast/conversion_rule.rb, line 13
def uri_unescape(string)
  CGI.unescape(string) if string
end