Skip to content

Commit

Permalink
update Hyrax::Analytics::Google#valid? to make sure we have the corre…
Browse files Browse the repository at this point in the history
…ct analytics values and not just the keys
  • Loading branch information
alishaevn committed May 18, 2023
1 parent d22e784 commit 1666cbd
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions app/services/hyrax/analytics/google.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require 'oauth2'
require 'signet/oauth_2/client'

Expand All @@ -10,7 +11,7 @@ module Google
# rubocop:disable Metrics/BlockLength
class_methods do
# Loads configuration options from config/analytics.yml. You only need PRIVATE_KEY_PATH or
# PRIVA_KEY_VALUE. VALUE htakes precedence.
# PRIVATE_KEY_VALUE. VALUE takes precedence.
# Expected structure:
# `analytics:`
# ` google:`
Expand Down Expand Up @@ -53,14 +54,16 @@ def initialize(config)

# @return [Boolean] are all the required values present?
def valid?
config_keys = @config.keys
return false unless config_keys.include?('privkey_value') || config_keys.include?('privkey_path')
REQUIRED_KEYS.all? { |required| config_keys.include?(required) }
return false unless @config['privkey_value'].present? || @config['privkey_path'].present?

REQUIRED_KEYS.all? { |required| @config[required].present? }
end

KEYS.each do |key|
class_eval %{ def #{key}; @config.fetch('#{key}'); end }
class_eval %{ def #{key}=(value); @config['#{key}'] = value; end }
# rubocop:disable Style/EvalWithLocation
class_eval %{ def #{key}; @config.fetch('#{key}'); end }
class_eval %{ def #{key}=(value); @config['#{key}'] = value; end }
# rubocop:enable Style/EvalWithLocation
end
end

Expand All @@ -80,6 +83,7 @@ def auth_client(scope)
private_key = Base64.decode64(config.privkey_value) if config.privkey_value.present?
if private_key.blank?
raise "Private key file for Google analytics was expected at '#{config.privkey_path}', but no file was found." unless File.exist?(config.privkey_path)

private_key = File.read(config.privkey_path)
end
Signet::OAuth2::Client.new token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
Expand Down Expand Up @@ -126,7 +130,7 @@ def to_date_range(period)

[start_date, end_date]
end
# rubocop:enabl e Metrics/MethodLength
# rubocop:enable Metrics/MethodLength

def keyword_conversion(date)
case date
Expand Down

0 comments on commit 1666cbd

Please sign in to comment.