<commit_before>module CoachClient
  class Entry < Resource
    attr_reader :id, :datecreated, :datemodified
    attr_accessor :publicvisible, :subscription, :comment,
      :entrydate, :entryduration, :entrylocation

    def self.extract_id_from_uri(uri)
      match = uri.match(/\/(\d+)\/\z/)
      match.captures.first
    end

    def initialize(client, subscription, info = {})
      super(client)
      @subscription = subscription
      @id = info[:id]
      @publicvisible = info[:publicvisible]
      @comment = info[:comment]
      @entrydate = info[:entrydate]
      @entryduration = info[:entryduration]
      @entrylocation = info[:entrylocation]
    end

    def update
      raise CoachClient::NotFound, 'Entry not found' unless exist?
      response = if @client.authenticated?(user.username, user.password)
                   CoachClient::Request.get(url, username: user.username,
                                            password: <PASSWORD>)
                 else
                   CoachClient::Request.get(url)
                 end
      tag = "entry#{@subscription.sport}"
      response = response.to_h[tag.to_sym]
      @datecreated = response[:datecreated]
      @datemodified = response[:datemodified]
      @publicvisible = response[:publicvisible]
      self
    end

    def create
      unless @client.authenticated?(user.username, user.password)
        raise CoachClient::Unauthorized.new(user), 'Unauthorized'
      end
      begin
        response = CoachClient::Request.post(@subscription.url,
                                             username: user.username,
                                             password: <PASSWORD>,
                                             payload: payload,
                                             content_type: :xml)
      rescue RestClient::Conflict
        raise CoachClient::IncompleteInformation.new(self), 'Incomplete information'
      end
      unless response.code == 200 || response.code == 201
        raise CoachClient::NotSaved.new(self), 'Could not create entry'
      end
      @id = self.class.extract_id_from_uri(response.header[:location])
      self
    end

    def save
      return create unless @id
      unless @client.authenticated?(user.username, user.password)
        raise CoachClient::Unauthorized.new(user), 'Unauthorized'
      end
      begin
        response = CoachClient::Request.put(url, username: user.username,
                                            password: <PASSWORD>,
                                            payload: payload,
                                            content_type: :xml)
      rescue RestClient::Conflict
        raise CoachClient::IncompleteInformation.new(self), 'Incomplete information'
      end
      unless response.code == 200 || response.code == 201
        raise CoachClient::NotSaved.new(self), 'Could not save entry'
      end
      self
    end

    def user
      if @subscription.is_a?(CoachClient::PartnershipSubscription)
        partnership = @subscription.partnership
        if @client.authenticated?(partnership.user1.username, partnership.user1.password)
          partnership.user1
        else
          partnership.user2
        end
      else
        @subscription.user
      end
    end

    def delete
      unless @client.authenticated?(user.username, user.password)
        raise CoachClient::Unauthorized.new(user), 'Unauthorized'
      end
      CoachClient::Request.delete(url, username: user.username,
                                  password: <PASSWORD>)
      true
    end

    def exist?(username: nil, password: nil)
      return false unless @id
      if @client.authenticated?(user.username, user.password)
        super(username: user.username, password: <PASSWORD>)
      else
        super
      end
    end

    def url
      "#{@subscription.url}/#{@id}"
    end

    def to_s
      @id.to_s<commit_msg>fix update and delete of entry<commit_after>module CoachClient
  class Entry < Resource
    attr_reader :id, :datecreated, :datemodified
    attr_accessor :publicvisible, :subscription, :comment,
      :entrydate, :entryduration, :entrylocation

    def self.extract_id_from_uri(uri)
      match = uri.match(/\/(\d+)\/\z/)
      match.captures.first
    end

    def initialize(client, subscription, info = {})
      super(client)
      @subscription = subscription
      @id = info[:id]
      @publicvisible = info[:publicvisible]
      @comment = info[:comment]
      @entrydate = info[:entrydate]
      @entryduration = info[:entryduration]
      @entrylocation = info[:entrylocation]
    end

    def update
      raise CoachClient::NotFound, 'Entry not found' unless exist?
      response = if @client.authenticated?(user.username, user.password)
                   CoachClient::Request.get(url, username: user.username,
                                            password: user.password)
                 else
                   CoachClient::Request.get(url)
                 end
      tag = "entry#{@subscription.sport}"
      response = response.to_h[tag.to_sym]
      @datecreated = response[:datecreated]
      @datemodified = response[:datemodified]
      @publicvisible = response[:publicvisible]
      @comment = response[:comment]
      @entrydate = response[:entrydate]
      @entryduration = response[:entryduration]
      @entrylocation = response[:entrylocation]
      self
    end

    def create
      unless @client.authenticated?(user.username, user.password)
        raise CoachClient::Unauthorized.new(user), 'Unauthorized'
      end
      begin
        response = CoachClient::Request.post(@subscription.url,
                                             username: user.username,
                                             password: <PASSWORD>,
                                             payload: payload,
                                             content_type: :xml)
      rescue RestClient::Conflict
        raise CoachClient::IncompleteInformation.new(self), 'Incomplete information'
      end
      unless response.code == 200 || response.code == 201
        raise CoachClient::NotSaved.new(self), 'Could not create entry'
      end
      @id = self.class.extract_id_from_uri(response.header[:location])
      self
    end

    def save
      return create unless @id
      unless @client.authenticated?(user.username, user.password)
        raise CoachClient::Unauthorized.new(user), 'Unauthorized'
      end
      begin
        response = CoachClient::Request.put(url, username: user.username,
                                            password: <PASSWORD>,
                                            payload: payload,
                                            content_type: :xml)
      rescue RestClient::Conflict
        raise CoachClient::IncompleteInformation.new(self), 'Incomplete information'
      end
      unless response.code == 200 || response.code == 201
        raise CoachClient::NotSaved.new(self), 'Could not save entry'
      end
      self
    end

    def user
      if @subscription.is_a?(CoachClient::PartnershipSubscription)
        partnership = @subscription.partnership
        if @client.authenticated?(partnership.user1.username, partnership.user1.password)
          partnership.user1
        else
          partnership.user2
        end
      else
        @subscription.user
      end
    end

    def delete
      raise CoachClient::NotFound.new(self), 'Entry not found' unless exist?
      unless @client.authenticated?(user.username, user.password)
        raise CoachClient::Unauthorized.new(user), 'Unauthorized'
      end
      CoachClient::Request.delete(url, username: user.username,
                                  password: <PASSWORD>)
      true
    end

    def exist?(username: nil, password: <PASSWORD>)
      return false unless @id
      if @client.authenticated?(user.username, user.password)
        super(username: user.username, password: <PASSWORD>)
      else
        super
      end
    end

    def url
      "#{@subscription.url}/#{@id}"
    end

    def to_s
      @id.to_s
{'fraction_non_alphanumeric': 0.09365708622398414, 'fraction_numerical': 0.003964321110009911, 'mean_word_length': 1.9866814650388458, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '890933', 'n_tokens_mistral': 2038, 'n_tokens_neox': 2027, 'n_words': 465}