rack-webtranslateit 0.1.1 → 0.1.2

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.
@@ -4,17 +4,20 @@ class Rack::Webtranslateit::Configuration
4
4
  attr_accessor :api_key, :files, :master_locale, :password
5
5
 
6
6
  def initialize
7
- root = defined?(RAILS_ROOT) && RAILS_ROOT
8
- root ||= defined?(RACK_ROOT) && RACK_ROOT
9
- root ||= File.expand_path(".")
10
- file = File.join(root, 'config', 'translation.yml')
7
+ unless file = self.class.config_location
8
+ root = defined?(RAILS_ROOT) && RAILS_ROOT
9
+ root ||= defined?(RACK_ROOT) && RACK_ROOT
10
+ root ||= File.expand_path(".")
11
+ file = File.join(root, 'config', 'translation.yml')
12
+ end
13
+
11
14
  configuration = YAML.load_file(file)
12
15
  self.api_key = configuration['api_key']
13
16
  self.password = configuration['password']
14
17
  self.master_locale = configuration['master_locale'].to_s
15
18
  self.files = []
16
19
  configuration['files'].each do |file_id, file_path|
17
- self.files.push(Rack::Webtranslateit::TranslationFile.new(file_id, file_path, api_key))
20
+ self.files.push(Rack::Webtranslateit::TranslationFile.new(file_id, file_path, api_key, master_locale))
18
21
  end
19
22
  end
20
23
 
@@ -32,8 +35,12 @@ class Rack::Webtranslateit::Configuration
32
35
  end
33
36
  end
34
37
 
35
- def self.method_missing(name, *args)
36
- @configuration ||= new
37
- @configuration.send(name, *args)
38
+ class << self
39
+ def method_missing(name, *args)
40
+ @configuration ||= new
41
+ @configuration.send(name, *args)
42
+ end
43
+
44
+ attr_accessor :config_location
38
45
  end
39
46
  end
@@ -1,12 +1,15 @@
1
1
  require 'net/https'
2
+ require 'net/http/post/multipart'
2
3
  require 'ftools'
4
+
3
5
  class Rack::Webtranslateit::TranslationFile
4
- attr_accessor :id, :file_path, :api_key
6
+ attr_accessor :id, :file_path, :api_key, :master_locale
5
7
 
6
- def initialize(id, file_path, api_key)
8
+ def initialize(id, file_path, api_key, master_locale)
7
9
  self.id = id
8
10
  self.file_path = file_path
9
11
  self.api_key = api_key
12
+ self.master_locale = master_locale
10
13
  end
11
14
 
12
15
  def for(locale)
@@ -30,34 +33,38 @@ class Rack::Webtranslateit::TranslationFile
30
33
  ! $?.success?
31
34
  end
32
35
 
36
+ def master?
37
+ @file.master_locale == locale
38
+ end
39
+
33
40
  def modified_remotely?
34
- get_translations.code.to_i == 200
41
+ get_translations.code.to_i == 200 unless master?
35
42
  end
36
43
 
37
44
  def get_translations(respect_modified_since = true)
38
45
  http_connection do |http|
39
46
  request = Net::HTTP::Get.new(api_url)
40
47
  request.add_field('If-Modified-Since', File.mtime(File.new(file_path, 'r')).rfc2822) if File.exist?(file_path) and respect_modified_since
41
- return http.request(request)
48
+ http.request(request)
42
49
  end
43
50
  end
44
51
 
45
52
  def fetch
46
53
  response = get_translations
47
- File.open(file_path, 'w'){|f| f << response.body } if response.code.to_i == 200 and not response.body.blank?
54
+ File.open(file_path, 'w'){|f| f << response.body } if response.code.to_i == 200 and not response.body == ""
48
55
  end
49
56
 
50
57
  def fetch!
51
58
  response = get_translations(false)
52
- File.open(file_path, 'w'){|f| f << response.body } if response.code.to_i == 200 and not response.body.blank?
59
+ File.open(file_path, 'w'){|f| f << response.body } if response.code.to_i == 200 and not response.body == ""
53
60
  end
