class SmartlingXcode::API

Constants

CREDFILE

Attributes

projectId[RW]
sandbox[RW]
userId[RW]
userSecret[RW]

Public Class Methods

new(args = {}) click to toggle source
# File lib/smartling_xcode/backend.rb, line 10
def initialize(args = {})
  @sandbox = args[:sandbox]
end

Public Instance Methods

fileApi() click to toggle source
# File lib/smartling_xcode/backend.rb, line 14
def fileApi
    if @sandbox
        return Smartling::File.sandbox(:userId => @userId, :userSecret => @userSecret, :projectId => @projectId)
    else
        return Smartling::File.new(:userId => @userId, :userSecret => @userSecret, :projectId => @projectId)
    end
end
loadCredentials() click to toggle source
# File lib/smartling_xcode/backend.rb, line 60
def loadCredentials
    if File.exist?(CREDFILE)
        file = File.read(CREDFILE)
        creds = JSON.parse(file)

        @userId = creds['userId']
        @userSecret = creds['userSecret']
        @projectId = creds['projectId']

        if @userId.nil? || @userSecret.nil? || @projectId.nil?
            Kernel.abort("⚠️  Invalid credentials") 
        end
    else
        Kernel.abort("⚠️  Credentials not found. Please run smartling_xcode init.")
    end
end
pushStringsFile(f, version) click to toggle source
# File lib/smartling_xcode/backend.rb, line 94
def pushStringsFile(f, version) 
    sl = fileApi()

    # Check if file contains at least one string
    file = File.open(f)
    line = file.read.scrub.tr("\000", '').gsub(/\s+/, "")
    if !line.include?('=')
        puts "No strings in #{f}"
        return
    end

    remote_path = "/#{version}/#{f.to_s.split('/').last}"

    # Upload
    begin
        res = sl.upload(f.to_s, remote_path, "ios", {:authorize => true})
        if res 
            puts "Uploaded #{res['stringCount']} strings from #{f}"
        end
    rescue Exception => e
        puts "⚠️  Upload failed for #{f}"
        if e.message
            puts e.message
        end
    end
end
pushStringsFromFiles(files, version) click to toggle source
# File lib/smartling_xcode/backend.rb, line 77
def pushStringsFromFiles(files, version)
    files.each do |f|

        # Check if file exists
        if !f || !File.exist?(f)
            puts "File not found #{f}"
            next
        end

        if f.to_s.end_with?(".strings")
            pushStringsFile(f, version)
        elsif 
            pushStringsdictFile(f, version)
        end
    end
end
pushStringsdictFile(f, version) click to toggle source
# File lib/smartling_xcode/backend.rb, line 121
def pushStringsdictFile(f, version) 
    sl = fileApi()
    remote_path = "/#{version}/#{f.to_s.split('/').last}"

    # Upload
    begin
        res = sl.upload(f.to_s, remote_path, "stringsdict", {:authorize => true})
        if res 
            puts "Uploaded #{f}"
        end
    rescue Exception => e
        puts "⚠️  Upload failed for #{f}"
        if e.message
            puts e.message
        end
    end
end
requestCredentials() click to toggle source
# File lib/smartling_xcode/backend.rb, line 22
def requestCredentials
    # Check for existing credentials
    if File.exist?(CREDFILE)
        puts "Credentials found in this directory. Overwrite? (y/N)"
        confirm = STDIN.gets.chomp
        case confirm
        when 'y', 'Y'
            # Continue
        else
            Kernel.abort("⚠️  Init cancelled")
        end
    end

    # Prompt for credentials
    puts "Smartling User ID:"
    @userId = STDIN.gets.chomp

    puts "Smartling User Secret:"
    @userSecret = STDIN.gets.chomp

    puts "Smartling Project ID:"
    @projectId = STDIN.gets.chomp

    # Dummy request to validate creds
    puts "Validating credentials..."
    begin
        sl = fileApi()
        res = sl.list
    rescue
        Kernel.abort("⚠️  Invalid credentials")
    end

    File.open(CREDFILE, "w") do |f|
        f.write({:userId => @userId, :userSecret => @userSecret, :projectId => @projectId}.to_json)
        puts "Credentials saved"
    end
end