<commit_before># Copyright © 2016-2017 Exosite LLC. All Rights Reserved
# License: PROPRIETARY. See LICENSE.txt.
# frozen_string_literal: true

# vim:tw=0:ts=2:sw=2:et:ai
# Unauthorized copying of this file is strictly prohibited.

require 'abbrev'
require 'cgi'
require 'digest/sha1'
require 'json'
require 'net/http'
require 'pathname'
require 'time'
require 'uri'
require 'yaml'
require 'MrMurano/progress'
require 'MrMurano/Solution'
require 'MrMurano/SyncRoot'

module MrMurano
  ##
  # Things that servers do that is common.
  class ServiceBase < SolutionBase
    def initialize(api_id=nil)
      super
    end

    def mkalias(_remote)
      # :nocov:
      raise 'Needs to be implemented in child'
      # :nocov:
    end

    def mkname(_remote)
      # :nocov:
      raise 'Needs to be implemented in child'
      # :nocov:
    end

    def fetch(name)
      raise 'Missing name!' if name.nil?
      raise 'Empty name!' if name.empty?
      ret = get('/' + CGI.escape(name))
      unless ret.is_a?(Hash) && !ret.key?(:error)
        error "#{UNEXPECTED_TYPE_OR_ERROR_MSG}: #{ret}"
        ret = {}
      end
      if block_given?
        yield (ret[:script] || '')
      else
        ret[:script] || ''
      end
    end

    # ??? remove
    def remove(name)
      return unless remove_item_allowed(name)
      delete('/' + name)
    end

    # @param modify Bool: True if item exists already and this is changing it
    def upload(localpath, thereitem, _modify=false)
      localpath = Pathname.new(localpath) unless localpath.is_a?(Pathname)
      if localpath.exist?
        # we assume these are small enough to slurp.
        script = localpath.read
      else
        # I.e., thereitem.phantom, an "undeletable" file that does not
        # exist locally but should not be deleted from server.
        # MAYBE/2017-09-07: Honor options.ignore_errors here?
        raise "no file at #{localpath}" unless thereitem.script
        script = thereitem.script
      end

      script = config_vars_decode(script)

      localpath = Pathname.new(localpath) unless localpath.is_a?(Pathname)
      name = mkname(thereitem)
      pst = thereitem.to_h.merge(
        #solution_id: $cfg[@solntype],
        solution_id: @api_id,
        script: script,
        alias: mkalias(thereitem),
        name: name,
      )
      debug "f: #{localpath} >> #{pst.reject { |k, _| k == :script }.to_json}"
      therealias = mkalias(thereitem)
      upload_script(therealias, name, localpath, pst)
    end

    def upload_script(therealias, name, localpath, pst)<commit_msg>Remove extraneous Pathname.new (was called earlier in fcn.).<commit_after># Copyright © 2016-2017 Exosite LLC. All Rights Reserved
# License: PROPRIETARY. See LICENSE.txt.
# frozen_string_literal: true

# vim:tw=0:ts=2:sw=2:et:ai
# Unauthorized copying of this file is strictly prohibited.

require 'abbrev'
require 'cgi'
require 'digest/sha1'
require 'json'
require 'net/http'
require 'pathname'
require 'time'
require 'uri'
require 'yaml'
require 'MrMurano/progress'
require 'MrMurano/Solution'
require 'MrMurano/SyncRoot'

module MrMurano
  ##
  # Things that servers do that is common.
  class ServiceBase < SolutionBase
    def initialize(api_id=nil)
      super
    end

    def mkalias(_remote)
      # :nocov:
      raise 'Needs to be implemented in child'
      # :nocov:
    end

    def mkname(_remote)
      # :nocov:
      raise 'Needs to be implemented in child'
      # :nocov:
    end

    def fetch(name)
      raise 'Missing name!' if name.nil?
      raise 'Empty name!' if name.empty?
      ret = get('/' + CGI.escape(name))
      unless ret.is_a?(Hash) && !ret.key?(:error)
        error "#{UNEXPECTED_TYPE_OR_ERROR_MSG}: #{ret}"
        ret = {}
      end
      if block_given?
        yield (ret[:script] || '')
      else
        ret[:script] || ''
      end
    end

    # ??? remove
    def remove(name)
      return unless remove_item_allowed(name)
      delete('/' + name)
    end

    # @param modify Bool: True if item exists already and this is changing it
    def upload(localpath, thereitem, _modify=false)
      localpath = Pathname.new(localpath) unless localpath.is_a?(Pathname)
      if localpath.exist?
        # we assume these are small enough to slurp.
        script = localpath.read
      else
        # I.e., thereitem.phantom, an "undeletable" file that does not
        # exist locally but should not be deleted from server.
        # MAYBE/2017-09-07: Honor options.ignore_errors here?
        raise "no file at #{localpath}" unless thereitem.script
        script = thereitem.script
      end

      script = config_vars_decode(script)

      name = mkname(thereitem)
      pst = thereitem.to_h.merge(
        #solution_id: $cfg[@solntype],
        solution_id: @api_id,
        script: script,
        alias: mkalias(thereitem),
        name: name,
      )
      debug "f: #{localpath} >> #{pst.reject { |k, _| k == :script }.to_json}"
      therealias = mkalias(thereitem)
      upload_script(therealias, name, localpath, pst)
    end

    def upload_script(therealias, name, localpath, pst)
{'fraction_non_alphanumeric': 0.10787000783085356, 'fraction_numerical': 0.007830853563038372, 'mean_word_length': 2.6968162083936322, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '951959', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1628, 'n_words': 468}