wlt 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
+ SHA1:
3
+ metadata.gz: 8049e360e977622ad37d565010206d18bcc57131
4
+ data.tar.gz: c6d95f8a82c7cf6ad4c06a6071cb48610b8f6999
5
+ SHA512:
6
+ metadata.gz: 261e17e4a4214747a293b1b2714a2bae4e680ecdc56f33976115f767dc1e89ca43a87e0332f202b456384b734ff43c61c962e30c809242a4cf4999f530e61656
7
+ data.tar.gz: '06683dfad19e529147a70e3a79ee8c6e21ab4e89ecaaf52c45c4c056d43c25829b1f63c6695a319c7c5574b44f8c11aafc05e40c4ef224831301dc13dbeb3db4'
@@ -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,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Mauro Morales
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,67 @@
1
+ # Watson Language Translator gem
2
+
3
+ Wlt is a command line application and API to translate text using the Watson
4
+ Language Translator service.
5
+
6
+ Before you can use Wlt you need to create a set of credentials see:
7
+ https://console.bluemix.net/docs/services/watson/getting-started-credentials.html#getting-credentials-manually
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'wlt'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install wlt
24
+
25
+ ## Usage
26
+
27
+ You can do translations via the CLI or using the Wlt API
28
+
29
+ ### CLI
30
+
31
+ Translate a file:
32
+
33
+ $ wlt --username=<your-watson-username> --password=<your-watson-password> --file=path/to/file
34
+
35
+ Save your credentials locally in order not to pass them every time you want to do a translation:
36
+
37
+ $ wlt --username=<your-watson-username> --password=<your-watson-password> --save-credentials
38
+
39
+ Choose different source and target languages:
40
+
41
+ $ wlt --source=es --target=en --file=path/to/file
42
+
43
+ Use process substitution to pass your text directly in the command line:
44
+
45
+ $ wlt --file=<(echo "Hello world!")
46
+
47
+ Print out the help message
48
+
49
+ $ wlt --help
50
+
51
+ ### API
52
+
53
+ ```ruby
54
+ require 'wlt'
55
+
56
+ creds_manager = Wlt::CredsManager.new('<your-watson-username>', '<your-watson-password>')
57
+ translate_args = { text: 'Hello world!', source: 'en', target: 'es' }
58
+ result = Wlt::Translator.new(creds_manager).translate(translate_args)
59
+ ```
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mauromorales/wlt.
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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
data/bin/wlt ADDED
@@ -0,0 +1,78 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'wlt'
5
+
6
+
7
+ options = {}
8
+ translate_args = {}
9
+
10
+ parser = OptionParser.new do |parser|
11
+ parser.on("-u", "--username USERNAME", "The Watson username") do |v|
12
+ options[:username] = v
13
+ end
14
+
15
+ parser.on("-p", "--password PASSWORD", "The Watson password") do |v|
16
+ options[:password] = v
17
+ end
18
+
19
+ parser.on("--save-credentials", "Saves the given credentials in $HOME/.wlt/credentials") do
20
+ options[:save_credentials] = true
21
+ end
22
+
23
+ parser.on("-s", "--source LANG", "Source language (default: en)") do |v|
24
+ translate_args[:source] = v
25
+ end
26
+
27
+ parser.on("-t", "--target LANG", "Target language (default: es)") do |v|
28
+ translate_args[:target] = v
29
+ end
30
+
31
+ parser.on("-f", "--file PATH", "File containing text to be translated") do |v|
32
+ options[:file_path] = v
33
+ end
34
+
35
+ parser.on("-h", "--help", "Prints this message") do |v|
36
+ puts parser
37
+ exit
38
+ end
39
+ end
40
+
41
+ parser.parse!
42
+
43
+ creds_manager = Wlt::CredsManager.new
44
+
45
+ if options[:username] && options[:password]
46
+ creds_manager.username = options[:username]
47
+ creds_manager.password = options[:password]
48
+ else
49
+ begin
50
+ creds_manager.load
51
+ rescue Errno::ENOENT
52
+ puts "Error: Missing credentials"
53
+ puts parser
54
+ exit 1
55
+ end
56
+ end
57
+
58
+ if options[:save_credentials]
59
+ creds_manager.save
60
+ puts "Saved credentials into $HOME/.wlt/credentials"
61
+ exit
62
+ end
63
+
64
+ if options[:file_path]
65
+ begin
66
+ translate_args[:text] = File.read(options[:file_path])
67
+ rescue Errno::ENOENT
68
+ puts "Cannot open file #{options[:file_path]}"
69
+ exit 1
70
+ end
71
+
72
+ result = Wlt::Translator.new(creds_manager).translate(translate_args)
73
+ puts result
74
+ else
75
+ puts "Error: Missing file"
76
+ puts parser
77
+ exit 1
78
+ end
@@ -0,0 +1,8 @@
1
+ require 'wlt/version'
2
+ require 'wlt/creds_manager'
3
+ require 'wlt/translator'
4
+
5
+ module Wlt; end
6
+
7
+ class Error < RuntimeError; end
8
+ class CredsManagerError < Error; end
@@ -0,0 +1,46 @@
1
+ require 'fileutils'
2
+
3
+ module Wlt
4
+
5
+ class CredsManager
6
+ attr_accessor :username, :password
7
+
8
+ def initialize(username=nil, password=nil)
9
+ @username = username
10
+ @password = password
11
+ end
12
+
13
+ def save
14
+ validate
15
+
16
+ wlt_dir = File.join(Dir.home, '.wlt')
17
+ FileUtils.mkdir_p(wlt_dir)
18
+ File.open(File.join(wlt_dir, 'credentials'), 'w+') do |file|
19
+ file.write("WATSON_USERNAME=#{@username}\n")
20
+ file.write("WATSON_PASSWORD=#{@password}\n")
21
+ end
22
+ end
23
+
24
+ def load
25
+ File.open(File.join(Dir.home, '.wlt', 'credentials')) do |file|
26
+ file.each_line do |line|
27
+ username = line.match(/WATSON_USERNAME=(.*)/)
28
+ @username = username[1] if username
29
+ password = line.match(/WATSON_PASSWORD=(.*)/)
30
+ @password = password[1] if password
31
+ end
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def invalid_argument(name)
38
+ "'#{name}' cannot be empty. See: https://console.bluemix.net/docs/services/watson/getting-started-credentials.html#getting-credentials-manually"
39
+ end
40
+
41
+ def validate
42
+ raise CredsManagerError, 'Missing username' if @username.nil? || @username.empty?
43
+ raise CredsManagerError, 'Missing password' if @password.nil? || @password.empty?
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,26 @@
1
+ require 'net/http'
2
+
3
+ module Wlt
4
+ class Translator
5
+
6
+ def initialize(authenticator)
7
+ @authenticator = authenticator
8
+ end
9
+
10
+ def translate(text:, source: 'en', target: 'es')
11
+ uri = URI('https://gateway.watsonplatform.net/language-translator/api/v2/translate')
12
+ params = { source: source, target: target, text: text }
13
+ uri.query = URI.encode_www_form(params)
14
+
15
+ req = Net::HTTP::Get.new(uri)
16
+ req.basic_auth @authenticator.username, @authenticator.password
17
+
18
+ http = Net::HTTP.new(uri.hostname, uri.port)
19
+ http.use_ssl = true
20
+
21
+ res = http.request(req)
22
+
23
+ res.body
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Wlt
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "wlt/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wlt"
8
+ spec.version = Wlt::VERSION
9
+ spec.authors = ["Mauro Morales"]
10
+ spec.email = ["contact@mauromorales.com"]
11
+
12
+ spec.summary = %q{Watson Language Translator}
13
+ spec.description = %q{Wlt is a command line application and API to translate text using the Watson Language Translator service.}
14
+ spec.homepage = "https://github.com/mauromorales/wlt"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "bin"
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wlt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mauro Morales
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-12-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Wlt is a command line application and API to translate text using the
56
+ Watson Language Translator service.
57
+ email:
58
+ - contact@mauromorales.com
59
+ executables:
60
+ - wlt
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/wlt
72
+ - lib/wlt.rb
73
+ - lib/wlt/creds_manager.rb
74
+ - lib/wlt/translator.rb
75
+ - lib/wlt/version.rb
76
+ - wlt.gemspec
77
+ homepage: https://github.com/mauromorales/wlt
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.6.13
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Watson Language Translator
101
+ test_files: []