<commit_before>require 'brew_sparkling/gateway/xcode'
require 'digest/sha1'

module BrewSparkling
  class User
    def account
      @account ||= find_or_first(accounts) { |account|
        account.username == env(:username)
      }
    end

    def certificate
      @certificate ||= find_or_first(certificates) { |certificate|
        certificate.commonName == env(:certificate)
      }
    end

    def device
      @device ||= find_or_first(devices) { |device|
        device.name == env(:device)
      }
    end

    def gateway
      @gateway ||= Gateway::Xcode.default
    end

    def postfix
      Digest::SHA1.hexdigest account.username
    end

    private

    def accounts
      @accounts ||= gateway.accounts
    end

    def certificates
      @certificates ||= gateway.certificates
    end

    def devices
      @devices ||= gateway.devices
    end

    def env(name)
      ENV["BREW_SPARKLING_#{name.to_s.upcase}"]
    end

    def find_or_first(xs, &f)
      first = proc { xs.first }
      xs.find(first, &f)
    end
  end
end<commit_msg>:speaker: show error message when cannot discover Xcode<commit_after>require 'brew_sparkling/logger'
require 'brew_sparkling/gateway/xcode'
require 'digest/sha1'

module BrewSparkling
  class User
    def account
      @account ||= find_or_first(accounts) { |account|
        account.username == env(:username)
      }
    end

    def certificate
      @certificate ||= find_or_first(certificates) { |certificate|
        certificate.commonName == env(:certificate)
      }
    end

    def device
      @device ||= find_or_first(devices) { |device|
        device.name == env(:device)
      }
    end

    def gateway
      @gateway ||= Gateway::Xcode.default || error(<<-END)
cannot discover Xcode. Please invoke Xcode, or install SparklingHelper properly.
      END
    end

    def postfix
      Digest::SHA1.hexdigest account.username
    end

    private

    def error(message)
      logger.error message
      exit 1
    end

    def accounts
      @accounts ||= gateway.accounts
    end

    def certificates
      @certificates ||= gateway.certificates
    end

    def devices
      @devices ||= gateway.devices
    end

    def env(name)
      ENV["BREW_SPARKLING_#{name.to_s.upcase}"]
    end

    def find_or_first(xs, &f)
      first = proc { xs.first }
      xs.find(first, &f)
    end

    def logger
      Logger.default
    end
  end
end
{'fraction_non_alphanumeric': 0.09730848861283643, 'fraction_numerical': 0.002070393374741201, 'mean_word_length': 2.4028169014084506, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6508839', 'n_tokens_mistral': 797, 'n_tokens_neox': 748, 'n_words': 179}