Module: EUtils

Included in:
EBuilder, ERewriter
Defined in:
lib/e-core/utils.rb,
lib/e-more/view/utils.rb

Class Method Summary collapse

Class Method Details

.action_to_route(action_name, path_rules = EConstants::PATH_RULES) ⇒ Object



131
132
133
134
135
# 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) ⇒ String

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

Examples:

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

Parameters:

  • path
  • args (Array)

Returns:

  • (String)


46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



138
139
140
141
142
# 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) ⇒ Object

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



126
127
128
# 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) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# 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 = {}) ⇒ Object

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

Parameters:

  • token (String) (defaults to: nil)
  • options (Hash) (defaults to: {})
    • optional Hash of the options



164
165
166
167
168
# 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) ⇒ Object



171
172
173
174
175
# 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_hashObject

Creates a Hash with indifferent access.



84
85
86
# 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) ⇒ Object

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



69
70
71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/e-core/utils.rb', line 58

def is_app? obj
  obj.respond_to?(:base_url)
end

.method_arity(method) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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) ⇒ String

Note:

it will also remove duplicating slashes.

Note:

TERRIBLE SLOW METHOD! use only at load time

“fluffing” potentially hostile paths to avoid paths traversing.

Parameters:

  • path (String, Symbol)

Returns:

  • (String)


13
14
15
# File 'lib/e-core/utils.rb', line 13

def normalize_path path
  path.gsub EConstants::PATH_MODIFIERS, '/'
end

.register_extra_engines!Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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) ⇒ Object

Note:

slow method! use only at loadtime

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



25
26
27
# 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) ⇒ Object



63
64
65
# File 'lib/e-core/utils.rb', line 63

def route_to_regexp route
  /\A#{Regexp.escape(route).gsub('/', '/+')}(.*)/n
end

.underscore(str) ⇒ Object

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



115
116
117
# 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