web_translate_it 2.0.0.rc2 → 2.0.0.rc3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/wti CHANGED
@@ -6,32 +6,29 @@ require 'trollop'
6
6
  require 'web_translate_it'
7
7
 
8
8
  show_commands = <<-EOS
9
- A CLI for syncing files with WebTranslateIt.com
10
-
11
9
  Usage: wti <command> [options]+ <filename>
12
10
 
13
11
  The most commonly used wti commands are:
14
-
15
- pull Pull target language file(s)
16
- push Push master language file(s)
17
- match Display matching of local files with File Manager
18
- add Create and push a new master language file
19
- rm Delete a master language file from a project
20
- addlocale Add a new locale to the project
21
- rmlocale Delete a locale from a project
22
- status Fetch and display project statistics
23
- init Configure your project to sync
12
+ pull Pull target language file(s)
13
+ push Push master language file(s)
14
+ match Display matching of local files with File Manager
15
+ add Create and push a new master language file
16
+ rm Delete a master language file from a project
17
+ addlocale Add a new locale to the project
18
+ rmlocale Delete a locale from a project
19
+ status Fetch and display project statistics
20
+ init Configure your project to sync
24
21
 
25
22
  See `wti <command> --help` for more information on a specific command.
26
23
 
27
24
  [options] are:
28
25
  EOS
29
26
 
30
- SUB_COMMANDS = %w(pull push match add addlocale server status st init)
27
+ SUB_COMMANDS = %w(pull push match add addlocale status st init)
31
28
  global_options = Trollop::options do
32
29
  stop_on SUB_COMMANDS
33
30
  banner show_commands
34
- version "Web Translate It v#{WebTranslateIt::Util.version}"
31
+ version "wti version #{WebTranslateIt::Util.version}"
35
32
  end
36
33
 
37
34
  command = ARGV.shift # get the subcommand
@@ -84,17 +81,6 @@ EOS
84
81
  banner "Delete a locale from the project"
85
82
  opt :config, "Path to a translation.yml file", :short => "-c", :default => ".wti"
86
83
  end
87
- when "server"
88
- Trollop::options do
89
- banner <<-EOS
90
- Deprecated, used to start a synchronisation server.
91
- `gem install web_translate_it_server` and run `wti-server` instead.
92
- [options] are:
93
- EOS
94
- opt :port, "Run server on a specific port", :default => 4000, :short => "-p"
95
- opt :host, "Run server on a specific host", :default => "0.0.0.0", :short => "-h"
96
- opt :config, "Path to a translation.yml file", :short => "-c", :default => ".wti"
97
- end
98
84
  when "status"
99
85
  Trollop::options do
100
86
  banner "Fetch and display project statistics"
data/history.md CHANGED
@@ -1,4 +1,10 @@
1
- ## Edge
1
+ ## Version 2.0.0.rc3 / 2012-04-19
2
+
3
+ * Remove deprecated `wti server`.
4
+ * Reword command help and output.
5
+ * Rework String, Translation, Term and TermTranslation refactoring. Now using a connection persisted as a class instance. See connection.rb. This makes for a nicer syntax.
6
+
7
+ ## Version 2.0.0.rc2 / 2012-04-16
2
8
 
3
9
  * Backport of compatibility fix for ruby 1.8.7 from 1.x serie.
4
10
  * Fix issue with new version system, which broke the gem. #86
@@ -1,5 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'web_translate_it/connection'
3
4
  require 'web_translate_it/util'
4
5
  require 'web_translate_it/util/array_util'
5
6
  require 'web_translate_it/util/string_util'
@@ -21,7 +22,7 @@ module WebTranslateIt
21
22
  locale = I18n.locale.to_s
22
23
  return if config.ignore_locales.include?(locale)
23
24
  config.logger.debug { "➔ Fetching #{locale.upcase} language file(s) from Web Translate It…" } if config.logger
24
- WebTranslateIt::Util.http_connection do |http|
25
+ WebTranslateIt::Connection.new(config.api_key) do |http|
25
26
  config.files.find_all{ |file| file.locale == locale }.each do |file|
26
27
  response = file.fetch(http)
27
28
  config.logger.debug { "➔ Web Translate It response: #{response}" } if config.logger
@@ -13,12 +13,10 @@ module WebTranslateIt
13
13
  end
14
14
  self.send(command)
15
15
  end
16
-
16
+
17
17
  def pull
18
18
  STDOUT.sync = true
