Sha256: 826ea7103e9c4aded928d9fb670941c53f85b384a6dfc03b1a6af7978d9798f0
Contents?: true
Size: 1.28 KB
Versions: 1
Compression:
Stored size: 1.28 KB
Contents
module AmpLinkToHelper def self.included(base) base.module_eval do alias_method :original_link_to, :link_to alias_method :link_to, :amp_link_to end end def amp_link_to(name = nil, options = nil, html_options = nil, &block) html_options, options, name = options, name, block if block_given? html_options ||= {} set_analytics_vars(options, html_options) options = cdn_url(options) if html_options.delete(:amp) && AmpHelper.configuration.enable_amp_link options, name, block = html_options, options, name if block_given? original_link_to(name, options, html_options, &block) end private # Pass url to amp-analytics as 'linkUrl' variable. def set_analytics_vars(options, html_options) html_options['data-vars-link-url'] = url_for(options) end # If AMP cache exists, return cdn url. def cdn_url(options) uri = URI(url_for(options)) identify = '' identify += 's/' if request.scheme == 'https' identify += "#{request.host}#{uri.path}#{uri.query}" escaped_host = request.host.gsub('-', '--').gsub('.', '-') cache_url = "https://#{escaped_host}.cdn.ampproject.org/c/#{identify}" response_200?(cache_url) ? cache_url : uri.to_s end def response_200?(url) Net::HTTP.get_response(URI(url)).code == '200' end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
amp_helper-1.0.0.pre.1 | lib/amp_helper/amp_link_to_helper.rb |