<commit_before>require 'json'
require_dependency 'carto/export/layer_exporter'
require_dependency 'carto/export/data_import_exporter'

# Version History
# TODO: documentation at http://cartodb.readthedocs.org/en/latest/operations/exporting_importing_visualizations.html
# 2: export full visualization. Limitations:
#   - No Odyssey support: export fails if any of parent_id / prev_id / next_id / slide_transition_options are set.
#   - Privacy is exported, but permissions are not.
# 2.0.1: export Widget.source_id
# 2.0.2: export username
# 2.0.3: export state (Carto::State)
# 2.0.4: export legends (Carto::Legend)
# 2.0.5: export explicit widget order
# 2.0.6: export version
# 2.0.7: export map options
# 2.0.8: export widget style
# 2.0.9: export visualization id
# 2.1.0: export datasets: permissions, user_tables and syncs
# 2.1.1: export vizjson2 mark
module Carto
  module VisualizationsExportService2Configuration
    CURRENT_VERSION = '2.1.1'.freeze

    def compatible_version?(version)
      version.to_i == CURRENT_VERSION.split('.')[0].to_i
    end
  end

  module VisualizationsExportService2Validator
    def check_valid_visualization(visualization)
      raise 'Only derived or canonical visualizations can be exported' unless visualization.derived? ||
                                                                              visualization.canonical? ||
                                                                              visualization.remote?
    end
  end

  module VisualizationsExportService2Importer
    include VisualizationsExportService2Configuration
    include LayerImporter
    include DataImportImporter

    def build_visualization_from_json_export(exported_json_string)
      build_visualization_from_hash_export(parse_json(exported_json_string))
    end

    def build_visualization_from_hash_export(exported_hash)
      raise 'Wrong export version' unless compatible_version?(exported_hash[:version])

      build_visualization_from_hash(exported_hash[:visualization])
    end

    def marked_as_vizjson2_from_json_export?(exported_json_string)
      marked_as_vizjson2_from_hash_export?(parse_json(exported_json_string))
    end

    def marked_as_vizjson2_from_hash_export?(exported_hash)
      exported_hash[:visualization][:uses_vizjson2]
    end

    private

    def parse_json(exported_json_string)
      JSON.parse(exported_json_string, symbolize_names: true)
    end

    def build_visualization_from_hash(exported_visualization)
      exported_layers = exported_visualization[:layers]
      exported_overlays = exported_visualization[:overlays]

      visualization = Carto::InMemoryVisualization.new(
        name: exported_visualization[:name],
        description: exported_visualization[:description],<commit_msg>Revert "Use InMemoryVisualizations instead of Visualizations when regenerating from mapcap"<commit_after>require 'json'
require_dependency 'carto/export/layer_exporter'
require_dependency 'carto/export/data_import_exporter'

# Version History
# TODO: documentation at http://cartodb.readthedocs.org/en/latest/operations/exporting_importing_visualizations.html
# 2: export full visualization. Limitations:
#   - No Odyssey support: export fails if any of parent_id / prev_id / next_id / slide_transition_options are set.
#   - Privacy is exported, but permissions are not.
# 2.0.1: export Widget.source_id
# 2.0.2: export username
# 2.0.3: export state (Carto::State)
# 2.0.4: export legends (Carto::Legend)
# 2.0.5: export explicit widget order
# 2.0.6: export version
# 2.0.7: export map options
# 2.0.8: export widget style
# 2.0.9: export visualization id
# 2.1.0: export datasets: permissions, user_tables and syncs
# 2.1.1: export vizjson2 mark
module Carto
  module VisualizationsExportService2Configuration
    CURRENT_VERSION = '2.1.1'.freeze

    def compatible_version?(version)
      version.to_i == CURRENT_VERSION.split('.')[0].to_i
    end
  end

  module VisualizationsExportService2Validator
    def check_valid_visualization(visualization)
      raise 'Only derived or canonical visualizations can be exported' unless visualization.derived? ||
                                                                              visualization.canonical? ||
                                                                              visualization.remote?
    end
  end

  module VisualizationsExportService2Importer
    include VisualizationsExportService2Configuration
    include LayerImporter
    include DataImportImporter

    def build_visualization_from_json_export(exported_json_string)
      build_visualization_from_hash_export(parse_json(exported_json_string))
    end

    def build_visualization_from_hash_export(exported_hash)
      raise 'Wrong export version' unless compatible_version?(exported_hash[:version])

      build_visualization_from_hash(exported_hash[:visualization])
    end

    def marked_as_vizjson2_from_json_export?(exported_json_string)
      marked_as_vizjson2_from_hash_export?(parse_json(exported_json_string))
    end

    def marked_as_vizjson2_from_hash_export?(exported_hash)
      exported_hash[:visualization][:uses_vizjson2]
    end

    private

    def parse_json(exported_json_string)
      JSON.parse(exported_json_string, symbolize_names: true)
    end

    def build_visualization_from_hash(exported_visualization)
      exported_layers = exported_visualization[:layers]
      exported_overlays = exported_visualization[:overlays]

      visualization = Carto::Visualization.new(
        name: exported_visualization[:name],
        description: exported_visualization[:description],
{'fraction_non_alphanumeric': 0.07186054784774101, 'fraction_numerical': 0.01672002845962291, 'mean_word_length': 3.7172818791946307, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '367958', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1585, 'n_words': 334}