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.
Files changed (57) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +513 -0
  6. data/Rakefile +1 -0
  7. data/lib/transifex.rb +87 -0
  8. data/lib/transifex/crud_requests.rb +99 -0
  9. data/lib/transifex/errors.rb +25 -0
  10. data/lib/transifex/formats.rb +9 -0
  11. data/lib/transifex/json.rb +23 -0
  12. data/lib/transifex/language.rb +13 -0
  13. data/lib/transifex/languages.rb +13 -0
  14. data/lib/transifex/project.rb +38 -0
  15. data/lib/transifex/project_components/language.rb +39 -0
  16. data/lib/transifex/project_components/language_components/coordinators.rb +29 -0
  17. data/lib/transifex/project_components/language_components/reviewers.rb +33 -0
  18. data/lib/transifex/project_components/language_components/translators.rb +33 -0
  19. data/lib/transifex/project_components/languages.rb +21 -0
  20. data/lib/transifex/projects.rb +22 -0
  21. data/lib/transifex/resource.rb +41 -0
  22. data/lib/transifex/resource_components/content.rb +54 -0
  23. data/lib/transifex/resource_components/source.rb +23 -0
  24. data/lib/transifex/resource_components/stats.rb +26 -0
  25. data/lib/transifex/resource_components/translation.rb +59 -0
  26. data/lib/transifex/resource_components/translation_components/string.rb +29 -0
  27. data/lib/transifex/resource_components/translation_components/strings.rb +39 -0
  28. data/lib/transifex/resource_components/translation_components/utilities.rb +22 -0
  29. data/lib/transifex/resources.rb +19 -0
  30. data/lib/transifex/version.rb +3 -0
  31. data/spec/lib/transifex/configuration_spec.rb +17 -0
  32. data/spec/lib/transifex/coordinators_spec.rb +32 -0
  33. data/spec/lib/transifex/formats_spec.rb +13 -0
  34. data/spec/lib/transifex/language_spec.rb +19 -0
  35. data/spec/lib/transifex/languages_spec.rb +13 -0
  36. data/spec/lib/transifex/project_language_spec.rb +49 -0
  37. data/spec/lib/transifex/project_languages_spec.rb +35 -0
  38. data/spec/lib/transifex/project_spec.rb +91 -0
  39. data/spec/lib/transifex/projects_spec.rb +48 -0
  40. data/spec/lib/transifex/resource_content_spec.rb +40 -0
  41. data/spec/lib/transifex/resource_source_spec.rb +30 -0
  42. data/spec/lib/transifex/resource_spec.rb +66 -0
  43. data/spec/lib/transifex/resources_spec.rb +50 -0
  44. data/spec/lib/transifex/reviewers_spec.rb +32 -0
  45. data/spec/lib/transifex/stats_spec.rb +34 -0
  46. data/spec/lib/transifex/translation_spec.rb +53 -0
  47. data/spec/lib/transifex/translation_string_spec.rb +33 -0
  48. data/spec/lib/transifex/translation_strings_spec.rb +57 -0
  49. data/spec/lib/transifex/translators_spec.rb +32 -0
  50. data/spec/lib/yaml/en.yml +26 -0
  51. data/spec/lib/yaml/fr.yml +4 -0
  52. data/spec/lib/yaml/resource_content_test.yml +27 -0
  53. data/spec/lib/yaml/resource_translation_default_content_test.yml +4 -0
  54. data/spec/lib/yaml/resource_translation_reviewed_content_test.yml +9 -0
  55. data/spec/spec_helper.rb +96 -0
  56. data/transifex-interface-ruby.gemspec +25 -0
  57. metadata +181 -0