54
61
 
55
- def send
62
+ def upload
56
63
  File.open(file_path) do |file|
57
64
  http_connection do |http|
58
65
  request = Net::HTTP::Put::Multipart.new(api_url, "file" => UploadIO.new(file, "text/plain", file.path))
59
66
  response = http.request(request)
60
- return response.code.to_i
67
+ raise "Failed to Upload File, #{response.code} response recieved" unless response.code.to_i < 400
61
68
  end
62
69
  end
63
70
  end
@@ -4,7 +4,6 @@ class Rack::Webtranslateit::Ui < Sinatra::Base
4
4
  set :views => File.join(File.dirname(__FILE__), *%w[.. .. .. templates])
5
5
 
6
6
  use Rack::ShowExceptions
7
- use Rack::Lint
8
7
  use Rack::Static, :urls => ["/static"], :root => File.join(File.dirname(__FILE__), *%w[.. .. .. public])
9
8
 
10
9
  get(''){redirect "/"}
@@ -19,8 +18,14 @@ class Rack::Webtranslateit::Ui < Sinatra::Base
19
18
  redirect "/"
20
19
  end
21
20
 
21
+ post '/upload' do
22
+ upload_master_file
23
+ redirect '/'
24
+ end
25
+
22
26
  helpers do
23
27
  def highlight_unless_equal(value, expected)
28
+ return if value.nil?
24
29
  value == expected ? value : "<em>#{value}</em>"
25
30
  end
26
31
 
@@ -42,10 +47,16 @@ protected
42
47
  def fetch_translations
43
48
  config.files.each do |file|
44
49
  config.locales.each do |locale|
45
- next if config.ignore_locales.include?(locale)
50
+ next if config.master_locale == locale
46
51
  response_code = file.for(locale).fetch!
47
52
  end
48
53
  end
49
54
  end
50
55
 
56
+ def upload_master_file
57
+ config.files.each do |file|
58
+ file.for(config.master_locale).upload
59
+ end
60
+ end
61
+
51
62
  end
@@ -29,7 +29,7 @@
29
29
  <tbody>
30
30
  <% locales.each do |locale| locale_file = file.for(locale) %>
31
31
  <tr>
32
- <th style="width: 5px;"><%= locale %></th>
32
+ <th style="width: 5px;"><%= locale %><%= " (master)" if locale_file.master? %></th>
33
33
  <td><abbr title="<%= locale_file.file_path %>"><%= File.basename(locale_file.file_path) %></abbr></td>
34
34
  <td><%= highlight_unless_equal(locale_file.exists?, true) %></td>
35
35
  <td><%= highlight_unless_equal(locale_file.committed?, true) %></td>
@@ -43,6 +43,10 @@
43
43
  <form method="post" action="<%= base_path %>/update">
44
44
  <input type="submit" value="Update All Translations" />
45
45
  </form>
46
+
47
+ <form method="post" action="<%= base_path %>/upload">
48
+ <input type="submit" value="Upload Master File" onclick="javascript: return confirm('This will immediately make new required translations available on webtranslateit.com. Are you sure you with to continue?')" />
49
+ </form>
46
50
  </div>
47
51
  </div>
48
52
  <div id="footer">
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-webtranslateit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Lea
@@ -9,9 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-05 00:00:00 +00:00
12
+ date: 2010-01-06 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
15
25
  - !ruby/object:Gem::Dependency
16
26
  name: sinatra
17
27
  type: :runtime
@@ -22,6 +32,36 @@ dependencies:
22
32
  - !ruby/object:Gem::Version
23
33
  version: "0"
24
34
  version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: multipart-post
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "1.0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: webmock
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ - !ruby/object:Gem::Dependency
56
+ name: shoulda
57
+ type: :development
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
25
65
  description:
26
66
  email: contrib@tomlea.co.uk
27
67
  executables: []