Module: Hovercraft::Helpers

Defined in:
lib/hovercraft/helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.registered(application) ⇒ Object



3
4
5
# File 'lib/hovercraft/helpers.rb', line 3

def self.registered(application)
  application.helpers(Hovercraft::Helpers)
end

Instance Method Details

#authenticate_with_wardenObject



7
8
9
# File 'lib/hovercraft/helpers.rb', line 7

def authenticate_with_warden
  warden.authenticate! if warden
end

#default_formatObject



42
43
44
# File 'lib/hovercraft/helpers.rb', line 42

def default_format
  :json
end

#formatObject



27
28
29
30
31
# File 'lib/hovercraft/helpers.rb', line 27

def format
  params[:format] ||
  preferred_type  ||
  default_format
end

#format_method_nameObject



23
24
25
# File 'lib/hovercraft/helpers.rb', line 23

def format_method_name
  "to_#{format}"
end

#preferred_typeObject



33
34
35
36
37
38
39
40
# File 'lib/hovercraft/helpers.rb', line 33

def preferred_type
  case request.preferred_type
  when /.*\/json/ then :json
  when /.*\/xml/  then :xml
  when /.*\/\*/   then default_format
  else fail "Unknown media type: %p" % request.preferred_type
  end
end

#respond_with(content) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/hovercraft/helpers.rb', line 15

def respond_with(content)
  if content.respond_to?(format_method_name)
    content.send(format_method_name)
  else
    fail "Serialization method not supported: #{format_method_name}"
  end
end

#wardenObject



11
12
13
# File 'lib/hovercraft/helpers.rb', line 11

def warden
  env.fetch('warden')
end