Class: Gors::Routes

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

Constant Summary collapse

NAME_PATTERN =
/:(\S+)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Routes

Returns a new instance of Routes.



112
113
114
115
116
117
# File 'lib/gors.rb', line 112

def initialize logger
  @routes = {}
  @models = {}
  @logger = logger
  @routespost = {}
end

Instance Attribute Details

#modelsObject

Returns the value of attribute models.



109
110
111
# File 'lib/gors.rb', line 109

def models
  @models
end

#postsObject

Returns the value of attribute posts.



110
111
112
# File 'lib/gors.rb', line 110

def posts
  @posts
end

#routes(path) ⇒ Object

Returns the value of attribute routes.



108
109
110
# File 'lib/gors.rb', line 108

def routes
  @routes
end

Instance Method Details

#build_regex(signature) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/gors.rb', line 163

def build_regex signature
    return %r(^#{signature}$) unless signature.include?(':')

    groups = signature.split('/').map do |part|
      next part if part.empty?
      next part unless part.start_with? ':'
      name = NAME_PATTERN.match(part)[1]
      %Q{(?<#{name}>\\S+)}
    end.select {|s| !s.empty? }.join('\/')

    %r(^\/#{groups}$)
end

#get(hash) ⇒ Object



119
120
121
122
# File 'lib/gors.rb', line 119

def get hash
  @routes[pattern_for(hash.keys.first.to_s)] = hash[hash.keys.first.to_s]
  @logger.log "Adding route GET "+hash.keys.first.to_s
end

#jsonObject



124
125
126
# File 'lib/gors.rb', line 124

def json
  # TODO zorg evoor dat routes ook van routes.json gehaald kunnen worden (structuur zie generator in de CLI)
end

#model(hash) ⇒ Object



137
138
139
140
# File 'lib/gors.rb', line 137

def model hash
  @routes["/api/"+hash.keys.first.to_s] = "Gors::Model#call:"+hash[hash.keys.first.to_s];
  puts "Adding model with path "+hash.keys.first.to_s
end

#pattern_for(signature) ⇒ Object

Logic from github.com/alisnic/nyny



156
157
158
159
160
161
# File 'lib/gors.rb', line 156

def pattern_for signature
    if(signature.class == Regexp)
      return signature
    end
    build_regex(signature.start_with?('/') ? signature : "/#{signature}")
end

#postObject



128
129
130
131
# File 'lib/gors.rb', line 128

def post hash
  @routespost[hash.keys.first.to_s] = hash[hash.keys.first.to_s]
  @logger.log "Adding route POST "+hash.keys.first.to_s
end