19
19
  `#{configuration.before_pull}` if configuration.before_pull
20
- puts StringUtil.titleize("Pulling files")
21
-
22
20
  # Selecting files to pull
23
21
  files = []
24
22
  fetch_locales_to_pull.each do |locale|
@@ -28,14 +26,14 @@ module WebTranslateIt
28
26
  puts "No files to pull."
29
27
  else
30
28
  # Now actually pulling files
29
+ puts "# Pulling files"
31
30
  time = Time.now
32
31
  threads = []
33
32
  n_threads = (files.count.to_f/3).ceil >= 20 ? 20 : (files.count.to_f/3).ceil
34
- puts "Using up to #{n_threads} threads"
35
33
  ArrayUtil.chunk(files, n_threads).each do |file_array|
36
34
  unless file_array.empty?
37
35
  threads << Thread.new(file_array) do |file_array|
38
- WebTranslateIt::Util.http_connection do |http|
36
+ WebTranslateIt::Connection.new(configuration.api_key) do |http|
39
37
  file_array.each do |file|
40
38
  file.fetch(http, command_options.force)
41
39
  end
@@ -45,7 +43,7 @@ module WebTranslateIt
45
43
  end
46
44
  threads.each { |thread| thread.join }
47
45
  time = Time.now - time
48
- puts "Pulled #{files.count} files at #{(files.count/time).round} files/sec."
46
+ puts "Pulled #{files.count} files at #{(files.count/time).round} files/sec, using #{n_threads} threads."
49
47
  `#{configuration.after_pull}` if configuration.after_pull
50
48
  end
51
49
  end
@@ -53,8 +51,8 @@ module WebTranslateIt
53
51
  def push
54
52
  STDOUT.sync = true
55
53
  `#{configuration.before_push}` if configuration.before_push
56
- puts StringUtil.titleize("Pushing files")
57
- WebTranslateIt::Util.http_connection do |http|
54
+ puts "# Pushing files"
55
+ WebTranslateIt::Connection.new(configuration.api_key) do |http|
58
56
  fetch_locales_to_push(configuration).each do |locale|
59
57
  configuration.files.find_all{ |file| file.locale == locale }.sort{|a,b| a.file_path <=> b.file_path} .each do |file|
60
58
  file.upload(http, command_options[:merge], command_options.ignore_missing, command_options.label, command_options.low_priority, command_options[:minor], command_options.force)
@@ -67,11 +65,11 @@ module WebTranslateIt
67
65
  def add
68
66
  STDOUT.sync = true
69
67
  if parameters == []
70
- puts StringUtil.failure("No master file given.")
71
- puts "Usage: wti add master_file_1 master_file_2 ..."
68
+ puts StringUtil.failure("Error: You must provide the path to the master file to add.")
69
+ puts "Usage: wti add path/to/master_file_1 path/to/master_file_2 ..."
72
70
  exit
73
71
  end
74
- WebTranslateIt::Util.http_connection do |http|
72
+ WebTranslateIt::Connection.new(configuration.api_key) do |http|
75
73
  added = configuration.files.find_all{ |file| file.locale == configuration.source_locale}.collect {|file| File.expand_path(file.file_path) }.to_set
76
74
  parameters.reject{ |param| added.include?(File.expand_path(param))}.each do |param|
77
75
  file = TranslationFile.new(nil, param, nil, configuration.api_key)
@@ -84,13 +82,13 @@ module WebTranslateIt
84
82
  def rm
85
83
  STDOUT.sync = true
86
84
  if parameters == []
87
- puts StringUtil.failure("No master file given.")
88
- puts "Usage: wti rm master_file_1 master_file_2 ..."
85
+ puts StringUtil.failure("Error: You must provide the path to the master file to remove.")
86
+ puts "Usage: wti path/to/rm master_file_1 path/to/master_file_2 ..."
89
87
  exit
90
88
  end
91
- WebTranslateIt::Util.http_connection do |http|
89
+ WebTranslateIt::Connection.new(configuration.api_key) do |http|
92
90
  parameters.each do |param|
93
- if Util.ask_yes_no("Are you certain you want to delete the master file #{param} and its attached target files and translations?", false)
91
+ if Util.ask_yes_no("Are you sure you want to delete the master file #{param}?\nThis will also delete its target files and translations.", false)
94
92
  configuration.files.find_all{ |file| file.file_path == param }.each do |master_file|
95
93
  master_file.delete(http)
