Sha256: 10a41910a992d3e8ede9f58b34db5ad5e88cd0120ba6920b49d617576c1ba5a3

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

# -*- encoding: utf-8 -*-
require 'cgi'

module MultiForecast
  module ConversionRule
    def uri_escape(string)
      # + => '%20' is to avoid GF (Kossy?) bug
      # . => '%2E' because a/./b is recognized as a/b as URL
      CGI.escape(string).gsub('+', '%20').gsub('.', '%2E') if string
    end

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

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

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

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

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

    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

    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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
multiforecast-client-0.62.0.5 lib/multiforecast/conversion_rule.rb