Avoiding a third party redirect can be solved with simulation of user browser actions.

Sometimes you get a requirement to achieve something by visiting a third party website. For instance, I had to get a compliance check for a PAN number by visiting a third party website and submitting details in a form there. The only caveat in this was avoiding the redirection of user.

Such a problem can be conquered by headless browsing. All you need to do is to spawn a new browser and navigate as a user by leveraging certain commands. PhantomJS is one such headless browser and we can interact with it in Ruby using gem Poltergeist. Poltergeist is just a driver for Capybara which provides DSL for interaction with PhantomJS.

After adding the gem to your Gemfile, you can set it up for your environment like

class PanCheck
  require 'capybara'
  require 'capybara/dsl'
  require 'capybara/poltergeist'
  include Capybara::DSL
  THIRD_PARTY_URL = Rails.application.secrets.third_party_url

  def initialize(pan_number)
    Capybara.current_driver = :poltergeist
    Capybara.register_driver :poltergeist do |app|
      Capybara::Poltergeist::Driver.new(app, {
        js_errors: false,
        phantomjs_options: ['--ignore-ssl-errors=yes', '--local-to-remote-url-access=yes', '--ssl-protocol=TLSv1']
    Capybara.run_server = false # Avoid starting server
    @pan_number = pan_number

Specifications can be provided for the driver and poltergeist which can be observed as js_errors and phantomjs_options respectively in the snippet above.

After setting it up, you can simply add a method to simulate the user actions for compliance check simply using Capybara DSL as

  # Hit the website and get the response
  def pan_check
    status = page.find('.searchResult #statusId').text
    user.update_attributes(pan_status = status)

In case you wish to run any javascript to simulate actions, you can use poltergeist evaluate_script for the same as

status = page.evaluate_script %{(function() {
           $('input[id=panElementId]')[0].value = 'PANNO1234A'
           return $('.searchResult #statusId')[0].innerText

Happy headless browsing :)!!!