Class: ZmeygoSync::Client
- Inherits:
-
Object
- Object
- ZmeygoSync::Client
- Defined in:
- lib/zmeygo_sync/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
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
9 10 11 12 |
# File 'lib/zmeygo_sync/client.rb', line 9 def initialize(={}) self. = self.config = ZmeygoSync.config end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
7 8 9 |
# File 'lib/zmeygo_sync/client.rb', line 7 def config @config end |
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/zmeygo_sync/client.rb', line 7 def @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 [: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
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 [: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
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 [: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,| puts "#{key} => #{}" total += 1 end end end "==== Total #{total} keys ====" end |
#list_config ⇒ Object
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_locales ⇒ Object
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_projects ⇒ Object
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 |
#ping ⇒ Object
50 51 52 53 54 55 |
# File 'lib/zmeygo_sync/client.rb', line 50 def ping puts "pinging server #{config[:server]} ..." if [:verbose] http_connect('ping') do |reply| reply['message'] end end |
#pull ⇒ Object
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 |
#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 |
# File 'lib/zmeygo_sync/client.rb', line 14 def push if ZmeygoSync.cache.empty? puts "No cache" if [:verbose] return end url = "http://zmeygo.com/api/pushmissing?api_token=#{config['api_token']}" keys = [] hash = ZmeygoSync.load_cache puts "cache=#{hash}" if [:verbose] begin puts "POST #{url}" if [:verbose] response = RestClient.post(url,:data => hash) ZmeygoSync.clear_cache 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) #do nothing on success elsif response.code == 401 puts "Invalid api_token given." elsif response.code == 500 puts "Ooops! Internal server error." end end |
#translate ⇒ Object
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 = [: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 |