Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b58595a1fe
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 43 lines (35 sloc) 0.998 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
require 'digest'

module HTMLPress
  class IdentityParser
    class << self
      attr_writer :auth_token_matcher, :secret_string
      
      def auth_token_matcher
        @auth_token_matcher || /(input name=["']authenticity_token["'] type=["']hidden["'] value=["'])([^"']*)/i
      end
      
      def secret_string
        @secret_string || "REPLACE_THIS_STRING_WITH_YOUR_SECRET"
      end
    end
    
    attr_reader :identifiable_html
    
    def initialize(html)
      @html = html
      extract_and_substitute_tokens
    end
    
    def id
      Digest::MD5.hexdigest(@identifiable_html)
    end
    
    def populate_tokens_into(html)
      html.gsub!(self.class.secret_string) do |match|
        @auth_tokens.shift
      end
    end
    
    private
    def extract_and_substitute_tokens
      @auth_tokens = []
      @identifiable_html = @html.gsub(self.class.auth_token_matcher) do |match|
        @auth_tokens << $2
        $1 + self.class.secret_string
      end
    end
  end
end
Something went wrong with that request. Please try again.