96
94
  # delete files
@@ -108,31 +106,35 @@ module WebTranslateIt
108
106
  def addlocale
109
107
  STDOUT.sync = true
110
108
  if parameters == []
111
- puts StringUtil.failure("No locale code given.")
112
- puts "Usage: wti addlocale locale_code_1 locale_code_2 ..."
109
+ puts StringUtil.failure("Locale code missing.")
110
+ puts "Usage: wti addlocale fr es ..."
113
111
  exit
114
112
  end
115
113
  parameters.each do |param|
116
- print StringUtil.success("Adding locale #{param}... ")
117
- puts WebTranslateIt::Project.create_locale(configuration.api_key, param)
114
+ print StringUtil.success("Adding locale #{param.upcase}... ")
115
+ WebTranslateIt::Connection.new(configuration.api_key) do
116
+ puts WebTranslateIt::Project.create_locale(param)
117
+ end
118
+ puts "Done."
118
119
  end
119
- puts "Done!"
120
120
  end
121
121
 
122
122
  def rmlocale
123
123
  STDOUT.sync = true
124
124
  if parameters == []
125
- puts StringUtil.failure("No locale code given.")
126
- puts "Usage: wti rmlocale locale_code_1 locale_code_2 ..."
125
+ puts StringUtil.failure("Error: You must provide the locale code to remove.")
126
+ puts "Usage: wti rmlocale fr es ..."
127
127
  exit
128
128
  end
129
129
  parameters.each do |param|
130
- if Util.ask_yes_no("Are you certain you want to delete the locale #{param} and its attached target files and translations?", false)
131
- print StringUtil.success("Deleting locale #{param}... ")
132
- puts WebTranslateIt::Project.delete_locale(configuration.api_key, param)
130
+ if Util.ask_yes_no("Are you certain you want to delete the locale #{param.upcase}?\nThis will also delete its files and translations.", false)
131
+ print StringUtil.success("Deleting locale #{param.upcase}... ")
132
+ WebTranslateIt::Connection.new(configuration.api_key) do |http|
133
+ puts WebTranslateIt::Project.delete_locale(param)
134
+ end
135
+ puts "Done."
133
136
  end
134
137
  end
135
- puts "Done!"
136
138
  end
137
139
 
138
140
  def init
@@ -147,12 +149,11 @@ module WebTranslateIt
147
149
  end
148
150
  File.open(path, 'w'){ |file| file << generate_configuration(api_key, project_info) }
149
151
  puts ""
150
- puts "Done! You can now use `wti` to push and pull your language files."
152
+ puts "Your project was successfully setup. You can now use `wti` to push and pull your language files."
151
153
  puts "Check `wti --help` for help."
152
154
  end
153
155
 
154
156
  def match
155
- puts StringUtil.titleize("Matching local files with File Manager")
156
157
  configuration.files.find_all{ |mf| mf.locale == configuration.source_locale }.each do |master_file|
157
158
  if !File.exists?(master_file.file_path)
158
159
  puts StringUtil.failure(master_file.file_path) + " (#{master_file.locale})"
@@ -179,21 +180,10 @@ module WebTranslateIt
179
180
  stale = true if values['stale']
180
181
  end
181
182
  if stale
182
- self.status if Util.ask_yes_no("Some of these stats are out of date. Would you like to refresh?", true)
183
+ self.status if Util.ask_yes_no("Some of these stats are outdated. Would you like to refresh?", true)
183
184
  end
184
185
  end
185
-
186
- def server
187
- puts "This feature is deprecated and was extracted to a separate gem,"
188
- puts " `web_translate_it_server`."
189
- puts "To use it, run `gem install web_translate_it_server`"
190
- puts "and run the server with `wti-server`."
191
- end
192
-
193
- def method_missing(m, *args, &block)
194
- puts "wti: '#{m}' is not a wti command. See 'wti --help'."
195
- end
196
-
186
+
197
187
  def fetch_locales_to_pull
198
188
  if command_options.locale
199
189
  locales = command_options.locale.split
@@ -10,7 +10,8 @@ module WebTranslateIt
10
10
  class Configuration
11
11
  require 'yaml'
12
12
  require 'fileutils'
13
- attr_accessor :path, :api_key, :source_locale, :target_locales, :files, :ignore_locales, :logger, :before_pull, :after_pull, :before_push, :after_push
13
+ attr_accessor :path, :api_key, :source_locale, :target_locales, :files, :ignore_locales
14
+ attr_accessor :logger, :before_pull, :after_pull, :before_push, :after_push
14
15
 
