Class: Imglab::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/imglab/utils.rb

Constant Summary collapse

NORMALIZE_PATH_PREFIX_REGEXP =
Regexp.compile(/\A\/*/)
NORMALIZE_PATH_SUFFIX_REGEXP =
Regexp.compile(/\/*$/)
WEB_URI_SCHEMES =
%w[https http]

Class Method Summary collapse

Class Method Details

.normalize_params(params) ⇒ Hash

Returns normalized params, transforming keys with undercores to hyphens.

Parameters:

  • params (Hash)

Returns:

  • (Hash)


19
20
21
22
23
# File 'lib/imglab/utils.rb', line 19

def self.normalize_params(params)
  params.inject({}) do |normalized_params, value|
    normalized_params.merge(normalize_param(dasherize(value[0]), value[1]))
  end
end

.normalize_path(path) ⇒ String

Returns a normalized path where suffix and prefix slashes are removed.

Parameters:

  • path (String)

Returns:

  • (String)


11
12
13
# File 'lib/imglab/utils.rb', line 11

def self.normalize_path(path)
  path.gsub(NORMALIZE_PATH_PREFIX_REGEXP, "").gsub(NORMALIZE_PATH_SUFFIX_REGEXP, "")
end

.web_uri?(uri) ⇒ Boolean

Returns a boolean value indicating whether a string is a valid HTTP/HTTPS URI or not.

Parameters:

  • uri (String)

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/imglab/utils.rb', line 29

def self.web_uri?(uri)
  WEB_URI_SCHEMES.include?(URI.parse(uri).scheme)
rescue URI::Error
  false
end