class Rack::SmartAppBanner

Constants

VERSION

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/rack/smart-app-banner.rb, line 9
def initialize(app, options = {})
  raise ArgumentError, 'App ID Required' unless options[:app_id] &&
                                                !options[:app_id].empty?
  raise ArgumentError, 'Incomplete Affiliate Information' if (options[:affiliate_partner_id] || options[:affiliate_site_id]) && !(options[:affiliate_partner_id] && options[:affiliate_site_id])
  raise ArgumentError, 'App Argument Must Be Lambda' if options[:app_argument] && !options[:app_argument].lambda?

  @app = app
  @options = options
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/smart-app-banner.rb, line 19
def call(env)
  @status, @headers, @response = @app.call(env)
  return [@status, @headers, @response] unless html?

  request = Rack::Request.new(env)
  response = Rack::Response.new([], @status, @headers)
  if @response.respond_to?(:to_ary)
    @response.each do |fragment|
      response.write inject_smart_app_banner(request, fragment)
    end
  end

  response.finish
end

Private Instance Methods

html?() click to toggle source
# File lib/rack/smart-app-banner.rb, line 36
def html?
  @headers['Content-Type'] =~ /html/
end
inject_smart_app_banner(request, response) click to toggle source
# File lib/rack/smart-app-banner.rb, line 40
    def inject_smart_app_banner(request, response)
      parameters = { 'app-id' => @options[:app_id] }
      parameters['affiliate-data'] = "partnerId=#{@options[:affiliate_partner_id]}&siteID=#{@options[:affiliate_site_id]}" if @options[:affiliate_partner_id] && @options[:affiliate_site_id]
      parameters['app-argument'] = @options[:app_argument].call(request) if @options[:app_argument]

      if argument = parameters['app-argument']
        parameters['app-argument'] = "http://#{argument}" unless URI(argument).scheme
      end

      content = parameters.collect { |k, v| "#{k}=#{v}" }.join(', ')

      meta = <<~EOF
        <meta name="apple-itunes-app" content="#{content}" />
      EOF

      response.gsub(%r{</head>}, meta + '</head>')
    end