Class: Gnarly::Url

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

Instance Method Summary collapse

Constructor Details

#initialize(url_mask) ⇒ Url



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/gnarly/url.rb', line 6

def initialize(url_mask)
  case url_mask
  when String
    @url_mask = make_url_mask url_mask
  when Regexp
    @url_mask = url_mask      
  end
  @responders = {}
  @keys = {}
  @allow = []
end

Instance Method Details

#after(&block) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/gnarly/url.rb', line 107

def after(&block)
  if block
    @after = block
  else
    @after
  end
end

#allowObject



62
63
64
# File 'lib/gnarly/url.rb', line 62

def allow()
  @allow.join " "
end

#before(&block) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/gnarly/url.rb', line 99

def before(&block)
  if block
    @before = block
  else
    @before
  end
end

#delete(*keys, &block) ⇒ Object



93
94
95
96
97
# File 'lib/gnarly/url.rb', line 93

def delete(*keys, &block)
  @responders[:delete] = block
  @keys[:delete] = keys
  @allow << "DELETE"
end

#get(*keys, &block) ⇒ Object



75
76
77
78
79
# File 'lib/gnarly/url.rb', line 75

def get(*keys, &block)
  @responders[:get] = block
  @keys[:get] = keys
  @allow << "GET"
end

#helper(helper = nil, &block) ⇒ Object



66
67
68
69
# File 'lib/gnarly/url.rb', line 66

def helper(helper=nil, &block)
  helper = Module.new &block if block and not helper
  (@helpers ||= []) << helper
end

#helpersObject



71
72
73
# File 'lib/gnarly/url.rb', line 71

def helpers()
  @helpers
end

#make_url_mask(template) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/gnarly/url.rb', line 115

def make_url_mask(template)
  types = []
  type_map = {}
  has_types = false
  s = template.gsub(/\{([^\}]*)\}/) do
    name, type = $1.split ":"
    t = nil
    p = case type
        when "integer", "index"
          has_types = true
          t = :integer
          types << t
          '\d+'
        else
          types << nil
          '[^/]+'
        end
    nc = ""
    if name and name != "" 
      nc = "?<#{name}>"
      type_map[name] = t
    end
    "(#{nc}#{p})"
  end
  if has_types
    @types = types
    @type_map = type_map
  end
  Regexp.new "^#{s}$"
end

#match?(path) ⇒ Boolean



18
19
20
21
# File 'lib/gnarly/url.rb', line 18

def match?(path)
  @match = @url_mask.match path if @url_mask
  @match != nil
end

#parameters(method) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gnarly/url.rb', line 23

def parameters(method)
  keys = @keys[method] 
  if keys.empty?
    a = @match.to_a
    a.shift
    if @types
      (0..a.size - 1).collect do |i|
        case @types[i]
        when :integer
          a[i].to_i
        else
          a[i]
        end
      end 
    else
      a
    end
  else
    if @type_map
      keys.collect do |key|
        case @type_map[key.to_s]
        when :integer
          @match[key].to_i            
        else
          @match[key]
        end
      end
    else
      keys.collect do |key|
        @match[key]
      end
    end
  end
end

#post(*keys, &block) ⇒ Object



87
88
89
90
91
# File 'lib/gnarly/url.rb', line 87

def post(*keys, &block)
  @responders[:post] = block
  @keys[:post] = keys
  @allow << "POST"
end

#put(*keys, &block) ⇒ Object



81
82
83
84
85
# File 'lib/gnarly/url.rb', line 81

def put(*keys, &block)
  @responders[:put] = block
  @keys[:put] = keys
  @allow << "PUT"
end

#responder(method) ⇒ Object



58
59
60
# File 'lib/gnarly/url.rb', line 58

def responder(method)
  @responders[method]
end