Sha256: a6e5e7a5317eae233c9684cacd49c66a0d183ee33e7cd8c4f279d2e6c5d36704

Contents?: true

Size: 1.87 KB

Versions: 6

Compression:

Stored size: 1.87 KB

Contents

require 'google/api_client'

module KepplerGaDashboard
  module Admin

    # DashboarController
    class DashboardController < ::ApplicationController
      layout 'keppler_ga_dashboard/admin/layouts/application'
      before_action :dashboard_access, only: [:analytics]
      before_action :set_apparience_colors
      before_action :authenticate_user!

      def analytics
        # set up a client instance
        client = ::Google::APIClient.new(
          application_name: 'keppler',
          application_version: '1'
        )

        client.authorization = Signet::OAuth2::Client.new(
          options
        ).tap(&:fetch_access_token!)

        @access_token = client.authorization.fetch_access_token!['access_token']

        gon.color = @color
      rescue StandardError
        render :connection_error
      end

      # Options for authenticate
      def options
        { token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
          audience: 'https://accounts.google.com/o/oauth2/token',
          scope: 'https://www.googleapis.com/auth/analytics.readonly',
          issuer: Rails.application.secrets.ga_auth.fetch(
            :service_account_email_address
          ),
          signing_key: Google::APIClient::KeyUtils.load_from_pkcs12(
            file_key, 'notasecret'
          )
        }
      end

      def set_apparience_colors
        variables_file = File.readlines(style_file)
        @color = ""
        variables_file.each { |line| @color = line[15..21] if line.include?('$keppler-color') }
      end

      def style_file
        "#{Rails.root}/app/assets/stylesheets/admin/utils/_variables.scss"
      end

      # get .p12 File for authenticate token
      def file_key
        File.join(
          Rails.root,
          'config',
          'gaAuth',
          Rails.application.secrets.ga_auth.fetch(:file_key_name)
        )
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
keppler-2.1.18 installer/core/rockets/keppler_ga_dashboard/app/controllers/keppler_ga_dashboard/admin/dashboard_controller.rb
keppler-2.1.17 installer/core/rockets/keppler_ga_dashboard/app/controllers/keppler_ga_dashboard/admin/dashboard_controller.rb
keppler-2.1.16 installer/core/rockets/keppler_ga_dashboard/app/controllers/keppler_ga_dashboard/admin/dashboard_controller.rb
keppler-2.1.15 installer/core/rockets/keppler_ga_dashboard/app/controllers/keppler_ga_dashboard/admin/dashboard_controller.rb
keppler-2.1.14 installer/core/rockets/keppler_ga_dashboard/app/controllers/keppler_ga_dashboard/admin/dashboard_controller.rb
keppler-2.1.13 installer/core/rockets/keppler_ga_dashboard/app/controllers/keppler_ga_dashboard/admin/dashboard_controller.rb