Sha256: be11cfbd5a02a6a51ec8f67d98e1b6db069238e31b2383d2c082a0731089d58e

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

module Ganapati
  class FileUrl
    attr_reader :scheme, :host, :port, :path

    def initialize(url, default_scheme='yourmom', default_host='localhost', default_port=8118)
      @url = url
      if @url.start_with? 'file://' or (not @url.start_with? 'hdfs://' and default_scheme == :file)
        parse_file_scheme
      else
        parse_hdfs_scheme(default_host, default_port)
      end
    end

    def to_s
      @url
    end

    def hdfs?
      @scheme == :hdfs
    end

    def local?
      @scheme == :file
    end

    private
    def parse_file_scheme
      @scheme = :file
      @path = @url.slice(7, @url.length-7)
    end

    def parse_hdfs_scheme(default_host, default_port)
      @scheme = :hdfs
      @path = @url.start_with?('hdfs://') ? @url.slice(7, @url.length-7) : @url
      if @path.start_with? '/'
        @host = default_host
        @port = default_port.to_i
      else
        parts = @path.split('/')
        hostport = parts.shift.split(':')
        @host = hostport.first
        @port = (hostport.length == 2) ? hostport.last.to_i : default_port.to_i
        @path = '/' + parts.join('/')
      end        
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ganapati-0.0.7 lib/ganapati/utils.rb
ganapati-0.0.6 lib/ganapati/utils.rb
ganapati-0.0.5 lib/ganapati/utils.rb