Module: Gnarly::Provides

Includes:
Base, Status
Included in:
Environment, UUID
Defined in:
lib/gnarly/provides.rb,
lib/gnarly/parameters.rb

Instance Method Summary collapse

Methods included from Status

#created, #default_content_type, #internal_server_error, #method_not_allowed, #not_acceptable, #not_found, #ok, #pop_default_content_type, #push_default_content_type, #unsupported_media_type

Methods included from Base

#body_charset, #logger, #request, #state

Instance Method Details

#parameters(*names) {|params| ... } ⇒ Object

Yields:

  • (params)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gnarly/parameters.rb', line 10

def parameters(*names)
  params = names.collect do |n|
    name, type = n
    p = request.params[name.to_s]
    case type
    when :integer, :int
      if /^-?[0-9]+$/ =~ p
        p.to_i
      else
        nil
      end
    when :symbol
      p.to_sym
    else
      p
    end
  end
  yield *params
end

#provides(*mimes, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gnarly/provides.rb', line 11

def provides(*mimes, &block)
  mime = MIMEParse.best_match mimes, request.accept
  if mime
    push_default_content_type mime
    response = block.call mime if block
    pop_default_content_type
    response
  else
    not_acceptable
  end
end