class Repost::Senpai
Constants
- DEFAULT_CHARSET
- DEFAULT_SUBMIT_BUTTON_TEXT
Attributes
authenticity_token[R]
autosubmit[R]
autosubmit_nonce[R]
charset[R]
form_id[R]
method[R]
options[R]
params[R]
section_classes[R]
section_html[R]
submit_classes[R]
submit_text[R]
url[R]
Public Class Methods
new(url, params: {}, options: {})
click to toggle source
# File lib/repost/senpai.rb, line 6 def initialize(url, params: {}, options: {}) @url = url @params = params @options = options @method = options.fetch(:method, :post) @authenticity_token = options.fetch(:authenticity_token, nil) @charset = options.fetch(:charset, DEFAULT_CHARSET) @form_id = options.fetch(:form_id, generated_form_id) @autosubmit = options.fetch(:autosubmit, true) @autosubmit_nonce = options.fetch(:autosubmit_nonce, nil) @section_classes = options.dig(:decor, :section, :classes) @section_html = options.dig(:decor, :section, :html) @submit_classes = options.dig(:decor, :submit, :classes) @submit_text = options.dig(:decor, :submit, :text) || DEFAULT_SUBMIT_BUTTON_TEXT end
Public Instance Methods
perform()
click to toggle source
# File lib/repost/senpai.rb, line 22 def perform compiled_body = if autosubmit form_body << auto_submit_script << no_script else form_body << submit_section end form_head << compiled_body << form_footer end
Private Instance Methods
auto_submit_script()
click to toggle source
# File lib/repost/senpai.rb, line 89 def auto_submit_script nonce_attr = %Q( nonce="#{autosubmit_nonce}") if autosubmit_nonce "<script#{nonce_attr}> document.getElementById('#{form_id}').submit(); </script>" end
csrf_token()
click to toggle source
# File lib/repost/senpai.rb, line 68 def csrf_token "<input name='authenticity_token' value='#{authenticity_token}' type='hidden'>" end
form_body()
click to toggle source
# File lib/repost/senpai.rb, line 41 def form_body inputs = params.map do |key, value| form_input(key, value) end inputs.unshift(csrf_token) if authenticity_token inputs.join end
form_head()
click to toggle source
# File lib/repost/senpai.rb, line 37 def form_head "<form id='#{form_id}' action='#{url}' method='#{method}' accept-charset='#{charset}'>" end
form_input(key, value)
click to toggle source
# File lib/repost/senpai.rb, line 49 def form_input(key, value) case value when Hash value.map do |inner_key, inner_value| form_input("#{key}[#{inner_key}]", inner_value) end.join when Array value.map do |inner_value| form_input("#{key}[]", inner_value) end.join else "<input type='hidden' name='#{key}' value=#{process_value(value)}>" end end
generated_form_id()
click to toggle source
# File lib/repost/senpai.rb, line 85 def generated_form_id "repost-#{Time.now.to_i}" end
no_script()
click to toggle source
# File lib/repost/senpai.rb, line 72 def no_script "<noscript> #{submit_section} </noscript>" end
process_value(value)
click to toggle source
# File lib/repost/senpai.rb, line 96 def process_value(value) return value if value.is_a?(Integer) '\'' + value.to_s + '\'' end
submit_section()
click to toggle source
# File lib/repost/senpai.rb, line 78 def submit_section "<div class='#{section_classes}'> #{section_html} <input class='#{submit_classes}' type='submit' value='#{submit_text}'></input> </div>" end