Class: SmartlingXcode::API

Inherits:
Object
  • Object
show all
Defined in:
lib/smartling_xcode/backend.rb

Constant Summary collapse

CREDFILE =
'./.smartling.json'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ API

Returns a new instance of API.



10
11
12
# File 'lib/smartling_xcode/backend.rb', line 10

def initialize(args = {})
  @sandbox = args[:sandbox]
end

Instance Attribute Details

#projectIdObject

Returns the value of attribute projectId.



8
9
10
# File 'lib/smartling_xcode/backend.rb', line 8

def projectId
  @projectId
end

#sandboxObject

Returns the value of attribute sandbox.



8
9
10
# File 'lib/smartling_xcode/backend.rb', line 8

def sandbox
  @sandbox
end

#userIdObject

Returns the value of attribute userId.



8
9
10
# File 'lib/smartling_xcode/backend.rb', line 8

def userId
  @userId
end

#userSecretObject

Returns the value of attribute userSecret.



8
9
10
# File 'lib/smartling_xcode/backend.rb', line 8

def userSecret
  @userSecret
end

Instance Method Details

#fileApiObject



14
15
16
17
18
19
20
# 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

#loadCredentialsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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

#pushStringsdictFile(f, version) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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

#pushStringsFile(f, version) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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

#requestCredentialsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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