translation_service 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 42b1229c9e01ce5f6a706f3d7bc5cda8bd3fbba9
4
+ data.tar.gz: 3abee52d381847885a4c14ff07ad485ef1f99282
5
+ SHA512:
6
+ metadata.gz: 41a8d79060645f6972153f9da784b38a8a8cb8a5c5cb853b402183df771e7359a4d16857ec53e0eb688762973b438947bcad6a3be959ab31a2fe18fc7321aa0f
7
+ data.tar.gz: 4136020038e49e4939d99e33e890a7c2caf2bf4fdfaa66def7f906813f7cfe1c6b6d1a5a77165c328177b64179d2e28b598232ae195a5e9a2de16b717c1003ca
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.3.3
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in translation_service.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ translation_service (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ fakeweb (1.3.0)
11
+ rake (12.3.3)
12
+ rspec (3.6.0)
13
+ rspec-core (~> 3.6.0)
14
+ rspec-expectations (~> 3.6.0)
15
+ rspec-mocks (~> 3.6.0)
16
+ rspec-core (3.6.0)
17
+ rspec-support (~> 3.6.0)
18
+ rspec-expectations (3.6.0)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.6.0)
21
+ rspec-mocks (3.6.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.6.0)
24
+ rspec-support (3.6.0)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ fakeweb (~> 1.3)
31
+ rake (~> 12.0)
32
+ rspec (~> 3.0)
33
+ translation_service!
34
+
35
+ BUNDLED WITH
36
+ 2.1.4
@@ -0,0 +1,47 @@
1
+ # TranslationService
2
+
3
+ This gem provides networking to a custom translation service.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'translation_service'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install translation_service
20
+
21
+ ## Usage
22
+
23
+ require 'translation_service'
24
+
25
+ Maybe put the config in an itializer?
26
+
27
+ ```ruby
28
+ TranslationService.configure do |config|
29
+ config.master_revision = 'test'
30
+ config.address = 'https://content.translations.lan/webservices/'
31
+ config.key = 'oi321'
32
+ config.password = 'fakepw'
33
+ config.location = '/spec/locales'
34
+ config.available_locales = [
35
+ :en, :fr
36
+ ]
37
+ end
38
+
39
+ TranslationService.pull_all
40
+
41
+ TranslationService.add('gem.test', 'test text', 'http://example.context')
42
+ ```
43
+
44
+ ## Development
45
+
46
+ It is currently private, however, may make it public in the future.
47
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "translation_service"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,139 @@
1
+ require "translation_service/version"
2
+ require "translation_service/configuration"
3
+ require 'net/http'
4
+ require 'digest'
5
+ require 'json'
6
+ require 'uri'
7
+ require 'logger'
8
+ require 'yaml'
9
+
10
+ module TranslationService
11
+ class Error < StandardError; end
12
+ class << self
13
+ attr_accessor :update_all
14
+ attr_accessor :add
15
+ attr_writer :config
16
+ end
17
+
18
+ def initialize(config = Config.new)
19
+ @config = config
20
+ end
21
+
22
+ def self.config
23
+ @config = Configuration.new
24
+ end
25
+
26
+ def self.configure
27
+ yield(config)
28
+ end
29
+
30
+ # Keep for legacy
31
+ def self.update_all
32
+ pull_all
33
+ end
34
+
35
+ def self.pull_all
36
+ locales.each do |lang|
37
+ puts "\e[42m" + 'downloading ' + lang + "\e[0m"
38
+ data = unsparse(pull(lang)[lang]['all'])
39
+
40
+ # Convert vi-VN to vi since they rather not add it
41
+ lang = 'vi' if lang == 'vi-VN'
42
+
43
+ new_data = {}
44
+ new_data[lang] = data
45
+ create_file(new_data, lang)
46
+ end
47
+ end
48
+
49
+ def self.add(key, text, url)
50
+ req = Net::HTTP::Post.new(add_address.path)
51
+ req.set_form_data(
52
+ timestamp: timestamp,
53
+ security_token: generate_token(add_address),
54
+ master_revision: @config.master_revision,
55
+ string: text,
56
+ key: key,
57
+ type: 'MARKDOWN',
58
+ is_locked: false,
59
+ context: url
60
+ )
61
+
62
+ send_request(add_address, req)
63
+ end
64
+
65
+ def self.pull(language)
66
+ req = Net::HTTP::Post.new(pull_address.path)
67
+ req.set_form_data(
68
+ timestamp: timestamp,
69
+ security_token: generate_token(pull_address),
70
+ master_revision: @config.master_revision,
71
+ locales: language
72
+ )
73
+
74
+ send_request(pull_address, req)
75
+ end
76
+
77
+ def self.send_request(address, req)
78
+ http = Net::HTTP.new(address.host, address.port)
79
+ http.use_ssl = true
80
+
81
+ begin
82
+ JSON.parse(http.request(req).body)
83
+ rescue => e
84
+ Logger.new(STDERR).error("Localize failed: #{e}")
85
+ end
86
+ end
87
+
88
+ def self.pull_address
89
+ URI(@config.address + @config.key + '/translations.json')
90
+ end
91
+
92
+ def self.add_address
93
+ URI(@config.address + @config.key + '/add-string.json')
94
+ end
95
+
96
+ def self.timestamp
97
+ @timestamp ||= Time.now.to_i
98
+ end
99
+
100
+ def self.generate_token(address)
101
+ sha1 = Digest::SHA1.new
102
+ digest = timestamp.to_s + address.to_s + @config.password
103
+ sha1.hexdigest digest
104
+ end
105
+
106
+ def self.create_file(data, lang)
107
+ dir = @config.location
108
+ file = File.join(Dir.pwd, dir, lang + '.yml')
109
+ #file = File.join(Rails.root, dir, lang + '.yml')
110
+ File.open(file, 'w') do |f|
111
+ f.write data.to_yaml
112
+ end
113
+ end
114
+
115
+ def self.locales
116
+ locales = @config.available_locales
117
+ locales.map!{ |l| l.to_s == 'vi' ? 'vi-VN' : l.to_s }
118
+ end
119
+
120
+ # Taken from Unsparsify gem. Error response now includes broken key.
121
+ def self.unsparse(hsh)
122
+ hsh.each_with_object({}) do |(k, v), memo|
123
+ current = memo
124
+ key = k.split('.')
125
+ up_next = partial = key.shift
126
+ until key.size.zero?
127
+ up_next = key.shift
128
+ up_next = up_next.to_i if up_next =~ /\A[0-9]+\Z/
129
+ current = (current[partial] ||= (up_next.is_a?(Integer) ? [] : {}))
130
+ case up_next
131
+ when Integer then raise 'Problem with key ' + k.to_s unless current.is_a? Array
132
+ else raise 'Problem with key ' + k.to_s unless current.is_a? Hash
133
+ end
134
+ partial = up_next
135
+ end
136
+ current[up_next] = v
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,10 @@
1
+ module TranslationService
2
+ class Configuration
3
+ attr_accessor :master_revision
4
+ attr_accessor :address
5
+ attr_accessor :key
6
+ attr_accessor :password
7
+ attr_accessor :location
8
+ attr_accessor :available_locales
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module TranslationService
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'lib/translation_service/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "translation_service"
5
+ spec.version = TranslationService::VERSION
6
+ spec.authors = ["EddieOne"]
7
+ spec.email = ["eddie@onefoldmedia.com"]
8
+
9
+ spec.summary = "translation service gem"
10
+ spec.description = "Connect a RoR porject to the transaltion service"
11
+ spec.homepage = "https://y8.com"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+ spec.add_development_dependency "rspec", "~> 3.8"
14
+ spec.add_development_dependency "fakeweb", ["~> 1.3"]
15
+
16
+ #spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
20
+ spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: translation_service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - EddieOne
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fakeweb
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ description: Connect a RoR porject to the transaltion service
42
+ email:
43
+ - eddie@onefoldmedia.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - lib/translation_service.rb
58
+ - lib/translation_service/configuration.rb
59
+ - lib/translation_service/version.rb
60
+ - translation_service.gemspec
61
+ homepage: https://y8.com
62
+ licenses: []
63
+ metadata:
64
+ homepage_uri: https://y8.com
65
+ source_code_uri: 'TODO: Put your gem''s public repo URL here.'
66
+ changelog_uri: 'TODO: Put your gem''s CHANGELOG.md URL here.'
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.3.0
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.5.2
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: translation service gem
87
+ test_files: []