transifex-interface-ruby 0.0.1
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.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +513 -0
- data/Rakefile +1 -0
- data/lib/transifex.rb +87 -0
- data/lib/transifex/crud_requests.rb +99 -0
- data/lib/transifex/errors.rb +25 -0
- data/lib/transifex/formats.rb +9 -0
- data/lib/transifex/json.rb +23 -0
- data/lib/transifex/language.rb +13 -0
- data/lib/transifex/languages.rb +13 -0
- data/lib/transifex/project.rb +38 -0
- data/lib/transifex/project_components/language.rb +39 -0
- data/lib/transifex/project_components/language_components/coordinators.rb +29 -0
- data/lib/transifex/project_components/language_components/reviewers.rb +33 -0
- data/lib/transifex/project_components/language_components/translators.rb +33 -0
- data/lib/transifex/project_components/languages.rb +21 -0
- data/lib/transifex/projects.rb +22 -0
- data/lib/transifex/resource.rb +41 -0
- data/lib/transifex/resource_components/content.rb +54 -0
- data/lib/transifex/resource_components/source.rb +23 -0
- data/lib/transifex/resource_components/stats.rb +26 -0
- data/lib/transifex/resource_components/translation.rb +59 -0
- data/lib/transifex/resource_components/translation_components/string.rb +29 -0
- data/lib/transifex/resource_components/translation_components/strings.rb +39 -0
- data/lib/transifex/resource_components/translation_components/utilities.rb +22 -0
- data/lib/transifex/resources.rb +19 -0
- data/lib/transifex/version.rb +3 -0
- data/spec/lib/transifex/configuration_spec.rb +17 -0
- data/spec/lib/transifex/coordinators_spec.rb +32 -0
- data/spec/lib/transifex/formats_spec.rb +13 -0
- data/spec/lib/transifex/language_spec.rb +19 -0
- data/spec/lib/transifex/languages_spec.rb +13 -0
- data/spec/lib/transifex/project_language_spec.rb +49 -0
- data/spec/lib/transifex/project_languages_spec.rb +35 -0
- data/spec/lib/transifex/project_spec.rb +91 -0
- data/spec/lib/transifex/projects_spec.rb +48 -0
- data/spec/lib/transifex/resource_content_spec.rb +40 -0
- data/spec/lib/transifex/resource_source_spec.rb +30 -0
- data/spec/lib/transifex/resource_spec.rb +66 -0
- data/spec/lib/transifex/resources_spec.rb +50 -0
- data/spec/lib/transifex/reviewers_spec.rb +32 -0
- data/spec/lib/transifex/stats_spec.rb +34 -0
- data/spec/lib/transifex/translation_spec.rb +53 -0
- data/spec/lib/transifex/translation_string_spec.rb +33 -0
- data/spec/lib/transifex/translation_strings_spec.rb +57 -0
- data/spec/lib/transifex/translators_spec.rb +32 -0
- data/spec/lib/yaml/en.yml +26 -0
- data/spec/lib/yaml/fr.yml +4 -0
- data/spec/lib/yaml/resource_content_test.yml +27 -0
- data/spec/lib/yaml/resource_translation_default_content_test.yml +4 -0
- data/spec/lib/yaml/resource_translation_reviewed_content_test.yml +9 -0
- data/spec/spec_helper.rb +96 -0
- data/transifex-interface-ruby.gemspec +25 -0
- metadata +181 -0
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/transifex.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'multi_json'
|
3
|
+
require 'yaml'
|
4
|
+
require 'uri'
|
5
|
+
require 'digest/md5'
|
6
|
+
|
7
|
+
require "transifex/version"
|
8
|
+
require "transifex/resource_components/translation_components/utilities"
|
9
|
+
require "transifex/crud_requests"
|
10
|
+
require "transifex/errors"
|
11
|
+
require "transifex/formats"
|
12
|
+
require "transifex/project"
|
13
|
+
require "transifex/projects"
|
14
|
+
require "transifex/json"
|
15
|
+
require "transifex/languages"
|
16
|
+
require "transifex/language"
|
17
|
+
require "transifex/project_components/language"
|
18
|
+
require "transifex/project_components/language_components/coordinators"
|
19
|
+
require "transifex/project_components/language_components/reviewers"
|
20
|
+
require "transifex/project_components/language_components/translators"
|
21
|
+
require "transifex/project_components/languages"
|
22
|
+
require "transifex/resource"
|
23
|
+
require "transifex/resource_components/content"
|
24
|
+
require "transifex/resource_components/source"
|
25
|
+
require "transifex/resource_components/stats"
|
26
|
+
require "transifex/resource_components/translation"
|
27
|
+
require "transifex/resource_components/translation_components/string"
|
28
|
+
require "transifex/resource_components/translation_components/strings"
|
29
|
+
require "transifex/resources"
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
module Transifex
|
34
|
+
|
35
|
+
class Configuration
|
36
|
+
attr_accessor :client_login, :client_secret, :root_url
|
37
|
+
|
38
|
+
def root_url
|
39
|
+
@root_url ||= "https://www.transifex.com/api/2"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class << self
|
44
|
+
attr_accessor :configuration
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.configure
|
48
|
+
self.configuration ||= Configuration.new
|
49
|
+
yield configuration
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.build_request_url(url='')
|
53
|
+
URI(self.configuration.root_url + url)
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.query_api(method, url, params={})
|
57
|
+
uri = build_request_url(url)
|
58
|
+
|
59
|
+
res = Net::HTTP.start(uri.host, 80) do |http|
|
60
|
+
req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, request_headers)
|
61
|
+
req.basic_auth self.configuration.client_login, self.configuration.client_secret
|
62
|
+
req.body = Transifex::JSON.dump(params)
|
63
|
+
http.request req
|
64
|
+
end
|
65
|
+
|
66
|
+
begin
|
67
|
+
data = Transifex::JSON.load(res.body.nil? ? '' : res.body)
|
68
|
+
rescue
|
69
|
+
data = res.body
|
70
|
+
end
|
71
|
+
|
72
|
+
unless (res.is_a? Net::HTTPOK) || (res.is_a? Net::HTTPCreated) || (res.is_a? Net::HTTPNoContent)
|
73
|
+
error = TransifexError.new(uri, res.code, data)
|
74
|
+
raise error
|
75
|
+
end
|
76
|
+
|
77
|
+
data
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.request_headers
|
81
|
+
request_headers = {
|
82
|
+
'Content-Type' => 'application/json',
|
83
|
+
'Accept' => 'application/json',
|
84
|
+
'User-Agent' => "Transifex-interface-ruby/#{Transifex::VERSION}"
|
85
|
+
}
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
module Transifex
|
2
|
+
module CrudRequests
|
3
|
+
class << self
|
4
|
+
def generate_url(object, params = {})
|
5
|
+
class_name_string = object.class.name.split("::").last.downcase.to_s
|
6
|
+
url = ""
|
7
|
+
if object.class.respond_to?(:parents)
|
8
|
+
object.class.parents.map{|parent| url += "/" + parent.to_s + "/" + object.instance_variable_get("@" + parent.to_s + "_slug")}
|
9
|
+
end
|
10
|
+
url += "/" + class_name_string
|
11
|
+
url += "/" + object.instance_variable_get("@" + class_name_string + "_slug") if object.instance_variable_defined?("@" + class_name_string + "_slug")
|
12
|
+
params.each do |param|
|
13
|
+
url = add_param(url, param[0], param[1])
|
14
|
+
end
|
15
|
+
url
|
16
|
+
end
|
17
|
+
|
18
|
+
def add_param(url, param_name, param_value)
|
19
|
+
uri = URI(url)
|
20
|
+
params = URI.decode_www_form(uri.query || []) << [param_name, param_value]
|
21
|
+
uri.query = URI.encode_www_form(params)
|
22
|
+
uri.to_s
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module Fetch
|
27
|
+
module InstanceMethods
|
28
|
+
def fetch(options = {})
|
29
|
+
url = CrudRequests.generate_url(self, options)
|
30
|
+
Transifex.query_api(:get, url)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
def self.included(receiver)
|
34
|
+
receiver.send(:include, InstanceMethods)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
module Create
|
38
|
+
module InstanceMethods
|
39
|
+
def create(params = {}, options = {})
|
40
|
+
missing_keys = self.class::CREATE_REQUIRED_PARAMS - params.keys
|
41
|
+
raise MissingParametersError.new(missing_keys) unless (missing_keys == [:repository_url] || missing_keys == [:private]) || missing_keys.empty?
|
42
|
+
if params[:repository_url] && !params[:repository_url].empty? && params[:repository_url].match(/^(http|https|ftp):\/\/[a-zA-Z]+\.[a-zA-Z]+\.[a-zA-Z]+/).nil?
|
43
|
+
raise ParametersFormatError.new(:repository_url, "http|https|ftp://x.x.x")
|
44
|
+
end
|
45
|
+
unless options[:trad_from_file].nil?
|
46
|
+
if params[:i18n_type] == "YAML"
|
47
|
+
params[:content] = YAML::load_file(params[:content]).to_yaml
|
48
|
+
else
|
49
|
+
file = File.open(params[:content], "rb")
|
50
|
+
params[:content] = file.read
|
51
|
+
file.close
|
52
|
+
end
|
53
|
+
end
|
54
|
+
url = CrudRequests.generate_url(self)
|
55
|
+
Transifex.query_api(:post, url, params)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.included(receiver)
|
60
|
+
receiver.send(:include, InstanceMethods)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
module Update
|
64
|
+
module InstanceMethods
|
65
|
+
def update(params = {}, options = {})
|
66
|
+
if params.is_a?(Hash) && params[:i18n_type] && params[:i18n_type] != "TXT"
|
67
|
+
if params[:i18n_type] == "YML"
|
68
|
+
params[:content] = YAML::load_file(params[:content]).to_yaml
|
69
|
+
else
|
70
|
+
file = File.open(params[:content], "rb")
|
71
|
+
params[:content] = file.read
|
72
|
+
file.close
|
73
|
+
end
|
74
|
+
# Deal with accents
|
75
|
+
params[:content] = params[:content].force_encoding('UTF-8')
|
76
|
+
end
|
77
|
+
url = CrudRequests.generate_url(self)
|
78
|
+
Transifex.query_api(:put, url, params)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.included(receiver)
|
83
|
+
receiver.send(:include, InstanceMethods)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
module Delete
|
87
|
+
module InstanceMethods
|
88
|
+
def delete(params = {}, options = {})
|
89
|
+
url = CrudRequests.generate_url(self)
|
90
|
+
Transifex.query_api(:delete, url, params)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.included(receiver)
|
95
|
+
receiver.send(:include, InstanceMethods)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Transifex
|
2
|
+
|
3
|
+
class Error < StandardError
|
4
|
+
end
|
5
|
+
|
6
|
+
class MissingParametersError < Error
|
7
|
+
def initialize(*missing_attributes)
|
8
|
+
super("The following attributes are missing:" + missing_attributes.join(','))
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class ParametersFormatError < Error
|
13
|
+
def initialize(parameter, format_expected)
|
14
|
+
super("The following parameter: " + parameter.to_s + " must follow the format: " + format_expected)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class TransifexError < Error
|
19
|
+
attr_reader :request_url, :code, :details
|
20
|
+
def initialize(request_url, code, details)
|
21
|
+
@request_url, @code, @details = request_url, code, details
|
22
|
+
super(details) if details
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Transifex
|
2
|
+
module JSON
|
3
|
+
class << self
|
4
|
+
if MultiJson.respond_to?(:dump)
|
5
|
+
def dump(*args)
|
6
|
+
MultiJson.dump(*args)
|
7
|
+
end
|
8
|
+
|
9
|
+
def load(*args)
|
10
|
+
MultiJson.load(*args)
|
11
|
+
end
|
12
|
+
else
|
13
|
+
def dump(*args)
|
14
|
+
MultiJson.encode(*args)
|
15
|
+
end
|
16
|
+
|
17
|
+
def load(*args)
|
18
|
+
MultiJson.decode(*args)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Transifex
|
2
|
+
class Language
|
3
|
+
include Transifex::CrudRequests::Fetch
|
4
|
+
|
5
|
+
attr_accessor :language_slug
|
6
|
+
|
7
|
+
def initialize(language_code = nil)
|
8
|
+
raise MissingParametersError.new(["language_code"]) if language_code.nil?
|
9
|
+
@language_slug = language_code
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# Add a method add_resource to create a new resource for the project
|
2
|
+
# Add a method get_resources to fetch all the resources
|
3
|
+
|
4
|
+
module Transifex
|
5
|
+
class Project
|
6
|
+
include Transifex::CrudRequests::Fetch
|
7
|
+
include Transifex::CrudRequests::Update
|
8
|
+
include Transifex::CrudRequests::Delete
|
9
|
+
|
10
|
+
attr_accessor :project_slug, :resources
|
11
|
+
|
12
|
+
def initialize(project_slug = nil)
|
13
|
+
raise MissingParametersError.new("You must provide a slug for a project") if project_slug.nil?
|
14
|
+
@project_slug = project_slug
|
15
|
+
end
|
16
|
+
|
17
|
+
def resources
|
18
|
+
@resources ||= Transifex::Resources.new(@project_slug)
|
19
|
+
end
|
20
|
+
|
21
|
+
def resource(resource_slug)
|
22
|
+
Transifex::Resource.new(@project_slug, resource_slug)
|
23
|
+
end
|
24
|
+
|
25
|
+
def languages
|
26
|
+
Transifex::ProjectComponents::Languages.new(@project_slug)
|
27
|
+
end
|
28
|
+
|
29
|
+
def language(language_code)
|
30
|
+
Transifex::ProjectComponents::Language.new(@project_slug, language_code)
|
31
|
+
end
|
32
|
+
|
33
|
+
def fetch_with_details
|
34
|
+
options = {:details => true}
|
35
|
+
fetch(options)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Transifex
|
2
|
+
module ProjectComponents
|
3
|
+
class Language
|
4
|
+
include Transifex::CrudRequests::Fetch
|
5
|
+
include Transifex::CrudRequests::Update
|
6
|
+
include Transifex::CrudRequests::Delete
|
7
|
+
|
8
|
+
attr_accessor :project_slug, :language_slug
|
9
|
+
|
10
|
+
def initialize(project_slug = nil, language_code = nil)
|
11
|
+
raise MissingParametersError.new(["project_slug"]) if project_slug.nil?
|
12
|
+
raise MissingParametersError.new(["language_code"]) if language_code.nil?
|
13
|
+
@project_slug = project_slug
|
14
|
+
@language_slug = language_code
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.parents
|
18
|
+
[:project]
|
19
|
+
end
|
20
|
+
|
21
|
+
def fetch_with_details
|
22
|
+
options = {:details => true}
|
23
|
+
fetch(options)
|
24
|
+
end
|
25
|
+
|
26
|
+
def coordinators
|
27
|
+
Transifex::ProjectComponents::LanguageComponents::Coordinators.new(@project_slug, @language_slug)
|
28
|
+
end
|
29
|
+
|
30
|
+
def reviewers
|
31
|
+
Transifex::ProjectComponents::LanguageComponents::Reviewers.new(@project_slug, @language_slug)
|
32
|
+
end
|
33
|
+
|
34
|
+
def translators
|
35
|
+
Transifex::ProjectComponents::LanguageComponents::Translators.new(@project_slug, @language_slug)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Transifex
|
2
|
+
module ProjectComponents
|
3
|
+
module LanguageComponents
|
4
|
+
class Coordinators
|
5
|
+
include Transifex::CrudRequests::Fetch
|
6
|
+
include Transifex::CrudRequests::Update
|
7
|
+
|
8
|
+
attr_accessor :project_slug, :language_slug
|
9
|
+
|
10
|
+
def initialize(project_slug = nil, language_code = nil)
|
11
|
+
raise MissingParametersError.new(["project_slug"]) if project_slug.nil?
|
12
|
+
raise MissingParametersError.new(["language_code"]) if language_code.nil?
|
13
|
+
@project_slug = project_slug
|
14
|
+
@language_slug = language_code
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.parents
|
18
|
+
[:project, :language]
|
19
|
+
end
|
20
|
+
|
21
|
+
def update(coordinators_list = {}, options = {})
|
22
|
+
params = {}
|
23
|
+
params[:coordinators] = coordinators_list
|
24
|
+
super(params, options)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Transifex
|
2
|
+
module ProjectComponents
|
3
|
+
module LanguageComponents
|
4
|
+
class Reviewers
|
5
|
+
include Transifex::CrudRequests::Fetch
|
6
|
+
include Transifex::CrudRequests::Update
|
7
|
+
|
8
|
+
attr_accessor :project_slug, :language_slug
|
9
|
+
|
10
|
+
def initialize(project_slug = nil, language_code = nil)
|
11
|
+
raise MissingParametersError.new(["project_slug"]) if project_slug.nil?
|
12
|
+
raise MissingParametersError.new(["language_code"]) if language_code.nil?
|
13
|
+
@project_slug = project_slug
|
14
|
+
@language_slug = language_code
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.parents
|
18
|
+
[:project, :language]
|
19
|
+
end
|
20
|
+
|
21
|
+
def update(reviewers_list = {}, options = {})
|
22
|
+
# Transifex needs coordinators list to be passed also when updating reviewers list. Strange
|
23
|
+
# Fetch the current coordinators list and add it to the params as a workaround.
|
24
|
+
fetched_language_infos = Transifex::Project.new(@project_slug).language(@language_slug).fetch
|
25
|
+
params = {}
|
26
|
+
params[:coordinators] = fetched_language_infos["coordinators"]
|
27
|
+
params[:reviewers] = reviewers_list
|
28
|
+
super(params, options)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Transifex
|
2
|
+
module ProjectComponents
|
3
|
+
module LanguageComponents
|
4
|
+
class Translators
|
5
|
+
include Transifex::CrudRequests::Fetch
|
6
|
+
include Transifex::CrudRequests::Update
|
7
|
+
|
8
|
+
attr_accessor :project_slug, :language_slug
|
9
|
+
|
10
|
+
def initialize(project_slug = nil, language_code = nil)
|
11
|
+
raise MissingParametersError.new(["project_slug"]) if project_slug.nil?
|
12
|
+
raise MissingParametersError.new(["language_code"]) if language_code.nil?
|
13
|
+
@project_slug = project_slug
|
14
|
+
@language_slug = language_code
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.parents
|
18
|
+
[:project, :language]
|
19
|
+
end
|
20
|
+
|
21
|
+
def update(translators_list = {}, options = {})
|
22
|
+
# Transifex needs coordinators list to be passed also when updating reviewers list. Strange
|
23
|
+
# Fetch the current coordinators list and add it to the params as a workaround.
|
24
|
+
fetched_language_infos = Transifex::Project.new(@project_slug).language(@language_slug).fetch
|
25
|
+
params = {}
|
26
|
+
params[:coordinators] = fetched_language_infos["coordinators"]
|
27
|
+
params[:translators] = translators_list
|
28
|
+
super(params, options)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|