@@ -0,0 +1,21 @@
1
+ module Transifex
2
+ module ProjectComponents
3
+ class Languages
4
+ include Transifex::CrudRequests::Fetch
5
+ include Transifex::CrudRequests::Create
6
+
7
+ CREATE_REQUIRED_PARAMS = [:language_code, :coordinators]
8
+
9
+ attr_accessor :project_slug
10
+
11
+ def initialize(project_slug = nil)
12
+ raise MissingParametersError.new(["project_slug"]) if project_slug.nil?
13
+ @project_slug = project_slug
14
+ end
15
+
16
+ def self.parents
17
+ [:project]
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ module Transifex
2
+ class Projects
3
+ include Transifex::CrudRequests::Fetch
4
+ include Transifex::CrudRequests::Create
5
+
6
+ CREATE_REQUIRED_PARAMS = [:slug, :name, :description, :source_language_code, :repository_url, :private]
7
+
8
+ def self.fetch
9
+ Transifex::Projects.new.fetch
10
+ end
11
+
12
+ def fetch_with_details
13
+ options = {:details => true}
14
+ fetch(options)
15
+ end
16
+
17
+ def self.create(params = {}, options = {})
18
+ Projects.new.create(params, options)
19
+ Transifex::Project.new(params[:slug])
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,41 @@
1
+ module Transifex
2
+ class Resource
3
+ include Transifex::CrudRequests::Fetch
4
+ include Transifex::CrudRequests::Update
5
+ include Transifex::CrudRequests::Delete
6
+
7
+ attr_accessor :project_slug, :resource_slug
8
+
9
+ def initialize(project_slug = nil, resource_slug = nil)
10
+ raise MissingParametersError.new(["project_slug"]) if project_slug.nil?
11
+ raise MissingParametersError.new(["resource_slug"]) if resource_slug.nil?
12
+ @project_slug = project_slug
13
+ @resource_slug = resource_slug
14
+ end
15
+
16
+ def self.parents
17
+ [:project]
18
+ end
19
+
20
+ def fetch_with_details
21
+ options = {:details => true}
22
+ self.fetch(options)
23
+ end
24
+
25
+ def content
26
+ Transifex::ResourceComponents::Content.new(project_slug, resource_slug)
27
+ end
28
+
29
+ def statistics
30
+ Transifex::ResourceComponents::Stats.new(project_slug, resource_slug)
31
+ end
32
+
33
+ def translation(language_code = nil)
34
+ Transifex::ResourceComponents::Translation.new(project_slug, resource_slug, language_code)
35
+ end
36
+
37
+ def source(key = nil, context = "")
38
+ Transifex::ResourceComponents::Source.new(project_slug, resource_slug, key, context)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,54 @@
1
+ module Transifex
2
+ module ResourceComponents
3
+ class Content
4
+ include Transifex::CrudRequests::Fetch
5
+ include Transifex::CrudRequests::Update
6
+
7
+ attr_accessor :project_slug, :resource_slug
8
+
9
+ def initialize(project_slug, resource_slug)
10
+ raise MissingParametersError.new(["project_slug"]) if project_slug.nil?
11
+ raise MissingParametersError.new(["resource_slug"]) if resource_slug.nil?
12
+ @project_slug = project_slug
13
+ @resource_slug = resource_slug
14
+ end
15
+ def self.parents
16
+ [:project, :resource]
17
+ end
18
+ def fetch_with_file(path_to_file = nil)
19
+ raise MissingParametersError.new(["path_to_file"]) if path_to_file.nil?
20
+ options = {:file => true}
21
+ file_body = fetch(options)
22
+ write_to_file(path_to_file, file_body)
23
+ end
24
+
25
+ def update(params = {}, options = {})
26
+ resource_i18n_type = Transifex::Resource.new(@project_slug, @resource_slug).fetch
27
+ raise Transifex::Error.new("You must use the mimetype #{resource_i18n_type["i18n_type"]} to upload a new resource file") if !check_i18n_type_concordance?(resource_i18n_type["i18n_type"], params[:i18n_type])
28
+ super
29
+ end
30
+
31
+ private
32
+
33
+ def write_to_file(path_to_file, content)
34
+ case File.extname(path_to_file)
35
+ when ".yml"
36
+ # Needed to correct carriage return problems when writing the Api response in the file
37
+ d = YAML::load(content)
38
+ File.open(path_to_file, "w") { |file| file.write d.to_yaml }
39
+ else
40
+ File.open(path_to_file, "w") { |file| file.write content }
41
+ end
42
+ end
43
+
44
+ def check_i18n_type_concordance?(resource_i18n_type, param)
45
+ case resource_i18n_type
46
+ when "YML"
47
+ param == "YML" || param == "YAML"
48
+ else
49
+ param == resource_i18n_type
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,23 @@
1
+ module Transifex
2
+ module ResourceComponents
3
+ class Source
4
+ include Transifex::CrudRequests::Fetch
5
+ include Transifex::CrudRequests::Update
6
+ include Transifex::ResourceComponents::TranslationComponents::Utilities
7
+
8
+ attr_accessor :project_slug, :resource_slug, :source_slug
9
+
10
+ def initialize(project_slug = nil, resource_slug = nil, translation_key = nil, translation_context = "")
11
+ raise MissingParametersError.new(["project_slug"]) if project_slug.nil?
12
+ raise MissingParametersError.new(["resource_slug"]) if resource_slug.nil?
13
+ raise MissingParametersError.new(["translation_key"]) if translation_key.nil?
14
+ @project_slug = project_slug
15
+ @resource_slug = resource_slug
16
+ @source_slug = compute_source_entity_hash(translation_key, translation_context)
17
+ end
18
+ def self.parents
19
+ [:project, :resource]
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ module Transifex
2
+ module ResourceComponents
3
+ class Stats
4
+ include Transifex::CrudRequests::Fetch
5
+
6
+ attr_accessor :project_slug, :resource_slug
7
+
8
+
9
+ def initialize(project_slug, resource_slug)
10
+ raise MissingParametersError.new(["project_slug"]) if project_slug.nil?
11
+ raise MissingParametersError.new(["resource_slug"]) if resource_slug.nil?
12
+ @project_slug = project_slug
13
+ @resource_slug = resource_slug
14
+ end
15
+
16
+ def self.parents
17
+ [:project, :resource]
18
+ end
19
+
20
+ def fetch(language_code = nil)
21
+ instance_variable_set(:@stats_slug, language_code) unless language_code.nil?
22
+ super()
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,59 @@
1
+ module Transifex
2
+ module ResourceComponents
3
+ class Translation
4
+ include Transifex::CrudRequests::Fetch
5
+ include Transifex::CrudRequests::Update
6
+
7
+ attr_accessor :project_slug, :resource_slug, :translation_slug
8
+
9
+ def initialize(project_slug = nil, resource_slug = nil, translation_slug = nil)
10
+ raise MissingParametersError.new(["project_slug"]) if project_slug.nil?
11
+ raise MissingParametersError.new(["resource_slug"]) if resource_slug.nil?
12
+ raise MissingParametersError.new(["translation_slug"]) if translation_slug.nil?
13
+ @project_slug = project_slug
14
+ @resource_slug = resource_slug
15
+ @translation_slug = translation_slug
16
+ end
17
+
18
+ def self.parents
19
+ [:project, :resource]
20
+ end
21
+
22
+ def fetch(options = {})
23
+ super
24
+ end
25
+
26
+ def fetch_with_file(options = {})
27
+ raise MissingParametersError.new(["path_to_file"]) if options[:path_to_file].nil?
28
+ path_to_file = options.delete(:path_to_file)
29
+ options[:file] = true
30
+ file_body = fetch(options)
31
+ write_to_file(path_to_file, file_body)
32
+ end
33
+
34
+ def strings
35
+ Transifex::ResourceComponents::TranslationComponents::Strings.new(@project_slug, @resource_slug, @translation_slug)
36
+ end
37
+
38
+ def string(key = nil , context = "")
39
+ raise MissingParametersError.new(["key"]) if key.nil?
40
+ raise MissingParametersError.new(["context"]) if context.nil?
41
+ Transifex::ResourceComponents::TranslationComponents::String.new(@project_slug, @resource_slug, @translation_slug, key, context)
42
+ end
43
+
44
+ private
45
+
46
+ def write_to_file(path_to_file, content)
47
+ case File.extname(path_to_file)
48
+ when ".yml"
49
+ # Needed to correct carriage return problems when writing the Api response in the file
50
+ d = YAML::load(content)
51
+ File.open(path_to_file, "w") { |file| file.write d.to_yaml }
52
+ else
53
+ File.open(path_to_file, "w") { |file| file.write content }
54
+ end
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,29 @@
1
+ module Transifex
2
+ module ResourceComponents
3
+ module TranslationComponents
4
+ class String
5
+ include Transifex::CrudRequests::Fetch
6
+ include Transifex::CrudRequests::Update
7
+ include Utilities
8
+
9
+ attr_accessor :project_slug, :resource_slug, :translation_slug, :string_slug
10
+
11
+ def initialize(project_slug = nil, resource_slug = nil, translation_slug = nil, translation_key = nil, translation_context = nil)
12
+ raise MissingParametersError.new(["translation_key"]) if translation_key.nil?
13
+ raise MissingParametersError.new(["translation_context"]) if translation_context.nil?
14
+ raise MissingParametersError.new(["project_slug"]) if project_slug.nil?
15
+ raise MissingParametersError.new(["resource_slug"]) if resource_slug.nil?
16
+ raise MissingParametersError.new(["translation_slug"]) if translation_slug.nil?
17
+ @project_slug = project_slug
18
+ @resource_slug = resource_slug
19
+ @translation_slug = translation_slug
20
+ @string_slug = compute_source_entity_hash(translation_key, translation_context)
21
+ end
22
+
23
+ def self.parents
24
+ [:project, :resource, :translation]
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,39 @@
1
+ module Transifex
2
+ module ResourceComponents
3
+ module TranslationComponents
4
+ class Strings
5
+ include Transifex::CrudRequests::Fetch
6
+ include Transifex::CrudRequests::Update
7
+ include Utilities
8
+ attr_accessor :project_slug, :resource_slug, :translation_slug
9
+
10
+ def initialize(project_slug = nil, resource_slug = nil, translation_slug = nil)
11
+ raise MissingParametersError.new(["project_slug"]) if project_slug.nil?
12
+ raise MissingParametersError.new(["resource_slug"]) if resource_slug.nil?
13
+ raise MissingParametersError.new(["translation_slug"]) if translation_slug.nil?
14
+ @project_slug = project_slug
15
+ @resource_slug = resource_slug
16
+ @translation_slug = translation_slug
17
+ end
18
+
19
+ def fetch_with_details(options = {})
20
+ options[:details] = true
21
+ self.fetch(options)
22
+ end
23
+
24
+ def self.parents
25
+ [:project, :resource, :translation]
26
+ end
27
+
28
+ def update(params)
29
+ params = [params] unless params.is_a?(Array)
30
+ params.each do |param|
31
+ raise MissingParametersError.new(["key"]) unless param.key?(:key)
32
+ param[:source_entity_hash] = compute_source_entity_hash(param[:key], param[:context])
33
+ end
34
+ super
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,22 @@
1
+ module Transifex
2
+ module ResourceComponents
3
+ module TranslationComponents
4
+ module Utilities
5
+ private
6
+ def compute_source_entity_hash(key, context = "")
7
+ keys = ''
8
+ if context.is_a?(Array)
9
+ unless context.empty?
10
+ keys = [key] + context
11
+ else
12
+ keys = [key, '']
13
+ end
14
+ else
15
+ keys = [key, context]
16
+ end
17
+ Digest::MD5.hexdigest(keys.join(':').encode("UTF-8"))
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module Transifex
2
+ class Resources
3
+ include Transifex::CrudRequests::Fetch
4
+ include Transifex::CrudRequests::Create
5
+
6
+ CREATE_REQUIRED_PARAMS = [:slug, :name, :i18n_type, :content]
7
+
8
+ attr_accessor :project_slug
9
+
10
+ def initialize(project_slug = nil)
11
+ raise MissingParametersError.new(["project_slug"]) if project_slug.nil?
12
+ @project_slug = project_slug
13
+ end
14
+
15
+ def self.parents
16
+ [:project]
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Transifex
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ describe Transifex::Configuration do
4
+
5
+ it "should have the correct credentials" do
6
+ c = Transifex.configuration
7
+ expect(c.client_login).to eq('')
8
+ expect(c.client_secret).to eq('')
9
+ end
10
+
11
+ it "should not have incorrect credentials" do
12
+ c = Transifex.configuration
13
+ expect(c.client_login).not_to eq('')
14
+ expect(c.client_secret).not_to eq('')
15
+ end
16
+
17
+ end
@@ -0,0 +1,32 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ describe Transifex::ProjectComponents::LanguageComponents::Coordinators do
4
+ before(:all) do
5
+ @project = Transifex::Project.new("projet-de-test-1")
6
+ end
7
+
8
+ describe "Instanciation" do
9
+ it "should raise an error when no parameters given" do
10
+ expect{ Transifex::ProjectComponents::LanguageComponents::Coordinators.new }.to raise_error(Transifex::MissingParametersError)
11
+ end
12
+ end
13
+
14
+ describe "Fetch" do
15
+ it "should not raise an error and retrieve the language's informations" do
16
+ language_coordinators_infos = nil
17
+ expect{ language_coordinators_infos = @project.language('en').coordinators.fetch }.to_not raise_error
18
+ expect(language_coordinators_infos).to be_a_kind_of(Hash)
19
+ expect(language_coordinators_infos.keys).to contain_exactly("coordinators")
20
+ end
21
+ end
22
+
23
+ describe "Update" do
24
+ it "should not raise an error and update the coordinators list" do
25
+ expect{ @project.language('en').coordinators.update(['fredericgrais'])}.to_not raise_error
26
+ end
27
+
28
+ it "should raise an error if the coordinator doesn't exist" do
29
+ expect{ @project.language('en').coordinators.update(['grgrgef'])}.to raise_error(Transifex::TransifexError)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,13 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ describe Transifex::Formats do
4
+
5
+ describe "Fetch" do
6
+ it "should fetch all formats without raising an error" do
7
+ fetched_formats = nil
8
+ expect{ fetched_formats = Transifex::Formats.fetch }.to_not raise_error
9
+ expect(fetched_formats).to be_a_kind_of(Hash)
10
+ expect(fetched_formats.first[1].keys).to contain_exactly("mimetype", "file-extensions", "description")
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ describe Transifex::Language do
4
+
5
+ describe "instanciation" do
6
+ it "should raise an error if a language code is not provided" do
7
+ expect { Transifex::Language.new }.to raise_error(Transifex::MissingParametersError)
8
+ end
9
+ end
10
+
11
+ describe "Fetch" do
12
+ it "should fetch the selected language infos without raising an error" do
13
+ fetched_languages = nil
14
+ expect{ fetched_languages = Transifex::Languages.fetch('fr') }.to_not raise_error
15
+ expect(fetched_languages).to be_a_kind_of(Hash)
16
+ expect(fetched_languages.keys).to contain_exactly("rtl", "pluralequation", "code", "name", "nplurals")
17
+ end
18
+ end
19
+ end