15
16
  # Load configuration file from the path.
16
17
  def initialize(root_path = Rails.root, path_to_config_file = ".wti")
@@ -0,0 +1,62 @@
1
+ # encoding: utf-8
2
+ module WebTranslateIt
3
+ class Connection
4
+ require 'net/http'
5
+ require 'net/https'
6
+ require 'uri'
7
+ require 'ostruct'
8
+
9
+ @@api_key = nil
10
+ @@http_connection = nil
11
+
12
+ #
13
+ # Initialize and yield a HTTPS Keep-Alive connection to WebTranslateIt.com
14
+ #
15
+ # Usage:
16
+ #
17
+ # WebTranslateIt::Connection.new(api_key) do
18
+ # # do something with Connection.api_key and Connection.http_connection
19
+ # end
20
+ #
21
+ # Or:
22
+ #
23
+ # WebTranslateIt::Connection.new(api_key) do |http_connection|
24
+ # http_connection.request(request)
25
+ # end
26
+ #
27
+ def initialize(api_key)
28
+ @@api_key = api_key
29
+ proxy = ENV['http_proxy'] ? URI.parse(ENV['http_proxy']) : OpenStruct.new
30
+ http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new('webtranslateit.com', 443)
31
+ http.use_ssl = true
32
+ http.open_timeout = http.read_timeout = 30
33
+ begin
34
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
35
+ if File.exists?('/etc/ssl/certs') # Ubuntu
36
+ http.ca_path = '/etc/ssl/certs'
37
+ else
38
+ http.ca_file = File.expand_path('cacert.pem', __FILE__)
39
+ end
40
+ @@http_connection = http.start
41
+ yield @@http_connection if block_given?
42
+ rescue OpenSSL::SSL::SSLError
43
+ puts "Unable to verify SSL certificate."
44
+ http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new('webtranslateit.com', 443)
45
+ http.use_ssl = true
46
+ http.open_timeout = http.read_timeout = 30
47
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
48
+ @@http_connection = http.start
49
+ yield @@http_connection if block_given?
50
+ end
51
+ end
52
+
53
+ def self.http_connection
54
+ @@http_connection
55
+ end
56
+
57
+ def self.api_key
58
+ @@api_key
59
+ end
60
+ end
61
+ end
62
+
@@ -4,7 +4,7 @@ module WebTranslateIt
4
4
 
5
5
  def self.fetch_info(api_key)
6
6
  begin
7
- WebTranslateIt::Util.http_connection do |http|
7
+ WebTranslateIt::Connection.new(api_key) do |http|
8
8
  request = Net::HTTP::Get.new("/api/projects/#{api_key}.yaml")
9
9
  request.add_field("X-Client-Name", "web_translate_it")
10
10
  request.add_field("X-Client-Version", WebTranslateIt::Util.version)
@@ -26,11 +26,11 @@ module WebTranslateIt
26
26
 
27
27
  def self.fetch_stats(api_key)
28
28
  begin
29
- WebTranslateIt::Util.http_connection do |http|
29
+ WebTranslateIt::Connection.new(api_key) do |http|
30
30
  request = Net::HTTP::Get.new("/api/projects/#{api_key}/stats.yaml")
31
31
  request.add_field("X-Client-Name", "web_translate_it")
32
32
  request.add_field("X-Client-Version", WebTranslateIt::Util.version)
33
- Util.handle_response(http.request(request), true)
33
+ return Util.handle_response(http.request(request), true)
34
34
  end
35
35
  rescue Timeout::Error
36
36
  puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
@@ -39,15 +39,13 @@ module WebTranslateIt
39
39
  end
40
40
  end
41
41
 
42
- def self.create_locale(api_key, locale_code)
42
+ def self.create_locale(locale_code)
43
43
  begin
44
- WebTranslateIt::Util.http_connection do |http|
45
- request = Net::HTTP::Post.new("/api/projects/#{api_key}/locales")
46
- request.add_field("X-Client-Name", "web_translate_it")
47
- request.add_field("X-Client-Version", WebTranslateIt::Util.version)
48
- request.set_form_data({ 'id' => locale_code }, ';')
49
- Util.handle_response(http.request(request), true)
50
- end
44
+ request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/locales")
45
+ request.add_field("X-Client-Name", "web_translate_it")
46
+ request.add_field("X-Client-Version", WebTranslateIt::Util.version)
47
+ request.set_form_data({ 'id' => locale_code }, ';')
48
+ Util.handle_response(Connection.http_connection.request(request), true)
51
49
  rescue Timeout::Error
