<commit_before>module Muvandy
  class Muvandy
    
    include HTTParty
    format :xml
    
    def initialize(experiment_id, options={})
      throw Exception.new("Please configure first.") if Muvandy.api_key.blank?
      throw Exception.new("No given Experiment ID") if experiment_id.nil?
      
      @visitor_key = options[:visitor_key] if !options[:visitor_key].blank?
      @segment_name = options[:segment_by] if !options[:segment_by].blank?
      
      # For HTTParty
      self.class.base_uri Muvandy.site
      self.class.basic_auth Muvandy.api_key, ''
      
      @experiment_id = experiment_id
      
      gather_extra_params
      
      get_all_variable_variations if options[:skip_variations].blank?
    end

    def get_variation(variable_key, default="")
      (@variable_variations[variable_key].blank?) ? default : @variable_variations[variable_key].html_safe
    end

    def convert!(value=nil)
      value = 50 if value.blank?
      
      post(:convert, :value => value )
    end

    class << self
      attr_accessor :api_key, 
                    :site,
                    :visitor_key,
                    :referrer,
                    :mode,
                    :utm_term,
                    :utm_campaign,
                    :utm_source,
                    :utm_medium
      
      def convert(experiment_id, options={})
        muvandy = Muvandy.new(experiment_id, options.merge(:skip_variations => true))
        muvandy.convert!(options[:value])
      end      
    end

    private
    
      def get_all_variable_variations
        @variable_variations = {}
        begin
          xml = get(:variable_variations)      
          # Rails.logger.debug { "DEBUG ------------ #{xml.parsed_response.inspect}" }
          if xml.parsed_response && !xml.parsed_response["visitor"].blank?
            # self.id  = xml.parsed_response["visitor"]["id"].to_i if !xml.parsed_response["visitor"].blank? && !xml.parsed_response["visitor"]["id"].blank?
            xml.parsed_response["visitor"]["variable_variations"]["variable"].each do |v|
              @variable_variations[v["key"]] =  v["value"]
            end
          end
        rescue
        end
      end

      def api_prefix
        "/api/v#{Versionomy.parse(VERSION).major}"
      end

      def get(action, add_to_query={})
        query_string = case action
        when :variable_variations
          "/experiments/#{@experiment_id}/visitors/variable_variations.xml"
        end
        begin
          Rails.logger.debug { "Mubandy DEBUG: #{extra_params_to_query(add_to_query)}" }
          xml = self.class.get("#{api_prefix}#{query_string}#{extra_params_to_query(add_to_query)}")
          raise Exception.new(xml.parsed_response["error"]["message"]) if xml.response.code != '200'<commit_msg>Hotfix for XML response bug.<commit_after>module Muvandy
  class Muvandy
    
    include HTTParty
    format :xml
    
    def initialize(experiment_id, options={})
      throw Exception.new("Please configure first.") if Muvandy.api_key.blank?
      throw Exception.new("No given Experiment ID") if experiment_id.nil?
      
      @visitor_key = options[:visitor_key] if !options[:visitor_key].blank?
      @segment_name = options[:segment_by] if !options[:segment_by].blank?
      
      # For HTTParty
      self.class.base_uri Muvandy.site
      self.class.basic_auth Muvandy.api_key, ''
      
      @experiment_id = experiment_id
      
      gather_extra_params
      
      get_all_variable_variations if options[:skip_variations].blank?
    end

    def get_variation(variable_key, default="")
      (@variable_variations[variable_key].blank?) ? default : @variable_variations[variable_key].html_safe
    end

    def convert!(value=nil)
      value = 50 if value.blank?
      
      post(:convert, :value => value )
    end

    class << self
      attr_accessor :api_key, 
                    :site,
                    :visitor_key,
                    :referrer,
                    :mode,
                    :utm_term,
                    :utm_campaign,
                    :utm_source,
                    :utm_medium
      
      def convert(experiment_id, options={})
        muvandy = Muvandy.new(experiment_id, options.merge(:skip_variations => true))
        muvandy.convert!(options[:value])
      end      
    end

    private
    
      def get_all_variable_variations
        @variable_variations = {}
        begin
          xml = get(:variable_variations)      
          # Rails.logger.debug { "DEBUG ------------ #{xml.parsed_response.inspect}" }
          if xml.parsed_response && !xml.parsed_response["visitor"].blank?
            # self.id  = xml.parsed_response["visitor"]["id"].to_i if !xml.parsed_response["visitor"].blank? && !xml.parsed_response["visitor"]["id"].blank?
            @variable_variations[xml.parsed_response["visitor"]["variable_variations"]["variable"]["key"]] =  xml.parsed_response["visitor"]["variable_variations"]["variable"]["value"]
            # xml.parsed_response["visitor"]["variable_variations"]["variable"].each do |v|
            #   @variable_variations[v["key"]] =  v["value"]
            # end
          end
        rescue
        end
      end

      def api_prefix
        "/api/v#{Versionomy.parse(VERSION).major}"
      end

      def get(action, add_to_query={})
        query_string = case action
        when :variable_variations
          "/experiments/#{@experiment_id}/visitors/variable_variations.xml"
        end
        begin
          Rails.logger.debug { "Mubandy DEBUG: #{extra_params_to_query(add_to_query)}" }
          xml = self.class.get("#{api_prefix}#{query_string}#{extra_params_to_query(add_to_query)}")
          raise Exception.new(xml.parsed_response["error"]["message"]) if xml.response.code != '200'
{'fraction_non_alphanumeric': 0.1194338971349672, 'fraction_numerical': 0.0017259233690024164, 'mean_word_length': 2.287010777084515, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 28}, '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': '5411395', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1742, 'n_words': 281}