Class: Zmeygo::Client
- Inherits:
-
Object
- Object
- Zmeygo::Client
- Defined in:
- lib/zmeygo/client.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #http_connect(path) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #list ⇒ Object
- #list_cache ⇒ Object
- #list_config ⇒ Object
- #list_locales ⇒ Object
- #list_projects ⇒ Object
- #ping ⇒ Object
- #pull ⇒ Object
- #push ⇒ Object
- #translate ⇒ Object
Constructor Details
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
7 8 9 |
# File 'lib/zmeygo/client.rb', line 7 def config @config end |
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/zmeygo/client.rb', line 7 def @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 [:verbose] response = RestClient.get(url) rescue puts "Direct http request #{url} failed!" if [:verbose] if RestClient.proxy.nil? puts "Now retrying through proxy..." if [:verbose] if ENV['http_proxy'] puts "Environment variable http_proxy found." if [: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 |
#list ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/zmeygo/client.rb', line 65 def list case [: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_cache ⇒ Object
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.(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,| puts "#{key} => #{}" total += 1 end end end "==== Total #{total} keys ====" end |
#list_config ⇒ Object
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_locales ⇒ Object
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 = [: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_projects ⇒ Object
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 |
#ping ⇒ Object
53 54 55 56 57 58 |
# File 'lib/zmeygo/client.rb', line 53 def ping puts "pinging server #{config[:server]} ..." if [:verbose] http_connect('ping') do |reply| reply['message'] end end |
#pull ⇒ Object
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 = [:project],[: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 |
#push ⇒ Object
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.(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 [:verbose] response = RestClient.post(url,:data => hash) rescue puts "Direct http request #{url} failed!" if [:verbose] if RestClient.proxy.nil? puts "Now retrying through proxy..." if [:verbose] if ENV['http_proxy'] puts "Environment variable http_proxy found." if [: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 |
#translate ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/zmeygo/client.rb', line 133 def translate project,locale,key = [:project],[:locale],[: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 |