52
50
  puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
53
51
  sleep(5)
@@ -55,14 +53,12 @@ module WebTranslateIt
55
53
  end
56
54
  end
57
55
 
58
- def self.delete_locale(api_key, locale_code)
56
+ def self.delete_locale(locale_code)
59
57
  begin
60
- WebTranslateIt::Util.http_connection do |http|
61
- request = Net::HTTP::Delete.new("/api/projects/#{api_key}/locales/#{locale_code}")
62
- request.add_field("X-Client-Name", "web_translate_it")
63
- request.add_field("X-Client-Version", WebTranslateIt::Util.version)
64
- Util.handle_response(http.request(request), true)
65
- end
58
+ request = Net::HTTP::Delete.new("/api/projects/#{Connection.api_key}/locales/#{locale_code}")
59
+ request.add_field("X-Client-Name", "web_translate_it")
60
+ request.add_field("X-Client-Version", WebTranslateIt::Util.version)
61
+ Util.handle_response(Connection.http_connection.request(request), true)
66
62
  rescue Timeout::Error
67
63
  puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
68
64
  sleep(5)
@@ -1,29 +1,26 @@
1
1
  # encoding: utf-8
2
2
  module WebTranslateIt
3
3
  class String
4
- require 'net/https'
5
4
  require 'json'
6
5
 
7
- attr_accessor :api_key, :id, :key, :plural, :type, :dev_comment, :word_count, :status, :category, :label, :file,
6
+ attr_accessor :id, :key, :plural, :type, :dev_comment, :word_count, :status, :category, :label, :file,
8
7
  :created_at, :updated_at, :translations, :new_record
9
8
 
10
9
  # Initialize a new WebTranslateIt::String
11
- # The only mandatory parameter is `api_key`
12
10
  #
13
11
  # Implementation Example:
14
12
  #
15
- # WebTranslateIt::String.new('secret_api_token', { "key" => "product_name_123" })
13
+ # WebTranslateIt::String.new({ "key" => "product_name_123" })
16
14
  #
17
15
  # to instantiate a new String without any text.
18
16
  #
19
17
  # translation_en = WebTranslateIt::Translation.new({ "locale" => "en", "text" => "Hello" })
20
18
  # translation_fr = WebTranslateIt::Translation.new({ "locale" => "fr", "text" => "Bonjour" })
21
- # WebTranslateIt::String.new('secret_api_token', { "key" => "product_name_123", "translations" => [translation_en, translation_fr]})
19
+ # WebTranslateIt::String.new({ "key" => "product_name_123", "translations" => [translation_en, translation_fr]})
22
20
  #
23
21
  # to instantiate a new String with a source and target translation.
24
22
 
25
- def initialize(api_key, params = {})
26
- self.api_key = api_key
23
+ def initialize(params = {})
27
24
  self.id = params["id"] || nil
28
25
  self.key = params["key"] || nil
29
26
  self.plural = params["plural"] || nil
@@ -41,12 +38,11 @@ module WebTranslateIt
41
38
  end
42
39
 
43
40
  # Find a String based on filters
44
- # Needs a HTTPS Connection
45
41
  #
46
42
  # Implementation Example:
47
43
  #
48
- # WebTranslateIt::Util.http_connection do |connection|
49
- # strings = WebTranslateIt::String.find_all(connection, 'secret_api_token', { "key" => "product_name_123" })
44
+ # WebTranslateIt::Connection.new('secret_api_token') do
45
+ # strings = WebTranslateIt::String.find_all({ "key" => "product_name_123" })
50
46
  # end
51
47
  #
52
48
  # puts strings.inspect #=> An array of WebTranslateIt::String objects
@@ -55,8 +51,8 @@ module WebTranslateIt
55
51
  #
56
52
  # TODO: Implement pagination
57
53
 
58
- def self.find_all(http_connection, api_key, params = {})
59
- url = "/api/projects/#{api_key}/strings.yaml"
54
+ def self.find_all(params = {})
55
+ url = "/api/projects/#{Connection.api_key}/strings.yaml"
60
56
  url += '?' + HashUtil.to_params("filters" => params) unless params.empty?
61
57
 
