Class: Zmeygo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zmeygo/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/client.rb', line 9

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

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#http_connect(path) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/zmeygo/client.rb', line 167

def http_connect(path)
  url = "#{config['server']}/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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/zmeygo/client.rb', line 65

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



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
# File 'lib/zmeygo/client.rb', line 80

def list_cache
  hash = {}
  total = 0
  File.open(File.expand_path(Constant::CACHE_FILE,'r')) do |f|
    data = f.read
    begin
      hash = Marshal.load(data)
    rescue
      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



60
61
62
63
# File 'lib/zmeygo/client.rb', line 60

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

#list_localesObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/zmeygo/client.rb', line 116

def list_locales
	project = options[:project]
	project ||= config['default_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



106
107
108
109
110
111
112
113
114
# File 'lib/zmeygo/client.rb', line 106

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

#pingObject



53
54
55
56
57
58
# File 'lib/zmeygo/client.rb', line 53

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

#pullObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/zmeygo/client.rb', line 146

def pull
	project, locale = options[:project],options[:locale]
	project ||= config['default_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
	puts "Will save locales to #{config[:locale_dir]}"
	if locale.nil?
		# pull all available locales
		locales = list_locales
		# for each available locale requst i18n data
		locales.split(',').each do |loc|
			pull_translations(project,loc)
		end
	else
		pull_translations(project,locale)
	end
	"Done."
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
49
50
51
# File 'lib/zmeygo/client.rb', line 14

def push
	url = "#{config['server']}/api/pushmissing?api_token=#{config['api_token']}"
    hash = {}
    keys = []
    File.open(File.expand_path(Constant::CACHE_FILE,'r')) do |f|
      data = f.read
      begin
        hash = Marshal.load(data)
      rescue
        puts "No cache"
        return
      end
    end
    Zmeygo.clear_cache
    begin
      puts "POST #{url}" if options[:verbose]
      response = RestClient.post(url,:data => hash)
    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)
    elsif response.code == 401
      puts "Invalid api_token given."
    elsif response.code == 500
      puts "Ooops! Internal server error."
    end
end

#translateObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/zmeygo/client.rb', line 133

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