translation_io-api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 490eaaf02161a2ee820ccd52cac7a6a1af4674c0b7c1543b4b7bfcc87fcb4995
4
+ data.tar.gz: 732d13e22a8ffc3d6a03225c053018f8f5a3b145ac47bbd46eb3c7a6ca9e6d8d
5
+ SHA512:
6
+ metadata.gz: 790eaab0d2c75eaf1747ec90040f759e595902c33d1ababca0f543efeb568fcae908dd3d904d7aa598919e81d867a8062f0b557bfc25bab2591ec88c66953b24
7
+ data.tar.gz: 0b68db11434bc16a102ab7112a030605b621dde548331d064bb23f6c44969a97a50fc7717b2dc7ba0aa4181043f65d403a61dc8c1c71cbcc83c0093c203512ec
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # Rubocop caching file
11
+ .rubocop-https*
12
+
13
+ Gemfile.lock
@@ -0,0 +1,2 @@
1
+ inherit_from:
2
+ - https://raw.githubusercontent.com/lessonly/rubocop-default-configuration/master/.rubocop.yml
@@ -0,0 +1 @@
1
+ 2.7.0
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.3
6
+ before_install: gem install bundler -v 2.1.4
File without changes
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at rreinhardt9@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in translation_io-api.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Ross Reinhardt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,111 @@
1
+ # TranslationIO::API
2
+
3
+ Wrapper for the translation.io rest API. This is a work in progress, we started with the endpoints we needed but expect that we can and contributers can continue to this base.
4
+ Check out the [the documentation](https://translation.io/docs/api) for their API and the documentation on this gem.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'translation_io-api'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install translation_io-api
21
+
22
+ ## Usage
23
+
24
+ ### List Segments
25
+
26
+ ```Ruby
27
+ client = TranslationIO::API::Segments.new(api_key: "my-api-key")
28
+ response = client.list("fr", tag: "needs review")
29
+
30
+ response.code
31
+ => 200
32
+
33
+ response.success?
34
+ => true
35
+
36
+ response.body
37
+ => {
38
+ "segments": [
39
+ {
40
+ "id": 2,
41
+ "source_id": "b9d78193f955836a4ffd2ace3d06c725f605a8fdf142522908dc5605fce30f72",
42
+ "target_language": "fr",
43
+ "type": "source",
44
+ "source": "Good afternoon",
45
+ "target": "Bon après-midi",
46
+ "tags": [
47
+ "needs review"
48
+ ]
49
+ }
50
+ ]
51
+ }
52
+
53
+ ```
54
+
55
+ ### Add a Tag
56
+
57
+ ```Ruby
58
+ client = TranslationIO::API::Segments.new(api_key: "my-api-key")
59
+ response = client.add_tag(1, name: "needs review")
60
+
61
+ response.body
62
+ => {
63
+ "segment": {
64
+ "id": 1,
65
+ "source_id": "85af68954641ff0400756bc3b93de505680e621e834040a63732621e659c1d82",
66
+ "target_language": "fr",
67
+ "type": "source",
68
+ "source": "Hello",
69
+ "target": "Bonjour",
70
+ "tags": ["need review"]
71
+ }
72
+ }
73
+ ```
74
+
75
+ ### Remove a Tag
76
+
77
+ ```Ruby
78
+ client = TranslationIO::API::Segments.new(api_key: "my-api-key")
79
+ response = client.remove_tag(1, name: "needs review")
80
+
81
+ response.body
82
+ => {
83
+ "segment": {
84
+ "id": 1,
85
+ "source_id": "85af68954641ff0400756bc3b93de505680e621e834040a63732621e659c1d82",
86
+ "target_language": "fr",
87
+ "type": "source",
88
+ "source": "Hello",
89
+ "target": "Bonjour"
90
+ }
91
+ }
92
+ ```
93
+
94
+ ## Development
95
+
96
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
97
+
98
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
99
+
100
+ ## Contributing
101
+
102
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rreinhardt9/translation_io-api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/rreinhardt9/translation_io-api/blob/master/CODE_OF_CONDUCT.md).
103
+
104
+
105
+ ## License
106
+
107
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
108
+
109
+ ## Code of Conduct
110
+
111
+ Everyone interacting in the TranslationIO::API project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rreinhardt9/translation_io-api/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "translation_io/api"
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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "translation_io/api/version"
4
+ require "translation_io/api/segments"
5
+ require "translation_io/api/request"
6
+ require "translation_io/api/response"
7
+
8
+ module TranslationIO
9
+ module API
10
+ class Error < StandardError; end
11
+ end
12
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "json"
5
+
6
+ module TranslationIO
7
+ module API
8
+ # An HTTP request
9
+ class Request
10
+ # Constructor
11
+ #
12
+ # @param api_key [String] translation.io api key
13
+ #
14
+ # @return [TranslationIO::API::Request]
15
+
16
+ def initialize(api_key:)
17
+ @api_key = api_key
18
+ end
19
+
20
+ # Get request
21
+ #
22
+ # @param uri [String]
23
+ # @param data [Hash] the JSON payload for this request
24
+ #
25
+ # @return [TranslationIO::API::Response]
26
+
27
+ def get(uri, data)
28
+ uri = URI.parse(uri)
29
+ req = Net::HTTP::Get.new(uri)
30
+
31
+ req["Content-Type"] = "application/json"
32
+ req["X-api-key"] = @api_key
33
+ req.body = data.to_json
34
+
35
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
36
+ http.request(req)
37
+ end
38
+
39
+ Response.new(res)
40
+ end
41
+
42
+ # Post request
43
+ #
44
+ # @param uri [String]
45
+ # @param data [Hash] the JSON payload for this request
46
+ #
47
+ # @return [TranslationIO::API::Response]
48
+
49
+ def post(uri, data)
50
+ uri = URI.parse(uri)
51
+ req = Net::HTTP::Post.new(uri)
52
+
53
+ req["Content-Type"] = "application/json"
54
+ req["X-api-key"] = @api_key
55
+ req.body = data.to_json
56
+
57
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
58
+ http.request(req)
59
+ end
60
+
61
+ Response.new(res)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module TranslationIO
6
+ module API
7
+ # Response represents a response from TranslationIO.
8
+ #
9
+ # It is returned when using methods in Request.
10
+ # @see TranslationIO::API::Request
11
+ class Response
12
+ attr_reader :raw_response
13
+
14
+ # Constructor
15
+ #
16
+ # @param response [Net::HTTPResponse] raw response from Net::HTTP
17
+ #
18
+ # @return [TranslationIO::API::Response
19
+
20
+ def initialize(response)
21
+ @raw_response = response
22
+ end
23
+
24
+ # Was the response code 2xx
25
+ #
26
+ # @return [Boolean]
27
+
28
+ def success?
29
+ @raw_response.is_a?(Net::HTTPSuccess)
30
+ end
31
+
32
+ # Integer representing the HTTP response code
33
+ #
34
+ # @return [Integer] HTTP response code
35
+
36
+ def code
37
+ Integer(@raw_response.code)
38
+ end
39
+
40
+ # Body of the response
41
+ #
42
+ # @return [Hash] response body
43
+
44
+ def body
45
+ return {} if @raw_response.body.strip.empty?
46
+
47
+ JSON.parse(@raw_response.body)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TranslationIO
4
+ module API
5
+ # Client for segment endpoints
6
+ class Segments
7
+ def initialize(api_key:)
8
+ @api_key = api_key
9
+ @base_url = "https://translation.io/api/v1/segments"
10
+ end
11
+
12
+ # List segments for target language
13
+ #
14
+ # @param target_language [String] Target language code (from
15
+ # https://translation.io/docs/languages). Example: "es"
16
+ # @param options [Hash] optional params to pass with the call to list
17
+ # @option options [String] :query Query segments to returns only the
18
+ # ones that contain query value in their key, source, target (with
19
+ # plurals), context, comment or references properties.
20
+ # Note: that query is case-insensitive.
21
+ # @option options [String] :type ("all") Filter by type of segments.
22
+ # Should be key, source or all.
23
+ # @option options [String] :state ("all") Filter by state of segments.
24
+ # Should be translated, untranslated or all.
25
+ # @option options [String] :tag Filter by tag. Only returns segments
26
+ # that are tagged with tag value.
27
+ #
28
+ # @return [TranslationIO::API::Response] response
29
+
30
+ def list(target_language, options = {})
31
+ request.get(
32
+ @base_url,
33
+ options.merge("target_language": target_language)
34
+ )
35
+ end
36
+
37
+ # Create a segment
38
+ #
39
+ # @param target_language [String] Target language code (from
40
+ # https://translation.io/docs/languages). Example: "es"
41
+ # @param type [String] Segment type. Should be "key" or "source".
42
+ # @param key [String] Key associated with the text to be translated.
43
+ # Only for "key" type. Example: "home.call_to_action.bottom"
44
+ # @param source [String] Source text. Example: "Click here to subscribe"
45
+ #
46
+ # @return [TranslationIO::API::Response] response containing created
47
+ # segment.
48
+
49
+ def create(target_language, type:, key:, source:)
50
+ request.post(
51
+ @base_url, {
52
+ target_language: target_language,
53
+ type: type,
54
+ key: key,
55
+ source: source
56
+ }
57
+ )
58
+ end
59
+
60
+ # Add a tag to a segment
61
+ #
62
+ # @param segment_id [String, Integer] The ID of the segment
63
+ # @param name [String] The name of the tag to add
64
+ #
65
+ # @note a unique color will be attributed to the tag. If this tag
66
+ # already exists in the project, the same color will be reused.
67
+ # @note if this tag is already added to the segment, nothing will change
68
+ # and no error will be triggered.
69
+ #
70
+ # @return [TranslationIO::API::Response] response containing updated
71
+ # segment
72
+
73
+ def add_tag(segment_id, name:)
74
+ request.post(@base_url + "/#{segment_id}/add_tag", { name: name })
75
+ end
76
+
77
+ # Remove a tag from a segment
78
+ #
79
+ # @param segment_id [String, Integer] The ID of the segment
80
+ # @param name [String] The name of the tag to remove
81
+ #
82
+ # @note if this tag doesn't exist or is not linked to this segment,
83
+ # nothing will change and no error will be triggered.
84
+ #
85
+ # @return [TranslationIO::API::Response] response containing updated
86
+ # segment
87
+
88
+ def remove_tag(segment_id, name:)
89
+ request.post(@base_url + "/#{segment_id}/remove_tag", { name: name })
90
+ end
91
+
92
+ private
93
+
94
+ def request
95
+ Request.new(api_key: @api_key)
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TranslationIO
4
+ module API
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/translation_io/api/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "translation_io-api"
5
+ spec.version = TranslationIO::API::VERSION
6
+ spec.authors = ["Ross Reinhardt"]
7
+ spec.email = ["rreinhardt9@gmail.com"]
8
+
9
+ spec.summary = %q{Client for translation.io REST API}
10
+ spec.homepage = "https://github.com/rreinhardt9/translation_io-api"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = spec.homepage
16
+ spec.metadata["changelog_uri"] = "https://github.com/rreinhardt9/translation_io-api/CHANGELOG.md"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "rubocop"
28
+ spec.add_development_dependency "webmock"
29
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: translation_io-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ross Reinhardt
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-06-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: webmock
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - rreinhardt9@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rubocop.yml"
50
+ - ".ruby-version"
51
+ - ".travis.yml"
52
+ - CHANGELOG.md
53
+ - CODE_OF_CONDUCT.md
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - bin/console
59
+ - bin/setup
60
+ - lib/translation_io/api.rb
61
+ - lib/translation_io/api/request.rb
62
+ - lib/translation_io/api/response.rb
63
+ - lib/translation_io/api/segments.rb
64
+ - lib/translation_io/api/version.rb
65
+ - translation_io-api.gemspec
66
+ homepage: https://github.com/rreinhardt9/translation_io-api
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ homepage_uri: https://github.com/rreinhardt9/translation_io-api
71
+ source_code_uri: https://github.com/rreinhardt9/translation_io-api
72
+ changelog_uri: https://github.com/rreinhardt9/translation_io-api/CHANGELOG.md
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 2.3.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.0.3
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Client for translation.io REST API
92
+ test_files: []