<commit_before>module Krikri::LDP
  ##
  # Adds simple LDP persistence to ActiveTriples::Resource classes
  # @see ActiveTriples::Resource
  #
  # @see http://www.w3.org/TR/ldp/#ldprs
  module RdfSource
    extend ActiveSupport::Concern
    include Krikri::LDP::Resource

    ##
    # PUTs the LDP resource named in #rdf_subject, populating it's content
    # (graph) from the object's RDF::Graph.
    #
    # @see Krikri::LDP::Resource#save
    # @note this may leave the resource's graph out of sync with the LDP 
    #   endpoint since the endpoint may add management triples when saving.
    def save(*)
      result = super(dump(:ttl))
      result
    end

    ##
    # Saves and forces reload. This updates the graph with any management 
    # triples added by the LDP endpoint.
    #
    # @see #save
    def save_and_reload(*args)
      result = save(*args)
      get({}, true)
      result 
    end

    ##
    # GETs the LDP resource from #rdf_subject and resets this object's
    # RDF::Graph to match the one returned from the LDP server.
    #
    # @see Krikri::LDP::Resource#get
    def get(*args)
      result = super
      reload_ldp
      result
    end

    ##
    # Adds an appropritate provenance statement with the given URI and saves
    # the resource.
    #
    # This method treats RDFSources as stateful resources. This is in conflict
    # with the PROV model, which assumes each revision is its own Resource. The
    # internal predicate `dpla:wasRevisedBy` is used for non-generating
    # revisions of stateful RDFSources.
    #
    # @todo Assuming a Marmotta LDP server, there are version URIs available
    #   (via Memento) which could be used for a direct PROV implementation.
    #   Consider options for doing that either alongside or in place of this
    #   approach.
    #
    # @param activity_uri [#to_term] the URI of the prov:Activity to mark as
    #   generating or revising the saved resource.
    #
    # @see #save
    #
    # @see http://www.w3.org/TR/prov-primer/
    # @see http://www.w3.org/TR/2013/REC-prov-o-20130430/
    def save_with_provenance(activity_uri)
      predicate =
        exists? ? RDF::DPLA.wasRevisedBy : RDF::PROV.wasGeneratedBy
      self << RDF::Statement(self, predicate, activity_uri)
      save
    end

    private

    ##
    # Clears the RDF::Graph and repopulates it from the http body.
    def reload_ldp
      return reload unless !node? && exists?
      clear
      self << RDF::Reader.for(:ttl).new(@http_cache.body).statements
    end
  end
end<commit_msg>Force encoding on RDFSource response bodies<commit_after>module Krikri::LDP
  ##
  # Adds simple LDP persistence to ActiveTriples::Resource classes
  # @see ActiveTriples::Resource
  #
  # @see http://www.w3.org/TR/ldp/#ldprs
  module RdfSource
    extend ActiveSupport::Concern
    include Krikri::LDP::Resource

    ##
    # PUTs the LDP resource named in #rdf_subject, populating it's content
    # (graph) from the object's RDF::Graph.
    #
    # @see Krikri::LDP::Resource#save
    # @note this may leave the resource's graph out of sync with the LDP 
    #   endpoint since the endpoint may add management triples when saving.
    def save(*)
      result = super(dump(:ttl))
      result
    end

    ##
    # Saves and forces reload. This updates the graph with any management 
    # triples added by the LDP endpoint.
    #
    # @see #save
    def save_and_reload(*args)
      result = save(*args)
      get({}, true)
      result 
    end

    ##
    # GETs the LDP resource from #rdf_subject and resets this object's
    # RDF::Graph to match the one returned from the LDP server.
    #
    # @see Krikri::LDP::Resource#get
    def get(*args)
      result = super
      reload_ldp
      result
    end

    ##
    # Adds an appropritate provenance statement with the given URI and saves
    # the resource.
    #
    # This method treats RDFSources as stateful resources. This is in conflict
    # with the PROV model, which assumes each revision is its own Resource. The
    # internal predicate `dpla:wasRevisedBy` is used for non-generating
    # revisions of stateful RDFSources.
    #
    # @todo Assuming a Marmotta LDP server, there are version URIs available
    #   (via Memento) which could be used for a direct PROV implementation.
    #   Consider options for doing that either alongside or in place of this
    #   approach.
    #
    # @param activity_uri [#to_term] the URI of the prov:Activity to mark as
    #   generating or revising the saved resource.
    #
    # @see #save
    #
    # @see http://www.w3.org/TR/prov-primer/
    # @see http://www.w3.org/TR/2013/REC-prov-o-20130430/
    def save_with_provenance(activity_uri)
      predicate =
        exists? ? RDF::DPLA.wasRevisedBy : RDF::PROV.wasGeneratedBy
      self << RDF::Statement(self, predicate, activity_uri)
      save
    end

    private

    ##
    # Clears the RDF::Graph and repopulates it from the http body. Forces text
    # encoding to UTF-8 before passing to the `RDF::Reader`.
    # 
    # @return [void]
    #
    # @see http://www.w3.org/TR/turtle/#sec-mime for info about Turtle encoding
    # @see http://www.w3.org/TR/ldp/#h-ldprs-get-turtle for info about LDP GET
    #   and Turtle.
    def reload_ldp
      return reload unless !node? && exists?
      clear
      self << RDF::Reader.for(:ttl).new(@http_cache.body.force_encoding('UTF-8'))
    end
  end
end
{'fraction_non_alphanumeric': 0.09079165897767116, 'fraction_numerical': 0.006274220335855324, 'mean_word_length': 2.6327077747989276, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, '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': '7363541', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1687, 'n_words': 558}