<commit_before>require 'fitting/doc/response'

module Fitting
  class Doc
    class Action
      attr_accessor :key, :type, :host, :prefix, :path, :method, :responses, :host_cover, :prefix_cover, :path_cover, :method_cover

      def initialize(type, host, prefix, method, path, responses)
        @type = type
        @host = host
        @prefix = prefix
        @method = method
        @path = path
        @cover = 0
        @responses = Fitting::Doc::Response.all(responses)
        @key = "#{method} #{host}#{prefix}#{path}"

        @host_cover = 0
        @prefix_cover = 0
        @path_cover = 0
        @method_cover = 0
      end

      def url
        "#{@host}#{@prefix}#{@path}"
      end

      def to_hash
        {
          host => {
            prefix => {
              path => {
                method => {
                  responses[0].code => {
                    responses[0].content_type =>
                      responses[0].body
                  }
                }
              }
            }
          }
        }
      end

      def to_yaml
        YAML.dump(to_hash)
      end

      def self.provided_all(apis)
        return [] unless apis
        apis.map do |api|
          Tomograph::Tomogram.new(prefix: api['prefix'], tomogram_json_path: api['path']).to_a.map do |action|
            new(
              'provided',
              YAML.safe_load(File.read('.fitting.yml'))['Host'],
              api['prefix'],
              action.to_hash['method'],
              action.to_hash['path'].path,
              action.responses
            )
          end
        end.flatten
      end

      def self.used_all(apis)
        return [] unless apis
        apis.map do |api|
          Tomograph::Tomogram.new(prefix: '', tomogram_json_path: api['path']).to_a.map do |action|
            new(
              'used',
              api['host'],
              '',
              action.to_hash['method'],
              action.to_hash['path'].path,
              action.responses
            )
          end
        end.flatten
      end

      def cover!(log)
        unless log.host == host
          return
        end
        @cover = 25 if @cover < 25
        @host_cover += 1

        unless prefix.size == 0 || log.path[0..prefix.size - 1] == prefix
          return
        end
        @cover = 50 if @cover < 50
        @prefix_cover += 1

        unless path_match(log.path)
          return
        end
        @cover = 75 if @cover < 75
        @path_cover += 1

        unless log.method == method
          return
        end
        @cover = 100 if @cover < 100
        @method_cover += 1

        true
      end

      def cover
        @cover
      end

      def nocover!
        @cover = 100
      end

      def path_match(find_path)
        regexp =~ find_path
      end

      def regexp
        return @regexp if @regexp

        str = Regexp.escape(path)
        str = str.gsub(/\\{\w+\\}/, '[^&=\/]+')
        str = "\\A#{str}\\z"
        @regexp = Regexp.new(str)
      end
    end
  end
end
<commit_msg>fix nocov<commit_after>require 'fitting/doc/response'

module Fitting
  class Doc
    class Action
      attr_accessor :key, :type, :host, :prefix, :path, :method, :responses, :host_cover, :prefix_cover, :path_cover, :method_cover

      def initialize(type, host, prefix, method, path, responses)
        @type = type
        @host = host
        @prefix = prefix
        @method = method
        @path = path
        @cover = 0
        @responses = Fitting::Doc::Response.all(responses)
        @key = "#{method} #{host}#{prefix}#{path}"

        @host_cover = 0
        @prefix_cover = 0
        @path_cover = 0
        @method_cover = 0
      end

      def url
        "#{@host}#{@prefix}#{@path}"
      end

      def to_hash
        {
          host => {
            prefix => {
              path => {
                method => {
                  responses[0].code => {
                    responses[0].content_type =>
                      responses[0].body
                  }
                }
              }
            }
          }
        }
      end

      def to_yaml
        YAML.dump(to_hash)
      end

      def self.provided_all(apis)
        return [] unless apis
        apis.map do |api|
          Tomograph::Tomogram.new(prefix: api['prefix'], tomogram_json_path: api['path']).to_a.map do |action|
            new(
              'provided',
              YAML.safe_load(File.read('.fitting.yml'))['Host'],
              api['prefix'],
              action.to_hash['method'],
              action.to_hash['path'].path,
              action.responses
            )
          end
        end.flatten
      end

      def self.used_all(apis)
        return [] unless apis
        apis.map do |api|
          Tomograph::Tomogram.new(prefix: '', tomogram_json_path: api['path']).to_a.map do |action|
            new(
              'used',
              api['host'],
              '',
              action.to_hash['method'],
              action.to_hash['path'].path,
              action.responses
            )
          end
        end.flatten
      end

      def cover!(log)
        unless log.host == host
          return
        end
        @cover = 25 if @cover < 25
        @host_cover += 1

        unless prefix.size == 0 || log.path[0..prefix.size - 1] == prefix
          return
        end
        @cover = 50 if @cover < 50
        @prefix_cover += 1

        unless path_match(log.path)
          return
        end
        @cover = 75 if @cover < 75
        @path_cover += 1

        unless log.method == method
          return
        end
        @cover = 100 if @cover < 100
        @method_cover += 1

        true
      end

      def cover
        @cover
      end

      def nocover!
        @cover = 100
        @host_cover = nil
        @prefix_cover = nil
        @path_cover = nil
        @method_cover = nil
      end

      def path_match(find_path)
        regexp =~ find_path
      end

      def regexp
        return @regexp if @regexp

        str = Regexp.escape(path)
        str = str.gsub(/\\{\w+\\}/, '[^&=\/]+')
        str = "\\A#{str}\\z"
        @regexp = Regexp.new(str)
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.11689145793192036, 'fraction_numerical': 0.011560693641618497, 'mean_word_length': 1.2816849816849818, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '3363137', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1814, 'n_words': 415}