module EUtils

Public Class Methods

action_to_route(action_name, path_rules = EConstants::PATH_RULES) click to toggle source
# File lib/e-core/utils.rb, line 131
def action_to_route action_name, path_rules = EConstants::PATH_RULES
  action_name = action_name.to_s.dup
  path_rules.each_pair {|from, to| action_name = action_name.gsub(from, to)}
  action_name
end
build_path(path, *args) click to toggle source

takes an arbitrary number of arguments and builds an HTTP path. Hash arguments will transformed into HTTP params. empty hash elements will be ignored.

@example

build_path :some, :page, and: :some_param
#=> some/page?and=some_param
build_path 'another', 'page', with: {'nested' => 'params'}
#=> another/page?with[nested]=params
build_path 'page', with: 'param-added', an_ignored_param: nil
#=> page?with=param-added

@param path @param [Array] args @return [String]

# File lib/e-core/utils.rb, line 46
def build_path path, *args
  path = path.to_s
  args.compact!

  query_string = args.last.is_a?(Hash) && (h = args.pop.delete_if{|k,v| v.nil?}).any? ?
    '?' << ::Rack::Utils.build_nested_query(h) : ''

  args.size == 0 || path =~ /\/\Z/ || args.unshift('')
  path + args.join('/') << query_string
end
canonical_to_route(canonical, action_setup) click to toggle source
# File lib/e-core/utils.rb, line 138
def canonical_to_route canonical, action_setup
  args = [canonical]
  args << action_setup[:action_path] unless action_setup[:action_name] == EConstants::INDEX_ACTION
  EUtils.rootify_url(*args).freeze
end
class_to_route(class_name) click to toggle source

convert class name to URL. basically this will convert Foo to foo FooBar to foo_bar Foo::Bar to foo/bar

# File lib/e-core/utils.rb, line 126
def class_to_route class_name
  '/' << class_name.to_s.split('::').map {|c| underscore(c)}.join('/')
end
deRESTify_action(action) click to toggle source
# File lib/e-core/utils.rb, line 145
def deRESTify_action action
  action_name, request_method = action.to_s.dup, :*
  EConstants::HTTP__REQUEST_METHODS.each do |m|
    regex = /\A#{m}_/i
    if action_name =~ regex
      request_method = m.freeze
      action_name = action_name.sub(regex, '')
      break
    end
  end
  [action_name.to_sym, request_method]
end
encode_token_auth_credentials(token = nil, options = {}) click to toggle source

Encodes the given token and options into an Authorization header value.

@param [String] token @param [Hash] options - optional Hash of the options

# File lib/e-core/utils.rb, line 164
def encode_token_auth_credentials(token = nil, options = {})
  token.is_a?(Hash) && (options = token) && (token = nil)
  token && options = {token: token}.merge(options)
  'Token %s' % options.map {|k,v| '%s=%s' % [k, v.to_s.inspect]}.join(', ')
