<commit_before>module MotionPhrase
  class ApiClient
    API_CLIENT_IDENTIFIER = "motion_phrase"
    API_BASE_URI = "https://phraseapp.com/api/v1/"

    def self.sharedClient
      Dispatch.once { @instance ||= new }
      @instance
    end

    def storeTranslation(keyName, content, fallbackContent, currentLocale)
      return unless auth_token_present?

      content ||= fallbackContent
      data = {
        locale: currentLocale,
        key: keyName,
        content: content,
        allow_update: false,
        skip_verification: true,
        api_client: API_CLIENT_IDENTIFIER,
      }

      client.POST("translations/store", parameters:authenticated(data), success:lambda {|task, responseObject|
        log "Translation stored [#{data.inspect}]"
      }, failure:lambda {|task, error|
        log "Error while storing translation [#{data.inspect}]"
      })
    end

  private
    def client
      Dispatch.once do
        @client = begin
          _client = AFHTTPSessionManager.alloc.initWithBaseURL(NSURL.URLWithString(API_BASE_URI))
          _client
        end
      end
      @client
    end

    def log(msg="")
      $stdout.puts "PHRASEAPP #{msg}"
    end

    def authenticated(params={})
      params.merge(auth_token: auth_token)
    end

    def auth_token
      if defined?(PHRASE_AUTH_TOKEN)
        PHRASE_AUTH_TOKEN
      else
        nil
      end
    end

    def auth_token_present?
      !auth_token.nil? && auth_token != ""
    end

    def project_id
      if defined?(PHRASE_PROJECT_ID)
        PHRASE_PROJECT_ID<commit_msg>storeTranslation uses API v2<commit_after>module MotionPhrase
  class ApiClient
    API_CLIENT_IDENTIFIER = "PhraseApp RubyMotion " + MotionPhrase::VERSION
    API_BASE_URI = "https://api.phraseapp.com/v2/"

    def self.sharedClient
      Dispatch.once { @instance ||= new }
      @instance
    end

    def storeTranslation(keyName, content, fallbackContent, currentLocale)
      return unless auth_token_present?

      content ||= fallbackContent
      data = {
        key: keyName,
      }

      client.POST("projects/" + project_id + "/keys", parameters:data, success:lambda {|task, responseObject|
        log "Translation stored [#{data.inspect}]"
      }, failure:lambda {|task, error|
        log "Error while storing translation [#{data.inspect}]"
        log error.localizedDescription
      })
    end

  private
    def client
      Dispatch.once do
        @client = begin
          _client = AFHTTPSessionManager.alloc.initWithBaseURL(NSURL.URLWithString(API_BASE_URI)) do
            header "Authorized", "token " + auth_token
            header "User-Agent", API_CLIENT_IDENTIFIER
          end
          _client
        end
      end
      @client
    end

    def log(msg="")
      $stdout.puts "PHRASEAPP #{msg}"
    end

    def auth_token
      if defined?(PHRASE_AUTH_TOKEN)
        PHRASE_AUTH_TOKEN
      else
        nil
      end
    end

    def auth_token_present?
      !auth_token.nil? && auth_token != ""
    end

    def project_id
      if defined?(PHRASE_PROJECT_ID)
        PHRASE_PROJECT_ID
{'fraction_non_alphanumeric': 0.08852988691437803, 'fraction_numerical': 0.0009693053311793214, 'mean_word_length': 2.405940594059406, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '4808579', 'n_tokens_mistral': 953, 'n_tokens_neox': 932, 'n_words': 204}