Sha256: fc0e30ab609bdf45166eccdb83d2923a40a25770ce708110b488000dc63687bc

Contents?: true

Size: 1.76 KB

Versions: 6

Compression:

Stored size: 1.76 KB

Contents

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

module MultiForecast
  module ConversionRule
    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

    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

6 entries across 6 versions & 1 rubygems

Version Path
multiforecast-client-0.80.0.3 lib/multiforecast/conversion_rule.rb
multiforecast-client-0.80.0.2 lib/multiforecast/conversion_rule.rb
multiforecast-client-0.80.0.1 lib/multiforecast/conversion_rule.rb
multiforecast-client-0.80.0.0 lib/multiforecast/conversion_rule.rb
multiforecast-client-0.72.0.1 lib/multiforecast/conversion_rule.rb
multiforecast-client-0.62.0.8 lib/multiforecast/conversion_rule.rb