end
extract_hosts(opts) click to toggle source
# File lib/e-core/utils.rb, line 171
def extract_hosts opts
  hosts = opts[:host] || opts[:hosts]
  hosts = [hosts] unless hosts.is_a?(Array)
  Hash[hosts.compact.map {|h| [h.to_s.strip.downcase.gsub(/\A\w+\:\/\//, ''), true]}]
end
indifferent_hash() click to toggle source

Creates a Hash with indifferent access.

# File lib/e-core/utils.rb, line 84
def indifferent_hash
  Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
end
indifferent_params(object) click to toggle source

Enable string or symbol key access to the nested params hash.

# File lib/e-core/utils.rb, line 69
def indifferent_params(object)
  case object
  when Hash
    new_hash = indifferent_hash
    object.each { |key, value| new_hash[key] = indifferent_params(value) }
    new_hash
  when Array
    object.map { |item| indifferent_params(item) }
  else
    object
  end
end
is_app?(obj) click to toggle source
# File lib/e-core/utils.rb, line 58
def is_app? obj
  obj.respond_to?(:base_url)
end
method_arity(method) click to toggle source
# File lib/e-core/utils.rb, line 89
def method_arity method
  parameters = method.parameters
  min, max = 0, parameters.size

  unlimited = false
  parameters.each_with_index do |param, i|

    increment = param.first == :req

    if (next_param = parameters.values_at(i+1).first)
      increment = true if next_param[0] == :req
    end

    if param.first == :rest
      increment = false
      unlimited = true
    end
    min += 1 if increment
  end
  max = nil if unlimited
  [min, max]
end
normalize_path(path) click to toggle source

“fluffing” potentially hostile paths to avoid paths traversing.

@note

it will also remove duplicating slashes.

@note TERRIBLE SLOW METHOD! use only at load time

@param [String, Symbol] path @return [String]

# File lib/e-core/utils.rb, line 13
def normalize_path path
  path.gsub EConstants::PATH_MODIFIERS, '/'
end
register_extra_engines!() click to toggle source
# File lib/e-more/view/utils.rb, line 3
def register_extra_engines!
  EConstants::VIEW__EXTRA_ENGINES.each do |name, info|
    if Object.const_defined?(name)
      Rabl.register! if name == :Rabl

      # This will constantize the template string
      template = info[:template].split('::').reduce(Object){ |cls, c| cls.const_get(c) }

      EConstants::VIEW__ENGINE_MAPPER[info[:extension]] = template
      EConstants::VIEW__ENGINE_BY_SYM[name] = template
      EConstants::VIEW__EXT_BY_ENGINE[template] = info[:extension].dup.freeze
      EConstants::VIEW__EXTRA_ENGINES.delete(name)
    end
  end
end
rootify_url(*paths) click to toggle source

rootify_url(‘path’) # => /path rootify_url(‘///some-path/’) # => /some-path rootify_url(‘/some’, ‘/path/’) # => /some/path rootify_url(‘some’, ‘another’, ‘path/’) # => /some/another/path

@note slow method! use only at loadtime

# File lib/e-core/utils.rb, line 25
def rootify_url *paths
  '/' << EUtils.normalize_path(paths.compact.join('/')).gsub(/\A\/+|\/+\Z/, '')
end
route_to_regexp(route) click to toggle source
# File lib/e-core/utils.rb, line 63
def route_to_regexp route
  /\A#{Regexp.escape(route).gsub('/', '/+')}(.*)/n
end
underscore(str) click to toggle source

call it like activesupport method convert constant names to underscored (file) names

# File lib/e-core/utils.rb, line 115
def underscore str
  str.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
end

Private Instance Methods

action_to_route(action_name, path_rules = EConstants::PATH_RULES) click to toggle source
# File lib/e-core/utils.rb, line 131
def action_to_route action_name, path_rules = EConstants::PATH_RULES
  action_name = action_name.to_s.dup
  path_rules.each_pair {|from, to| action_name = action_name.gsub(from, to)}
  action_name
end
build_path(path, *args) click to toggle source

takes an arbitrary number of arguments and builds an HTTP path. Hash arguments will transformed into HTTP params. empty hash elements will be ignored.

@example

build_path :some, :page, and: :some_param
#=> some/page?and=some_param
build_path 'another', 'page', with: {'nested' => 'params'}
#=> another/page?with[nested]=params
build_path 'page', with: 'param-added', an_ignored_param: nil
#=> page?with=param-added

@param path @param [Array] args @return [String]

# File lib/e-core/utils.rb, line 46
def build_path path, *args
  path = path.to_s
  args.compact!

  query_string = args.last.is_a?(Hash) && (h = args.pop.delete_if{|k,v| v.nil?}).any? ?
    '?' << ::Rack::Utils.build_nested_query(h) : ''

  args.size == 0 || path =~ /\/\Z/ || args.unshift('')
  path + args.join('/') << query_string
end
canonical_to_route(canonical, action_setup) click to toggle source
# File lib/e-core/utils.rb, line 138
def canonical_to_route canonical, action_setup
  args = [canonical]
  args << action_setup[:action_path] unless action_setup[:action_name] == EConstants::INDEX_ACTION
  EUtils.rootify_url(*args).freeze
end
class_to_route(class_name) click to toggle source

convert class name to URL. basically this will convert Foo to foo FooBar to foo_bar Foo::Bar to foo/bar

# File lib/e-core/utils.rb, line 126
def class_to_route class_name
  '/' << class_name.to_s.split('::').map {|c| underscore(c)}.join('/')
end
deRESTify_action(action) click to toggle source
# File lib/e-core/utils.rb, line 145
def deRESTify_action action
  action_name, request_method = action.to_s.dup, :*
  EConstants::HTTP__REQUEST_METHODS.each do |m|
    regex = /\A#{m}_/i
    if action_name =~ regex
      request_method = m.freeze
      action_name = action_name.sub(regex, '')
      break
    end
  end
  [action_name.to_sym, request_method]
end
encode_token_auth_credentials(token = nil, options = {}) click to toggle source

Encodes the given token and options into an Authorization header value.

@param [String] token @param [Hash] options - optional Hash of the options

# File lib/e-core/utils.rb, line 164
def encode_token_auth_credentials(token = nil, options = {})
  token.is_a?(Hash) && (options = token) && (token = nil)
  token && options = {token: token}.merge(options)
  'Token %s' % options.map {|k,v| '%s=%s' % [k, v.to_s.inspect]}.join(', ')
end
extract_hosts(opts) click to toggle source
# File lib/e-core/utils.rb, line 171
def extract_hosts opts
  hosts = opts[:host] || opts[:hosts]
  hosts = [hosts] unless hosts.is_a?(Array)
  Hash[hosts.compact.map {|h| [h.to_s.strip.downcase.gsub(/\A\w+\:\/\//, ''), true]}]
end
indifferent_hash() click to toggle source

Creates a Hash with indifferent access.

# File lib/e-core/utils.rb, line 84
def indifferent_hash
  Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
end
indifferent_params(object) click to toggle source

Enable string or symbol key access to the nested params hash.

# File lib/e-core/utils.rb, line 69
def indifferent_params(object)
  case object
  when Hash
    new_hash = indifferent_hash
    object.each { |key, value| new_hash[key] = indifferent_params(value) }
    new_hash
  when Array
    object.map { |item| indifferent_params(item) }
  else
    object
  end
end
is_app?(obj) click to toggle source
# File lib/e-core/utils.rb, line 58
def is_app? obj
  obj.respond_to?(:base_url)
end
method_arity(method) click to toggle source
# File lib/e-core/utils.rb, line 89
def method_arity method
  parameters = method.parameters
  min, max = 0, parameters.size

  unlimited = false
  parameters.each_with_index do |param, i|

    increment = param.first == :req

    if (next_param = parameters.values_at(i+1).first)
      increment = true if next_param[0] == :req
    end

    if param.first == :rest
      increment = false
      unlimited = true
    end
    min += 1 if increment
  end
  max = nil if unlimited
  [min, max]
end
normalize_path(path) click to toggle source

“fluffing” potentially hostile paths to avoid paths traversing.

@note

it will also remove duplicating slashes.

@note TERRIBLE SLOW METHOD! use only at load time

@param [String, Symbol] path @return [String]

# File lib/e-core/utils.rb, line 13
def normalize_path path
  path.gsub EConstants::PATH_MODIFIERS, '/'
end
register_extra_engines!() click to toggle source
# File lib/e-more/view/utils.rb, line 3
def register_extra_engines!
  EConstants::VIEW__EXTRA_ENGINES.each do |name, info|
    if Object.const_defined?(name)
      Rabl.register! if name == :Rabl

      # This will constantize the template string
      template = info[:template].split('::').reduce(Object){ |cls, c| cls.const_get(c) }

      EConstants::VIEW__ENGINE_MAPPER[info[:extension]] = template
      EConstants::VIEW__ENGINE_BY_SYM[name] = template
      EConstants::VIEW__EXT_BY_ENGINE[template] = info[:extension].dup.freeze
      EConstants::VIEW__EXTRA_ENGINES.delete(name)
    end
  end
end
rootify_url(*paths) click to toggle source

rootify_url(‘path’) # => /path rootify_url(‘///some-path/’) # => /some-path rootify_url(‘/some’, ‘/path/’) # => /some/path rootify_url(‘some’, ‘another’, ‘path/’) # => /some/another/path

@note slow method! use only at loadtime

# File lib/e-core/utils.rb, line 25
def rootify_url *paths
  '/' << EUtils.normalize_path(paths.compact.join('/')).gsub(/\A\/+|\/+\Z/, '')
end
route_to_regexp(route) click to toggle source
# File lib/e-core/utils.rb, line 63
def route_to_regexp route
  /\A#{Regexp.escape(route).gsub('/', '/+')}(.*)/n
end
underscore(str) click to toggle source

call it like activesupport method convert constant names to underscored (file) names

# File lib/e-core/utils.rb, line 115
def underscore str
  str.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
end