62
58
  request = Net::HTTP::Get.new(url)
@@ -64,10 +60,10 @@ module WebTranslateIt
64
60
  request.add_field("X-Client-Version", WebTranslateIt::Util.version)
65
61
 
66
62
  begin
67
- response = Util.handle_response(http_connection.request(request), true)
63
+ response = Util.handle_response(Connection.http_connection.request(request), true)
68
64
  strings = []
69
65
  YAML.load(response).each do |string_response|
70
- string = WebTranslateIt::String.new(api_key, string_response)
66
+ string = WebTranslateIt::String.new(string_response)
71
67
  string.new_record = false
72
68
  strings.push(string)
73
69
  end
@@ -80,12 +76,11 @@ module WebTranslateIt
80
76
  end
81
77
 
82
78
  # Find a String based on its ID
83
- # Needs a HTTPS Connection
84
79
  #
85
80
  # Implementation Example:
86
81
  #
87
- # WebTranslateIt::Util.http_connection do |connection|
88
- # string = WebTranslateIt::String.find(connection, 'secret_api_token', 1234)
82
+ # WebTranslateIt::Connection.new('secret_api_token') do
83
+ # string = WebTranslateIt::String.find(1234)
89
84
  # end
90
85
  #
91
86
  # puts string.inspect #=> A WebTranslateIt::String object
@@ -93,15 +88,15 @@ module WebTranslateIt
93
88
  # to find and instantiate the String which ID is `1234`.
94
89
  #
95
90
 
96
- def self.find(http_connection, api_key, id)
97
- request = Net::HTTP::Get.new("/api/projects/#{api_key}/strings/#{id}.yaml")
91
+ def self.find(id)
92
+ request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/strings/#{id}.yaml")
98
93
  request.add_field("X-Client-Name", "web_translate_it")
99
94
  request.add_field("X-Client-Version", WebTranslateIt::Util.version)
100
95
 
101
96
  begin
102
- response = http_connection.request(request)
97
+ response = Connection.http_connection.request(request)
103
98
  return nil if response.code.to_i == 404
104
- string = WebTranslateIt::String.new(api_key, YAML.load(response.body))
99
+ string = WebTranslateIt::String.new(YAML.load(response.body))
105
100
  string.new_record = false
106
101
  return string
107
102
  rescue Timeout::Error
@@ -112,43 +107,37 @@ module WebTranslateIt
112
107
  end
113
108
 
114
109
  # Update or create a String to WebTranslateIt.com
115
- # Needs a HTTPS Connection
116
110
  #
117
111
  # Implementation Example:
118
112
  #
119
- # WebTranslateIt::Util.http_connection do |connection|
120
- # string = WebTranslateIt::String.find(connection, 'secret_api_token', 1234)
113
+ # WebTranslateIt::Connection.new('secret_api_token') do
114
+ # string = WebTranslateIt::String.find(1234)
121
115
  # string.status = "status_obsolete"
122
- # string.save(http_connection)
116
+ # string.save
123
117
  # end
124
118
  #
125
119
 
126
- def save(http_connection)
127
- if self.new_record
128
- self.create(http_connection)
129
- else
130
- self.update(http_connection)
131
- end
120
+ def save
121
+ self.new_record ? self.create : self.update
132
122
  end
133
123
 
134
124
  # Delete a String on WebTranslateIt.com
135
- # Needs a HTTPS Connection
136
125
  #
137
126
  # Implementation Example:
138
127
  #
139
- # WebTranslateIt::Util.http_connection do |connection|
140
- # string = WebTranslateIt::String.find(connection, 'secret_api_token', 1234)
141
- # string.delete(http_connection)
128
+ # WebTranslateIt::Connection.new('secret_api_token') do
129
+ # string = WebTranslateIt::String.find(1234)
130
+ # string.delete
142
131
  # end
143
132
  #
144
133
 
145
- def delete(http_connection)
146
- request = Net::HTTP::Delete.new("/api/projects/#{self.api_key}/strings/#{self.id}")
134
+ def delete
135
+ request = Net::HTTP::Delete.new("/api/projects/#{Connection.api_key}/strings/#{self.id}")
147
136
  request.add_field("X-Client-Name", "web_translate_it")
148
137
  request.add_field("X-Client-Version", WebTranslateIt::Util.version)
149
138
 
150
139
  begin
151
- Util.handle_response(http_connection.request(request), true)
140
+ Util.handle_response(Connection.http_connection.request(request), true)
152
141
  rescue Timeout::Error
