Sha256: 910c163fe5b264a320e4141c97e7adba87788407b0d6d65ae0247b8353f4fd10

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

require 'securerandom'
module Rack::App::Utils
  extend self

  # Normalizes URI path.
  #
  # Strips off trailing slash and ensures there is a leading slash.
  #
  #   normalize_path("/foo")  # => "/foo"
  #   normalize_path("/foo/") # => "/foo"
  #   normalize_path("foo")   # => "/foo"
  #   normalize_path("")      # => "/"
  def normalize_path(path)
    path = "/#{path}"
    path.squeeze!('/')
    path.sub!(%r{/+\Z}, '')
    path = '/' if path == ''
    path
  end

  # Based on ActiveSupport, removed inflections.
  # https://github.com/rails/rails/blob/v4.1.0.rc1/activesupport/lib/active_support/inflector/methods.rb
  def underscore(camel_cased_word)
    word = camel_cased_word.to_s.gsub('::', '/')
    word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
    word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
    word.tr!("-", "_")
    word.downcase!
    word
  end

  def pwd(*path_parts)

    root_folder = if ENV['BUNDLE_GEMFILE']
                    ENV['BUNDLE_GEMFILE'].split(File::Separator)[0..-2].join(File::Separator)
                  else
                    Dir.pwd.to_s
                  end

    return File.join(root_folder, *path_parts)

  end

  def uuid
    ary = SecureRandom.random_bytes(16).unpack("NnnnnN")
    ary[2] = (ary[2] & 0x0fff) | 0x4000
    ary[3] = (ary[3] & 0x3fff) | 0x8000
    "%08x-%04x-%04x-%04x-%04x%08x" % ary
  end

  def join(*url_path_parts)
    url_path_parts = [url_path_parts].flatten.compact
    File.join(*url_path_parts).gsub(File::Separator, '/').sub(/^\/?(.*)$/, '/\1')
  end

  def expand_path(file_path)
    case file_path

      when /^\.\//
        File.expand_path(File.join(File.dirname(caller[1]),file_path))

      when /^[^\/]/
        File.join(caller[1].split(/\.(?:rb|ru)/)[0],file_path)

      when /^\//
        pwd(file_path)

    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rack-app-1.1.0 lib/rack/app/utils.rb
rack-app-1.0.0 lib/rack/app/utils.rb
rack-app-0.24.0 lib/rack/app/utils.rb
rack-app-0.23.0 lib/rack/app/utils.rb
rack-app-0.22.0 lib/rack/app/utils.rb