nele-cli 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.md +28 -0
- data/README.md~ +24 -0
- data/Rakefile +7 -0
- data/bin/nele +11 -0
- data/build.sh +3 -0
- data/build.sh~ +3 -0
- data/lib/nele-cli.rb +94 -0
- data/lib/nele-cli.rb~ +94 -0
- data/lib/nele-cli/config.rb +50 -0
- data/lib/nele-cli/config.rb~ +47 -0
- data/lib/nele-cli/version.rb +5 -0
- data/lib/nele-cli/version.rb~ +5 -0
- data/nele-cli.gemspec +24 -0
- data/nele-cli.gemspec~ +24 -0
- data/test/cli_test.rb +132 -0
- data/test/cli_test.rb~ +127 -0
- data/test/config_test.rb +47 -0
- data/test/config_test.rb~ +40 -0
- data/test/tests_helper.rb +14 -0
- data/test/tests_helper.rb~ +14 -0
- data/test/ui_test.rb~ +11 -0
- metadata +91 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
Command line interface for nele gem
|
2
|
+
|
3
|
+
### Instalation:
|
4
|
+
|
5
|
+
$ gem install nele-cli
|
6
|
+
|
7
|
+
### Usage:
|
8
|
+
Generate config file placed in ~/.nele:<br/>
|
9
|
+
|
10
|
+
$ nele --create-config
|
11
|
+
|
12
|
+
Microsoft translator has been set as a default translator. Edit ~/.nele config file and add app id.
|
13
|
+
|
14
|
+
$ nele 'nice girl'
|
15
|
+
$ miła dziewczyna
|
16
|
+
|
17
|
+
$ nele --to es 'nice girl'
|
18
|
+
$ linda chica
|
19
|
+
|
20
|
+
You can specify all translator's options in params e.g:<br/>
|
21
|
+
|
22
|
+
$ nele -t ms --appId 5CE6C887658AB9698E1FB710C8F064F94646053B hello
|
23
|
+
$ Witaj
|
24
|
+
|
25
|
+
Switch to Yahoo's Babelfish translator:<br/>
|
26
|
+
|
27
|
+
$ nele -t babelfish hello
|
28
|
+
$ Hola
|
data/README.md~
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
Command line interface for nele gem
|
2
|
+
|
3
|
+
### Usage:
|
4
|
+
Generate config file placed in ~/.nele:<br/>
|
5
|
+
|
6
|
+
$ nele --create-config
|
7
|
+
|
8
|
+
Microsoft translator has been set as a default translator. Edit ~/.nele config file and add app id.
|
9
|
+
|
10
|
+
$ nele 'nice girl'
|
11
|
+
$ miła dziewczyna
|
12
|
+
|
13
|
+
$ nele --to es 'nice girl'
|
14
|
+
$ linda chica
|
15
|
+
|
16
|
+
You can specify all translator's options in params e.g:<br/>
|
17
|
+
|
18
|
+
$ nele -t ms --appId 5CE6C887658AB9698E1FB710C8F064F94646053B hello
|
19
|
+
$ Witaj
|
20
|
+
|
21
|
+
Switch to Yahoo's Babelfish translator:<br/>
|
22
|
+
|
23
|
+
$ nele -t babelfish hello
|
24
|
+
$ Hola
|
data/Rakefile
ADDED
data/bin/nele
ADDED
data/build.sh
ADDED
data/build.sh~
ADDED
data/lib/nele-cli.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'nele'
|
2
|
+
require 'yaml'
|
3
|
+
require 'nele-cli/config'
|
4
|
+
|
5
|
+
module Nele
|
6
|
+
class CLI
|
7
|
+
CONFIG_PATH = ENV['HOME'] + '/.nele'
|
8
|
+
attr_reader :translator, :query, :params
|
9
|
+
|
10
|
+
def initialize options
|
11
|
+
parse_options options
|
12
|
+
end
|
13
|
+
|
14
|
+
def translate
|
15
|
+
catch_exception do
|
16
|
+
configure_translator
|
17
|
+
print query.translate + "\n" if query
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def config
|
22
|
+
@config ||= Config.new(path)
|
23
|
+
end
|
24
|
+
|
25
|
+
def path
|
26
|
+
@path ||= CONFIG_PATH
|
27
|
+
end
|
28
|
+
|
29
|
+
def configure_translator
|
30
|
+
String.translator = translator
|
31
|
+
cfg = String.translator.config.merge translator_config
|
32
|
+
String.translator.config = cfg
|
33
|
+
end
|
34
|
+
|
35
|
+
def translator_config
|
36
|
+
@translator_config ||= config.translators[translator].merge params
|
37
|
+
end
|
38
|
+
|
39
|
+
def translator
|
40
|
+
@translator || config.default_translator.to_sym
|
41
|
+
end
|
42
|
+
|
43
|
+
def catch_exception &block
|
44
|
+
begin
|
45
|
+
yield
|
46
|
+
rescue Nele::ConnectionError
|
47
|
+
print "Connection error\n"
|
48
|
+
rescue Nele::TranslatorMissingError
|
49
|
+
print "Translator missing\n"
|
50
|
+
rescue Nele::TranslatorInvalidError
|
51
|
+
print "'#{translator}': invalid translator\n"
|
52
|
+
rescue Nele::TranslatorAPIError => err
|
53
|
+
print "API error: #{err}\n"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def parse_options options
|
60
|
+
unless options.empty?
|
61
|
+
@params = {}
|
62
|
+
options.each_with_index do |e,i|
|
63
|
+
_next = i+1
|
64
|
+
if e =~ /^-t$/
|
65
|
+
@translator = options.delete_at(_next).to_sym
|
66
|
+
elsif e =~ /^--create-config$/
|
67
|
+
@path = options.delete_at(_next) if options[_next]
|
68
|
+
Config.create path
|
69
|
+
elsif e =~ /^--help$/
|
70
|
+
help
|
71
|
+
elsif e =~ /^-c$/
|
72
|
+
@path = options.delete_at(_next)
|
73
|
+
elsif e =~ /^--[a-z]/
|
74
|
+
key = options[i].gsub('--', '')
|
75
|
+
value = options.delete_at(_next)
|
76
|
+
@params[key.to_sym] = value
|
77
|
+
else
|
78
|
+
@query = e
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def help
|
87
|
+
print "--create-config Generates config file in your home dir\n"
|
88
|
+
print "-t NAME Change translator\n"
|
89
|
+
exit(0)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
class CLIOptionError < StandardError; end
|
94
|
+
end
|
data/lib/nele-cli.rb~
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'ruby-debug'
|
2
|
+
require 'nele'
|
3
|
+
require 'yaml'
|
4
|
+
require 'nele-cli/config'
|
5
|
+
|
6
|
+
module Nele
|
7
|
+
class CLI
|
8
|
+
CONFIG_PATH = ENV['HOME'] + '/.nele'
|
9
|
+
attr_reader :translator, :query, :params
|
10
|
+
|
11
|
+
def initialize options
|
12
|
+
parse_options options
|
13
|
+
end
|
14
|
+
|
15
|
+
def translate
|
16
|
+
catch_exception do
|
17
|
+
configure_translator
|
18
|
+
print query.translate + "\n" if query
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def config
|
23
|
+
@config ||= Config.new(path)
|
24
|
+
end
|
25
|
+
|
26
|
+
def path
|
27
|
+
@path ||= CONFIG_PATH
|
28
|
+
end
|
29
|
+
|
30
|
+
def configure_translator
|
31
|
+
String.translator = translator
|
32
|
+
cfg = String.translator.config.merge translator_config
|
33
|
+
String.translator.config = cfg
|
34
|
+
end
|
35
|
+
|
36
|
+
def translator_config
|
37
|
+
@translator_config ||= config.translators[translator].merge params
|
38
|
+
end
|
39
|
+
|
40
|
+
def translator
|
41
|
+
@translator || config.default_translator.to_sym
|
42
|
+
end
|
43
|
+
|
44
|
+
def catch_exception &block
|
45
|
+
begin
|
46
|
+
yield
|
47
|
+
rescue Nele::ConnectionError
|
48
|
+
print "Connection error\n"
|
49
|
+
rescue Nele::TranslatorMissingError
|
50
|
+
print "Translator missing\n"
|
51
|
+
rescue Nele::TranslatorInvalidError
|
52
|
+
print "'#{translator}': invalid translator\n"
|
53
|
+
rescue Nele::TranslatorAPIError => err
|
54
|
+
print "API error: #{err}\n"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def parse_options options
|
61
|
+
unless options.empty?
|
62
|
+
@params = {}
|
63
|
+
options.each_with_index do |e,i|
|
64
|
+
_next = i+1
|
65
|
+
debugger
|
66
|
+
if e =~ /^-t$/
|
67
|
+
@translator = options.delete_at(_next).to_sym
|
68
|
+
elsif e =~ /^--create-config$/
|
69
|
+
@path = options.delete_at(_next) if options[_next]
|
70
|
+
Config.create path
|
71
|
+
elsif e =~ /^--help$/
|
72
|
+
help
|
73
|
+
exit(0)
|
74
|
+
elsif e =~ /^-c$/
|
75
|
+
@path = options.delete_at(_next)
|
76
|
+
elsif e =~ /^--[a-z]/
|
77
|
+
key = options[i].gsub('--', '')
|
78
|
+
value = options.delete_at(_next)
|
79
|
+
@params[key.to_sym] = value
|
80
|
+
else
|
81
|
+
@query = e
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def help
|
88
|
+
print "--create-config Generates config file in your home dir\n"
|
89
|
+
print "-t NAME Change translator\n"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
class CLIOptionError < StandardError; end
|
94
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Nele
|
2
|
+
class Config
|
3
|
+
attr_reader :path, :params
|
4
|
+
|
5
|
+
def initialize path
|
6
|
+
@path = path
|
7
|
+
@params = load_file
|
8
|
+
end
|
9
|
+
|
10
|
+
def translators
|
11
|
+
@translators ||= params[:translators]
|
12
|
+
end
|
13
|
+
|
14
|
+
def default_translator
|
15
|
+
@default_translator ||= params[:default]
|
16
|
+
end
|
17
|
+
|
18
|
+
class << self
|
19
|
+
def create path
|
20
|
+
File.open(path, 'w') { |f| YAML.dump file_template, f }
|
21
|
+
end
|
22
|
+
|
23
|
+
def file_template
|
24
|
+
{ :default => "ms",
|
25
|
+
:translators => {
|
26
|
+
:ms => {
|
27
|
+
:appId => "YOUR_KEY",
|
28
|
+
:from => "en",
|
29
|
+
:to => "pl",
|
30
|
+
:url => "http://api.microsofttranslator.com/v2/" \
|
31
|
+
"Http.svc/Translate"
|
32
|
+
},
|
33
|
+
:babelfish => {
|
34
|
+
:url => "http://babelfish.yahoo.com/translate_txt",
|
35
|
+
:lp => "en_es"
|
36
|
+
}
|
37
|
+
}
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def load_file
|
43
|
+
begin
|
44
|
+
@params ||= YAML.load_file path
|
45
|
+
rescue Errno::ENOENT
|
46
|
+
print "Can't find #{path}\n"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Nele
|
2
|
+
class Config
|
3
|
+
attr_reader :path, :params
|
4
|
+
|
5
|
+
def initialize path
|
6
|
+
@path = path
|
7
|
+
@params = load_file
|
8
|
+
end
|
9
|
+
|
10
|
+
def translators
|
11
|
+
@translators ||= params[:translators]
|
12
|
+
end
|
13
|
+
|
14
|
+
def default_translator
|
15
|
+
@default_translator ||= params[:default]
|
16
|
+
end
|
17
|
+
|
18
|
+
class << self
|
19
|
+
def create path
|
20
|
+
File.open(path, 'w') { |f| YAML.dump file_template, f }
|
21
|
+
end
|
22
|
+
|
23
|
+
def file_template
|
24
|
+
{ :default => "ms",
|
25
|
+
:translators => {
|
26
|
+
:ms => {
|
27
|
+
:appId => "YOUR_KEY",
|
28
|
+
:from => "en",
|
29
|
+
:to => "pl",
|
30
|
+
:url => "http://api.microsofttranslator.com/v2/" \
|
31
|
+
"Http.svc/Translate"
|
32
|
+
},
|
33
|
+
:babelfish => {
|
34
|
+
:url => "http://babelfish.yahoo.com/translate_txt",
|
35
|
+
:lp => "en_es"
|
36
|
+
}
|
37
|
+
}
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
def load_file
|
44
|
+
@params ||= YAML.load_file path
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/nele-cli.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
lib = File.expand_path('../lib/', __FILE__)
|
2
|
+
$:.unshift lib unless $:.include?(lib)
|
3
|
+
|
4
|
+
require 'nele-cli/version'
|
5
|
+
require 'bundler'
|
6
|
+
|
7
|
+
spec = Gem::Specification.new do |s|
|
8
|
+
s.name = "nele-cli"
|
9
|
+
s.summary = "command line interface for nele gem"
|
10
|
+
s.description= File.read(File.join(File.dirname(__FILE__), 'README.md'))
|
11
|
+
s.requirements =
|
12
|
+
[ 'An installed dictionary (most Unix systems have one)' ]
|
13
|
+
s.version = Nele::CLI::VERSION
|
14
|
+
s.author = "cfx"
|
15
|
+
s.email = "jozef@applicake.com"
|
16
|
+
s.homepage = "http://github.com/cfx/nele-cli"
|
17
|
+
s.platform = Gem::Platform::RUBY
|
18
|
+
s.required_ruby_version = '>=1.8.7'
|
19
|
+
s.files = Dir['**/**']
|
20
|
+
s.executables = Dir['bin/*'].map { |f| File.basename(f) }
|
21
|
+
s.test_files = Dir["test/test*.rb"]
|
22
|
+
s.has_rdoc = false
|
23
|
+
s.add_dependency('nele', '>= 0.2.2')
|
24
|
+
end
|
data/nele-cli.gemspec~
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
lib = File.expand_path('../lib/', __FILE__)
|
2
|
+
$:.unshift lib unless $:.include?(lib)
|
3
|
+
|
4
|
+
require 'nele-cli/version'
|
5
|
+
require 'bundler'
|
6
|
+
|
7
|
+
spec = Gem::Specification.new do |s|
|
8
|
+
s.name = "nele-cli"
|
9
|
+
s.summary = "command line interface for nele gem"
|
10
|
+
s.description= File.read(File.join(File.dirname(__FILE__), 'README.md'))
|
11
|
+
s.requirements =
|
12
|
+
[ 'An installed dictionary (most Unix systems have one)' ]
|
13
|
+
s.version = Nele::CLI::VERSION
|
14
|
+
s.author = "cfx"
|
15
|
+
s.email = "jozef@applicake.com"
|
16
|
+
s.homepage = "http://github.com/cfx/nele-cli"
|
17
|
+
s.platform = Gem::Platform::RUBY
|
18
|
+
s.required_ruby_version = '>=1.8.7'
|
19
|
+
s.files = Dir['**/**']
|
20
|
+
s.executables = Dir['bin/*'].map { |f| File.basename(f) }
|
21
|
+
s.test_files = Dir["test/test*.rb"]
|
22
|
+
s.has_rdoc = false
|
23
|
+
s.add_dependency('nele', '>= 0.0.2')
|
24
|
+
end
|
data/test/cli_test.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
require "./test/tests_helper"
|
2
|
+
require "nele-cli"
|
3
|
+
|
4
|
+
module Nele
|
5
|
+
describe "CLI" do
|
6
|
+
def setup
|
7
|
+
cleanup
|
8
|
+
Config.create filepath
|
9
|
+
String.translator = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def subject options=options
|
13
|
+
@subject ||= CLI.new options
|
14
|
+
end
|
15
|
+
|
16
|
+
def options
|
17
|
+
['-t', 'ms', '--from', 'xx', '--to', 'yy', '-c', filepath, "hello"]
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#path" do
|
21
|
+
it 'has default path set to "~/.nele"' do
|
22
|
+
subject = CLI.new [ 'hello' ]
|
23
|
+
subject.path.must_equal ENV['HOME'] + '/.nele'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#new" do
|
28
|
+
def expected_config subject=subject
|
29
|
+
subject.translator_config[:from].must_equal 'xx'
|
30
|
+
subject.translator_config[:to].must_equal 'yy'
|
31
|
+
end
|
32
|
+
|
33
|
+
it "creates config file in given path" do
|
34
|
+
cleanup
|
35
|
+
File.exist?(filepath).must_equal false
|
36
|
+
CLI.new ['--create-config', filepath]
|
37
|
+
File.exist?(filepath).must_equal true
|
38
|
+
end
|
39
|
+
|
40
|
+
it "extracts translator's name from options" do
|
41
|
+
subject.translator.must_equal :ms
|
42
|
+
end
|
43
|
+
|
44
|
+
it "extracts query from options" do
|
45
|
+
subject.query.must_equal "hello"
|
46
|
+
end
|
47
|
+
|
48
|
+
it "extracts translator's params from options" do
|
49
|
+
expected_config
|
50
|
+
end
|
51
|
+
|
52
|
+
it "doesn't matter when order is different" do
|
53
|
+
subject = CLI.new [
|
54
|
+
'-c', filepath,
|
55
|
+
'hello', '--to',
|
56
|
+
'yy', '-t', 'ms',
|
57
|
+
'--from', 'xx']
|
58
|
+
|
59
|
+
subject.configure_translator
|
60
|
+
expected_config(subject)
|
61
|
+
subject.query.must_equal "hello"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#translator" do
|
66
|
+
it 'returns always symbol' do
|
67
|
+
subject.translator.must_be_instance_of Symbol
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "when translator's name exist in params" do
|
71
|
+
it "attaches translator" do
|
72
|
+
subject = CLI.new ['-t', 'babelfish', '-c', filepath]
|
73
|
+
subject.configure_translator
|
74
|
+
String.translator.class.must_equal Nele::BabelfishTranslator
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "when translator's name doesn't exist in params" do
|
79
|
+
it "attaches default value from config file" do
|
80
|
+
subject = CLI.new ['-c', filepath]
|
81
|
+
subject.configure_translator
|
82
|
+
String.translator.class.must_equal Nele::MsTranslator
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe "#configure_translator" do
|
88
|
+
it "laods options from config file" do
|
89
|
+
subject = CLI.new ['-t', 'ms', '-c', filepath]
|
90
|
+
subject.configure_translator
|
91
|
+
|
92
|
+
String.translator.config[:from].must_equal "en"
|
93
|
+
String.translator.config[:to].must_equal "pl"
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "configuring translator based on params" do
|
97
|
+
it "overrides default translator config" do
|
98
|
+
subject.configure_translator
|
99
|
+
String.translator.config[:from].must_equal "xx"
|
100
|
+
String.translator.config[:to].must_equal "yy"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe 'catching exceptions' do
|
106
|
+
def subject
|
107
|
+
@subject ||= CLI.new ['-t', 'ms', '-c', filepath]
|
108
|
+
end
|
109
|
+
|
110
|
+
it "catches Nele::ConnectionError" do
|
111
|
+
subject.query.stubs(:translate).raises Nele::ConnectionError
|
112
|
+
subject.translate.must_be_nil
|
113
|
+
end
|
114
|
+
|
115
|
+
it "catches Nele::TranslatorMissingError" do
|
116
|
+
subject.query.stubs(:translate).raises Nele::TranslatorMissingError
|
117
|
+
subject.translate.must_be_nil
|
118
|
+
end
|
119
|
+
|
120
|
+
it "catches Nele::TranslatorInvalidError" do
|
121
|
+
subject.query.stubs(:translate).raises Nele::TranslatorInvalidError
|
122
|
+
subject.translate.must_be_nil
|
123
|
+
end
|
124
|
+
|
125
|
+
it "catches Nele::TranslatorAPIError" do
|
126
|
+
subject.query.stubs(:translate).raises Nele::TranslatorAPIError
|
127
|
+
subject.translate.must_be_nil
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
end
|
data/test/cli_test.rb~
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
require "./test/tests_helper"
|
2
|
+
require "nele-cli"
|
3
|
+
|
4
|
+
module Nele
|
5
|
+
describe "CLI" do
|
6
|
+
def setup
|
7
|
+
cleanup
|
8
|
+
Config.create filepath
|
9
|
+
String.translator = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def subject options=options
|
13
|
+
@subject ||= CLI.new options
|
14
|
+
end
|
15
|
+
|
16
|
+
def options
|
17
|
+
['-t', 'ms', '--from', 'xx', '--to', 'yy', '-c', filepath, "hello"]
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#path" do
|
21
|
+
it 'has default path set to "~/.nele"' do
|
22
|
+
subject = CLI.new [ 'hello' ]
|
23
|
+
subject.path.must_equal ENV['HOME'] + '/.nele'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#new" do
|
28
|
+
def expected_config subject=subject
|
29
|
+
subject.translator_config[:from].must_equal 'xx'
|
30
|
+
subject.translator_config[:to].must_equal 'yy'
|
31
|
+
end
|
32
|
+
|
33
|
+
it "creates config file in given path" do
|
34
|
+
cleanup
|
35
|
+
File.exist?(filepath).must_equal false
|
36
|
+
CLI.new ['--create-config', filepath]
|
37
|
+
File.exist?(filepath).must_equal true
|
38
|
+
end
|
39
|
+
|
40
|
+
it "extracts translator's name from options" do
|
41
|
+
subject.translator.must_equal :ms
|
42
|
+
end
|
43
|
+
|
44
|
+
it "extracts query from options" do
|
45
|
+
subject.query.must_equal "hello"
|
46
|
+
end
|
47
|
+
|
48
|
+
it "extracts translator's params from options" do
|
49
|
+
expected_config
|
50
|
+
end
|
51
|
+
|
52
|
+
it "doesn't matter when order is different" do
|
53
|
+
subject = CLI.new [
|
54
|
+
'-c', filepath,
|
55
|
+
'hello', '--to',
|
56
|
+
'yy', '-t', 'ms',
|
57
|
+
'--from', 'xx']
|
58
|
+
|
59
|
+
subject.configure_translator
|
60
|
+
expected_config(subject)
|
61
|
+
subject.query.must_equal "hello"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#translator" do
|
66
|
+
it 'returns always symbol' do
|
67
|
+
subject.translator.must_be_instance_of Symbol
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "when translator's name exist in params" do
|
71
|
+
it "attaches translator" do
|
72
|
+
subject = CLI.new ['-t', 'babelfish', '-c', filepath]
|
73
|
+
subject.configure_translator
|
74
|
+
String.translator.class.must_equal Nele::BabelfishTranslator
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "when translator's name doesn't exist in params" do
|
79
|
+
it "attaches default value from config file" do
|
80
|
+
subject = CLI.new ['-c', filepath]
|
81
|
+
subject.configure_translator
|
82
|
+
String.translator.class.must_equal Nele::MsTranslator
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe "#configure_translator" do
|
88
|
+
it "laods options from config file" do
|
89
|
+
subject = CLI.new ['-t', 'ms', '-c', filepath]
|
90
|
+
subject.configure_translator
|
91
|
+
|
92
|
+
String.translator.config[:from].must_equal "en"
|
93
|
+
String.translator.config[:to].must_equal "pl"
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "configuring translator based on params" do
|
97
|
+
it "overrides default translator config" do
|
98
|
+
subject.configure_translator
|
99
|
+
String.translator.config[:from].must_equal "xx"
|
100
|
+
String.translator.config[:to].must_equal "yy"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe 'catching exceptions' do
|
106
|
+
def subject
|
107
|
+
@subject ||= CLI.new ['-t', 'ms', '-c', filepath]
|
108
|
+
end
|
109
|
+
|
110
|
+
it "catches Nele::ConnectionError" do
|
111
|
+
subject.query.stubs(:translate).raises Nele::ConnectionError
|
112
|
+
subject.translate.must_be_nil
|
113
|
+
end
|
114
|
+
|
115
|
+
it "catches Nele::TranslatorMissingError " do
|
116
|
+
subject.query.stubs(:translate).raises Nele::TranslatorMissingError
|
117
|
+
subject.translate.must_be_nil
|
118
|
+
end
|
119
|
+
|
120
|
+
it "catches Nele::TranslatorInvalidError " do
|
121
|
+
subject.query.stubs(:translate).raises Nele::TranslatorInvalidError
|
122
|
+
subject.translate.must_be_nil
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
end
|
data/test/config_test.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "./test/tests_helper"
|
2
|
+
require "nele-cli"
|
3
|
+
|
4
|
+
module Nele
|
5
|
+
describe "Config" do
|
6
|
+
def subject
|
7
|
+
cleanup
|
8
|
+
Config.create filepath
|
9
|
+
@subject ||= Config.new filepath
|
10
|
+
end
|
11
|
+
|
12
|
+
it "creates config file if doesn't exist" do
|
13
|
+
cleanup
|
14
|
+
File.exist?(filepath).must_equal false
|
15
|
+
Config.create(filepath)
|
16
|
+
File.exist?(filepath).must_equal true
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#init" do
|
20
|
+
it 'catches exception when config file not found' do
|
21
|
+
YAML.stubs(:load_file).raises Errno::ENOENT
|
22
|
+
subject.load_file
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#params' do
|
27
|
+
it 'has /translators/ key' do
|
28
|
+
subject.params.has_key?(:translators).must_equal true
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'has /default/ key' do
|
32
|
+
subject.params.has_key?(:default).must_equal true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#translators' do
|
37
|
+
it 'has /ms/ key' do
|
38
|
+
subject.translators.has_key?(:ms).must_equal true
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'has /babelfish/ key' do
|
42
|
+
subject.translators.has_key?(:babelfish).must_equal true
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "./test/tests_helper"
|
2
|
+
require "nele-cli"
|
3
|
+
|
4
|
+
module Nele
|
5
|
+
describe "Config" do
|
6
|
+
def subject
|
7
|
+
Config.new CLI::CONFIG_PATH
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#init" do
|
11
|
+
it "creates config file if doesn't exist" do
|
12
|
+
cleanup
|
13
|
+
File.exist?(filepath).must_equal false
|
14
|
+
Config.create(filepath)
|
15
|
+
File.exist?(filepath).must_equal true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#params' do
|
20
|
+
it 'has /translators/ key' do
|
21
|
+
subject.params.has_key?(:translators).must_equal true
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'has /default/ key' do
|
25
|
+
subject.params.has_key?(:default).must_equal true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#translators' do
|
30
|
+
it 'has /ms/ key' do
|
31
|
+
subject.translators.has_key?(:ms).must_equal true
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'has /babelfish/ key' do
|
35
|
+
subject.translators.has_key?(:babelfish).must_equal true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'mocha'
|
5
|
+
require 'rubygems'
|
6
|
+
require 'ruby-debug'
|
7
|
+
|
8
|
+
def filepath
|
9
|
+
Dir.pwd + '/test/.nele'
|
10
|
+
end
|
11
|
+
|
12
|
+
def cleanup
|
13
|
+
`rm #{filepath}` if File.exist? filepath
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'mocha'
|
5
|
+
require 'rubygems'
|
6
|
+
require 'ruby-debug'
|
7
|
+
|
8
|
+
#def remove_config_file
|
9
|
+
# `rm #{ENV['HOME']}/.nele` if File.exist?(ENV['HOME'] + "/.nele")
|
10
|
+
# Nele::Config.presence?.must_equal false
|
11
|
+
#end
|
12
|
+
|
13
|
+
|
14
|
+
|
data/test/ui_test.rb~
ADDED
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nele-cli
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.2.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- cfx
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-12-13 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: nele
|
17
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.2.2
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *id001
|
26
|
+
description: "Command line interface for nele gem\n\n\
|
27
|
+
### Instalation:\n\n $ gem install nele-cli\n\n\
|
28
|
+
### Usage:\n\
|
29
|
+
Generate config file placed in ~/.nele:<br/>\n\n $ nele --create-config\n\n\
|
30
|
+
Microsoft translator has been set as a default translator. Edit ~/.nele config file and add app id.\n\n $ nele 'nice girl'\n $ mi\xC5\x82a dziewczyna\n\n $ nele --to es 'nice girl'\n $ linda chica\n\n\
|
31
|
+
You can specify all translator's options in params e.g:<br/>\n\n $ nele -t ms --appId 5CE6C887658AB9698E1FB710C8F064F94646053B hello\n $ Witaj\n\n\
|
32
|
+
Switch to Yahoo's Babelfish translator:<br/>\n\n $ nele -t babelfish hello\n $ Hola"
|
33
|
+
email: jozef@applicake.com
|
34
|
+
executables:
|
35
|
+
- nele
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files: []
|
39
|
+
|
40
|
+
files:
|
41
|
+
- bin/nele
|
42
|
+
- build.sh
|
43
|
+
- build.sh~
|
44
|
+
- lib/nele-cli/config.rb
|
45
|
+
- lib/nele-cli/config.rb~
|
46
|
+
- lib/nele-cli/version.rb
|
47
|
+
- lib/nele-cli/version.rb~
|
48
|
+
- lib/nele-cli.rb
|
49
|
+
- lib/nele-cli.rb~
|
50
|
+
- LICENSE
|
51
|
+
- nele-cli.gemspec
|
52
|
+
- nele-cli.gemspec~
|
53
|
+
- Rakefile
|
54
|
+
- README.md
|
55
|
+
- README.md~
|
56
|
+
- test/cli_test.rb
|
57
|
+
- test/cli_test.rb~
|
58
|
+
- test/config_test.rb
|
59
|
+
- test/config_test.rb~
|
60
|
+
- test/tests_helper.rb
|
61
|
+
- test/tests_helper.rb~
|
62
|
+
- test/ui_test.rb~
|
63
|
+
homepage: http://github.com/cfx/nele-cli
|
64
|
+
licenses: []
|
65
|
+
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 1.8.7
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: "0"
|
83
|
+
requirements:
|
84
|
+
- An installed dictionary (most Unix systems have one)
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.8.12
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: command line interface for nele gem
|
90
|
+
test_files:
|
91
|
+
- test/tests_helper.rb
|