153
142
  puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
154
143
  sleep(5)
@@ -157,27 +146,26 @@ module WebTranslateIt
157
146
  end
158
147
 
159
148
  # Gets a Translation for a String
160
- # Needs a HTTPS Connection
161
149
  #
162
150
  # Implementation Example:
163
151
  #
164
- # WebTranslateIt::Util.http_connection do |connection|
165
- # string = WebTranslateIt::String.find(connection, 'secret_api_token', 1234)
166
- # puts string.translation_for(connection, "fr") #=> A Translation object
152
+ # WebTranslateIt::Connection.new('secret_api_token') do
153
+ # string = WebTranslateIt::String.find(1234)
154
+ # puts string.translation_for("fr") #=> A Translation object
167
155
  # end
168
156
  #
169
157
 
170
- def translation_for(http_connection, locale)
158
+ def translation_for(locale)
171
159
  return self.translations unless self.translations == []
172
- request = Net::HTTP::Get.new("/api/projects/#{self.api_key}/strings/#{self.id}/locales/#{locale}/translations.yaml")
160
+ request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/strings/#{self.id}/locales/#{locale}/translations.yaml")
173
161
  request.add_field("X-Client-Name", "web_translate_it")
174
162
  request.add_field("X-Client-Version", WebTranslateIt::Util.version)
175
163
 
176
164
  begin
177
- response = Util.handle_response(http_connection.request(request), true)
165
+ response = Util.handle_response(Connection.http_connection.request(request), true)
178
166
  hash = YAML.load(response)
179
167
  return nil if hash.empty?
180
- translation = WebTranslateIt::Translation.new(api_key, hash)
168
+ translation = WebTranslateIt::Translation.new(hash)
181
169
  return translation
182
170
 
183
171
  rescue Timeout::Error
@@ -190,11 +178,10 @@ module WebTranslateIt
190
178
  protected
191
179
 
192
180
  # Save the changes made to a String to WebTranslateIt.com
193
- # Needs a HTTPS Connection
194
181
  #
195
182
 
196
- def update(http_connection)
197
- request = Net::HTTP::Put.new("/api/projects/#{self.api_key}/strings/#{self.id}.yaml")
183
+ def update
184
+ request = Net::HTTP::Put.new("/api/projects/#{Connection.api_key}/strings/#{self.id}.yaml")
198
185
  request.add_field("X-Client-Name", "web_translate_it")
199
186
  request.add_field("X-Client-Version", WebTranslateIt::Util.version)
200
187
  request.add_field("Content-Type", "application/json")
@@ -202,11 +189,11 @@ module WebTranslateIt
202
189
 
203
190
  self.translations.each do |translation|
204
191
  translation.string_id = self.id
205
- translation.save(http_connection)
192
+ translation.save
206
193
  end
207
194
 
208
195
  begin
209
- Util.handle_response(http_connection.request(request), true)
196
+ Util.handle_response(Connection.http_connection.request(request), true)
210
197
  rescue Timeout::Error
211
198
  puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
212
199
  sleep(5)
@@ -215,18 +202,17 @@ module WebTranslateIt
215
202
  end
216
203
 
217
204
  # Create a new String to WebTranslateIt.com
218
- # Needs a HTTPS Connection
219
205
  #
220
206
 
221
- def create(http_connection)
222
- request = Net::HTTP::Post.new("/api/projects/#{self.api_key}/strings")
207
+ def create
208
+ request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/strings")
223
209
  request.add_field("X-Client-Name", "web_translate_it")
224
210
  request.add_field("X-Client-Version", WebTranslateIt::Util.version)
225
211
  request.add_field("Content-Type", "application/json")
226
212
  request.body = self.to_json(true)
227
213
 
228
214
  begin
229
- response = YAML.load(Util.handle_response(http_connection.request(request), true))
215
+ response = YAML.load(Util.handle_response(Connection.http_connection.request(request), true))
230
216
  self.id = response["id"]
231
217
  self.new_record = false
232
218
  return true
@@ -247,9 +233,7 @@ module WebTranslateIt
247
233
  "status" => status,
248
234
  "label" => label,
249
235
  "category" => category,
250
- "file" => {
251
- "id" => file
252
- }
236
+ "file" => file
253
237
  }
254
238
  if self.translations.any? && with_translations
255
239
  hash.update({ "translations" => [] })