Go to file
chiu f3797b796d first commit 2020-05-28 22:06:56 +08:00
bin first commit 2020-05-28 22:06:56 +08:00
lib first commit 2020-05-28 22:06:56 +08:00
spec first commit 2020-05-28 22:06:56 +08:00
.rspec first commit 2020-05-28 22:06:56 +08:00
.travis.yml first commit 2020-05-28 22:06:56 +08:00
CODE_OF_CONDUCT.md first commit 2020-05-28 22:06:56 +08:00
Gemfile first commit 2020-05-28 22:06:56 +08:00
Gemfile.lock first commit 2020-05-28 22:06:56 +08:00
LICENSE.txt first commit 2020-05-28 22:06:56 +08:00
README.md first commit 2020-05-28 22:06:56 +08:00
Rakefile first commit 2020-05-28 22:06:56 +08:00
repost.gemspec first commit 2020-05-28 22:06:56 +08:00

README.md

Gem Repost implements Redirect using POST method

Gem Version Build Status

Installation

Add this line to your application's Gemfile:

gem 'repost'

And then execute:

$ bundle

Or install it yourself as:

$ gem install repost

Diagram

What problem does it solve?

When you need to send some parameters to an endpoint which should redirect you after execution. There wouldn't be a problem if an endpoint receives [GET], because you can just use redirect_to post_url(id: @model.id, token: model.token...).

But when an endpoint receives [POST], you have to generate html form and submit it. So repost gem helps to avoid creation of additional view with html form, just use redirect_post method instead. I faced with this problem when was dealing with bank transactions. You can see the approximate scheme:

Usage

If you use Rails, gem automatically includes helper methods to your controllers:

repost(...)

and, as an alias

redirect_post(...)

Under the hood it calls render method of current controller with html:.

Example in Rails app:

class MyController < ApplicationController
  ...
  def index
    repost(...)
  end
  ...
end

If you use Sinatra, Roda or etc., you need to require it first somewhere in you project:

require 'repost'

Then ask your senpai to generate a string with html:

Repost::Senpai.perform(...)

Example in Sinatra, Roda, etc. app:

class MyController < Sinatra::Base
  get '/' do
    Repost::Senpai.perform(...)
  end
end

Reminder:

  • In Rails app use repost or redirect_post method in your controller which performs 'redirect' when it is called.

  • In Sinatra, Roda, etc. app or if you need html output - call Senpai

Full example:

UPD: authenticity token is turned off by default. Use :auto or 'auto' to turn on default authenticity token from Rails. Any other string value would be treated as custom auth token value.

Repost::Senpai.perform('http://examp.io/endpoint',  # URL, looks understandable 
  params: {a: 1, b: 2, c: '3', d: "4"},             # Your request body
  options: {
    method: :post,                                  # OPTIONAL - DEFAULT is :post, but you can use others if needed
    authenticity_token: 'auto',                     # OPTIONAL - :auto or 'auto' for Rails form_authenticity_token, string - custom token
    charset: 'Windows-1251',                        # OPTIONAL - DEFAULT is "UTF-8", corresponds for accept-charset
    form_id: 'CustomFormID',                        # OPTIONAL - DEFAULT is autogenerated
    autosubmit: false,                              # OPTIONAL - DEFAULT is true, if you want to get a confirmation for redirect  
    decor: {                                        # If autosubmit is turned off or Javascript is disabled on client
      section: {                                    # ... you can decorate confirmation section and button
        classes: 'red-bg red-text',                 # OPTIONAL - <DIV> section, set classNames, separate with space
        html: '<h1>Press this button, dude!</h1>'   # OPTIONAL - Any html, which will appear before submit button
      },
      submit: {
        classes: 'button-decorated round-border',   # OPTIONAL - <Input> with type submit, set classNames, separate with space
        text: 'c0n71nue ...'                        # OPTIONAL - DEFAULT is 'Continue'
      }
    }
  }
)

Authenticity Token (Rails)

Currently you can pass the authenticity token in two ways:

  • Recommended:

    Use options and :auto to pass the auth token. That should protect you from any implementation changes in future Rails versions

    redirect_post('https://exmaple.io/endpoint', options: {authenticity_token: :auto})
    
  • Or, it is still valid to:

    use params and form_authenticity_token method directly from ActionController

    redirect_post('https://exmaple.io/endpoint', params: {authenticity_token: form_authenticity_token})
    

License

The gem is available as open source under the terms of the MIT License.

Copyright © 2019 Yaro.

GitHub license

That's all folks.