Class: ZmeygoSync::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zmeygo_sync/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



9
10
11
12
# File 'lib/zmeygo_sync/client.rb', line 9

def  initialize(options={})
   self.options = options
   self.config = ZmeygoSync.config
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/zmeygo_sync/client.rb', line 7

def config
  @config
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/zmeygo_sync/client.rb', line 7

def options
  @options
end

Instance Method Details

#http_connect(path) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/zmeygo_sync/client.rb', line 162

def http_connect(path)
  url = "#{config['server'] || 'http://zmeygo.com'}/api/#{path}?api_token=#{config['api_token']}"
  begin
    puts "GET #{url}" if options[:verbose]
    response = RestClient.get(url)
  rescue
    puts "Direct http request #{url} failed!" if options[:verbose]
    if RestClient.proxy.nil?
      puts "Now retrying through proxy..." if options[:verbose]
      if ENV['http_proxy']
        puts "Environment variable http_proxy found." if options[:verbose]
        RestClient.proxy = ENV['http_proxy']
        retry
      end
    end
    puts "http request #{url} failed!"
    exit
  end
  if response.code == 200
    reply = JSON.parse(response.body)
    yield(reply)
  elsif response.code == 401
    puts "Invalid api_token given."
  elsif response.code == 500
    puts "Ooops! Internal server error."
  end
end

#listObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/zmeygo_sync/client.rb', line 62

def list
  case options[:list]
  when "projects"
    return list_projects
  when "locales"
    return list_locales
  when "config"
    return list_config
  when 'cache'
    return list_cache
  else
    return "list what? projects or locales or cache?"
  end
end

#list_cacheObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/zmeygo_sync/client.rb', line 77

def list_cache
  hash = {}
  total = 0
  file = "#{Rails.root}/tmp/ZmeygoSync_sync_cache"
  if File.zero?(file)
    puts "No cache" if options[:verbose]
    return
  end
  File.open(file,'r') do |f|
    begin
      data = f.read
      hash = Marshal.load(data)
    rescue  => e
      puts e
      puts "No cache"
      return
    end
  end
  if hash.empty?
    puts "No cache"
  else
    hash.each_pair do |proj,v|
      puts "===== #{proj} ====="
      v.each_pair do |key,options|
          puts "#{key} => #{options}"
          total += 1
      end
    end
  end
  "==== Total #{total} keys ===="
end

#list_configObject



57
58
59
60
# File 'lib/zmeygo_sync/client.rb', line 57

def list_config
  puts "Server: #{config[:server]}"
  puts "Default project: #{config[:default_project]}"
end

#list_localesObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/zmeygo_sync/client.rb', line 119

def list_locales
  project = config['project']

  if project.nil?
    return "Please provide project name for which I should list locales. Use -p option."
  end
  http_connect("available_locales/#{project}") do |reply|
    if reply['message'] == 'OK'
      return reply['data']
    else
      puts "#{reply['message']}"
      return reply['message']
    end
  end
end

#list_projectsObject



109
110
111
112
113
114
115
116
117
# File 'lib/zmeygo_sync/client.rb', line 109

def list_projects
  http_connect('available_projects') do |reply|
    if reply['message'] == 'OK'
      return reply['data']
    else
      return reply['message']
    end
  end
end

#pingObject



50
51
52
53
54
55
# File 'lib/zmeygo_sync/client.rb', line 50

def ping
  puts "pinging server #{config[:server]} ..." if options[:verbose]
  http_connect('ping') do |reply|
    reply['message']
  end
end

#pullObject



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/zmeygo_sync/client.rb', line 148

def pull
  project = config['project']
  if project.nil?
    puts "Please provide project name from where to pull i18n keys from. Either use -p option or specify default project in config file."
    return
  end
  # pull all available locales
  locales = list_locales
  # for each available locale requst i18n data
  locales.split(',').each do |loc|
    pull_translations(project,loc)
  end
end

#pushObject



14
15
16
17
18
19
20
21
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
# File 'lib/zmeygo_sync/client.rb', line 14

def push
  if ZmeygoSync.cache.empty?
    puts "No cache" if options[:verbose]
    return
  end
  url = "http://zmeygo.com/api/pushmissing?api_token=#{config['api_token']}"
  keys = []
  hash = ZmeygoSync.load_cache
  puts "cache=#{hash}" if options[:verbose]
  begin
    puts "POST #{url}" if options[:verbose]
    response = RestClient.post(url,:data => hash)
    ZmeygoSync.clear_cache
  rescue
    puts "Direct http request #{url} failed!" if options[:verbose]
    if RestClient.proxy.nil?
      puts "Now retrying through proxy..." if options[:verbose]
      if ENV['http_proxy']
        puts "Environment variable http_proxy found." if options[:verbose]
        RestClient.proxy = ENV['http_proxy']
        retry
      end
    end
    puts "http request #{url} failed!"
    exit
  end
  if response.code == 200
    #reply = JSON.parse(response.body)
    #do nothing on success
  elsif response.code == 401
    puts "Invalid api_token given."
  elsif response.code == 500
    puts "Ooops! Internal server error."
  end
end

#translateObject



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/zmeygo_sync/client.rb', line 135

def translate
  project,locale,key = options[:project],options[:locale],options[:key]
  project ||= config.default_project

  http_connect("tr/#{project}/#{locale}/#{key}") do |reply|
    if reply['message'] == 'OK'
      return reply['data']
    else
      return reply['message']
    end
  end
end