<commit_before>module Zuppler
  class User < Model
    include ActiveAttr::Model

    attribute :id
    attribute :name
    attribute :email
    attribute :phone
    attribute :roles
    attribute :access_token
    attribute :provider

    def self.find(access_token, id = nil)
      Zuppler::User.new access_token: access_token, id: id
    end

    def self.from_omniauth(omniauth)
      info = omniauth['info']
      extra = omniauth['extra']
      credentials = omniauth['credentials']
      Zuppler::User.new id: info['id'],
                        name: info['name'],
                        email: info['email'],
                        phone: info['phone'],
                        roles: info['roles'],
                        acls: info['acls'],
                        provider: extra['provider'],
                        access_token: credentials['token']
    end

    def details
      if @details.nil?
        Retriable.retriable on: Zuppler::RetryError, base_interval: 1 do
          response = execute_get user_url, {}, headers
          if v4_success? response
            @details = Hashie::Mash.new response['user']
            self.id = @details.id
          else
            if v4_response_code(response) == 401
              fail Zuppler::NotAuthorized, 'not authorized'
            elsif v4_response_code(response) > 500
              fail Zuppler::RetryError, response.message
            else
              fail Zuppler::ServerError, response.message
            end
          end
        end
      end
      @details
    end

    def role?(role)
      details.roles.include? role
    end

    def user?
      details.roles.empty?
    end

    def channel?
      role? 'channel'
    end

    def restaurant?
      role? 'restaurant'
    end

    def restaurant_staff?
      role? 'restaurant_staff'
    end

    def ambassador?
      role? 'ambassador'
    end

    def config?
      role? 'config'
    end

    def support?
      role? 'support'
    end

    def admin?
      role? 'admin'
    end

    def acls(param = nil)
      param ? details.acls[param] : details.acls
    end

    def grant(user_id, options = {})
      response = execute_update user_grant_url(user_id), { acls: options }, headers
      v4_success? response
    end

    def revoke(user_id, options = {})
      response = execute_update user_revoke_url(user_id), { acls: options }, headers
      v4_success? response
    end

    def touch(user_id)
      response = execute_update user_touch_url(user_id), {}, headers
      v4_success? response
    end

    def providers(user_id)
      if @providers.nil?
        response = execute_get user_providers_url(user_id), {}, headers
        if v4_success? response
          @providers = Hashie::Mash.new response['providers']
        elsif v4_response_code(response) == 401
          raise Zuppler::NotAuthorized, 'not authorized'
        elsif v4_response_code(response) > 500
          raise Zuppler::RetryError, response.message
        else
          raise Zuppler::ServerError, response.message
        end
      end
      @providers
    end

    private

    def headers
      { 'Authorization' => " Bearer #{access_token}" }
    end

    def user_url
      user_id = id || 'current'
      "#{Zuppler.users_api_url}/users/#{user_id}.json"
    end

    def user_grant_url(id)
      "#{Zuppler.users_api_url}/users/#{id}/grant.json"
    end

    def user_revoke_url(id)
      "#{Zuppler.users_api_url}/users/#{id}/revoke.json"<commit_msg>update user providers<commit_after>module Zuppler
  class User < Model
    include ActiveAttr::Model

    attribute :id
    attribute :name
    attribute :email
    attribute :phone
    attribute :roles
    attribute :access_token
    attribute :provider

    def self.find(access_token, id = nil)
      Zuppler::User.new access_token: access_token, id: id
    end

    def self.from_omniauth(omniauth)
      info = omniauth['info']
      extra = omniauth['extra']
      credentials = omniauth['credentials']
      Zuppler::User.new id: info['id'],
                        name: info['name'],
                        email: info['email'],
                        phone: info['phone'],
                        roles: info['roles'],
                        acls: info['acls'],
                        provider: extra['provider'],
                        access_token: credentials['token']
    end

    def details
      if @details.nil?
        Retriable.retriable on: Zuppler::RetryError, base_interval: 1 do
          response = execute_get user_url, {}, headers
          if v4_success? response
            @details = Hashie::Mash.new response['user']
            self.id = @details.id
          else
            if v4_response_code(response) == 401
              fail Zuppler::NotAuthorized, 'not authorized'
            elsif v4_response_code(response) > 500
              fail Zuppler::RetryError, response.message
            else
              fail Zuppler::ServerError, response.message
            end
          end
        end
      end
      @details
    end

    def role?(role)
      details.roles.include? role
    end

    def user?
      details.roles.empty?
    end

    def channel?
      role? 'channel'
    end

    def restaurant?
      role? 'restaurant'
    end

    def restaurant_staff?
      role? 'restaurant_staff'
    end

    def ambassador?
      role? 'ambassador'
    end

    def config?
      role? 'config'
    end

    def support?
      role? 'support'
    end

    def admin?
      role? 'admin'
    end

    def providers
      if @providers.nil?
        response = execute_get user_providers_url(id), {}, headers
        if v4_success? response
          @providers = Hashie::Mash.new response['providers']
        elsif v4_response_code(response) == 401
          raise Zuppler::NotAuthorized, 'not authorized'
        elsif v4_response_code(response) > 500
          raise Zuppler::RetryError, response.message
        else
          raise Zuppler::ServerError, response.message
        end
      end
      @providers
    end

    def acls(param = nil)
      param ? details.acls[param] : details.acls
    end

    def grant(user_id, options = {})
      response = execute_update user_grant_url(user_id), { acls: options }, headers
      v4_success? response
    end

    def revoke(user_id, options = {})
      response = execute_update user_revoke_url(user_id), { acls: options }, headers
      v4_success? response
    end

    def touch(user_id)
      response = execute_update user_touch_url(user_id), {}, headers
      v4_success? response
    end

    private

    def headers
      { 'Authorization' => " Bearer #{access_token}" }
    end

    def user_url
      user_id = id || 'current'
      "#{Zuppler.users_api_url}/users/#{user_id}.json"
    end

    def user_grant_url(id)
      "#{Zuppler.users_api_url}/users/#{id}/grant.json"
    end

    def user_revoke_url(id)
      "#{Zuppler.users_api_url}/users/#{id}/revoke.json"
{'fraction_non_alphanumeric': 0.09417169107091587, 'fraction_numerical': 0.006316393913293138, 'mean_word_length': 2.006905481225723, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '724811', 'n_tokens_mistral': 2120, 'n_tokens_neox': 2041, 'n_words': 487}