Class: OmniAuth::Form

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

Constant Summary collapse

DEFAULT_CSS =
File.read(File.expand_path('../form.css', __FILE__))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Form

Returns a new instance of Form.



7
8
9
10
11
12
13
14
15
16
# File 'lib/omniauth/form.rb', line 7

def initialize(options = {})
  options[:title] ||= 'Authentication Info Required'
  options[:header_info] ||= ''
  self.options = options

  @html = +'' # unary + string allows it to be mutable if strings are frozen
  @with_custom_button = false
  @footer = nil
  header(options[:title], options[:header_info])
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/omniauth/form.rb', line 5

def options
  @options
end

Class Method Details

.build(options = {}, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/omniauth/form.rb', line 18

def self.build(options = {}, &block)
  form = OmniAuth::Form.new(options)
  if block.arity > 0
    yield form
  else
    form.instance_eval(&block)
  end
  form
end

Instance Method Details

#button(text) ⇒ Object



50
51
52
53
# File 'lib/omniauth/form.rb', line 50

def button(text)
  @with_custom_button = true
  @html << "\n<button type='submit'>#{text}</button>"
end

#fieldset(legend, options = {}, &block) ⇒ Object



59
60
61
62
63
64
# File 'lib/omniauth/form.rb', line 59

def fieldset(legend, options = {}, &block)
  @html << "\n<fieldset#{" style='#{options[:style]}'" if options[:style]}#{" id='#{options[:id]}'" if options[:id]}>\n  <legend>#{legend}</legend>\n"
  instance_eval(&block)
  @html << "\n</fieldset>"
  self
end


83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/omniauth/form.rb', line 83

def footer
  return self if @footer

  @html << "\n<button type='submit'>Connect</button>" unless @with_custom_button
  @html << "  </form>\n  </body>\n  </html>\n  HTML\n  @footer = true\n  self\nend\n"

#header(title, header_info) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/omniauth/form.rb', line 66

def header(title, header_info)
  @html << "  <!DOCTYPE html>\n  <html>\n  <head>\n    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n    <title>\#{title}</title>\n    \#{css}\n    \#{header_info}\n  </head>\n  <body>\n  <h1>\#{title}</h1>\n  <form method='post' \#{\"action='\#{options[:url]}' \" if options[:url]}noValidate='noValidate'>\n  HTML\n  self\nend\n"

#html(html) ⇒ Object



55
56
57
# File 'lib/omniauth/form.rb', line 55

def html(html)
  @html << html
end

#input_field(type, name) ⇒ Object



33
34
35
36
# File 'lib/omniauth/form.rb', line 33

def input_field(type, name)
  @html << "\n<input type='#{type}' id='#{name}' name='#{name}'/>"
  self
end

#label_field(text, target) ⇒ Object



28
29
30
31
# File 'lib/omniauth/form.rb', line 28

def label_field(text, target)
  @html << "\n<label for='#{target}'>#{text}:</label>"
  self
end

#password_field(label, name) ⇒ Object



44
45
46
47
48
# File 'lib/omniauth/form.rb', line 44

def password_field(label, name)
  label_field(label, name)
  input_field('password', name)
  self
end

#text_field(label, name) ⇒ Object



38
39
40
41
42
# File 'lib/omniauth/form.rb', line 38

def text_field(label, name)
  label_field(label, name)
  input_field('text', name)
  self
end

#to_htmlObject



96
97
98
99
# File 'lib/omniauth/form.rb', line 96

def to_html
  footer
  @html
end

#to_responseObject



101
102
103
104
# File 'lib/omniauth/form.rb', line 101

def to_response
  footer
  Rack::Response.new(@html, 200, 'content-type' => 'text/html').finish
end