bt 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 21beb1575ec8202ceaa66d909cd4aa74ae598e8f
4
+ data.tar.gz: cf2f30a15bfe46618586c76b303bdc82e0a96f02
5
+ SHA512:
6
+ metadata.gz: a12ce47a2932f5143af50ae848be62eec38fd0ad43ac0f262beadf14c5789bee44e7b124a21caaa99e9fc1c461b32649529e564c89e0a47f625c3da3da46cea4
7
+ data.tar.gz: f7bf538b1c7e3b88dc54ea7a01a238ec92caa253b35e595090638a67cae28f6387c9c6e9d3ef61a99acae51ee9fcabb57bbe44b3aaf7d9ddca68bf8f94fd6b11
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ before_install: gem install bundler -v 1.10.5
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://ruby.taobao.org'
2
+
3
+ # Specify your gem's dependencies in bt.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 vau
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.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Bt
2
+
3
+ Bt makes it easy to use Baidu translator in your terminal. Just try it!
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ $ gem install bt
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```
14
+ bt 'from' 'to' 'text'
15
+ ```
16
+ or
17
+ ```
18
+ bt 'text'
19
+ ```
20
+
21
+ ## Example:
22
+
23
+ ```
24
+ bt en zh 'how are you'
25
+ => "你好吗"
26
+
27
+ bt '你为什么这么帅?'
28
+ => "Why are you so handsome?"
29
+
30
+ bt zh kor '你为什么这么帅?'
31
+ => 너 왜 이렇게 멋있니?
32
+ ```
33
+
34
+ ## Language codes:
35
+ * english - en
36
+ * polish - pl
37
+ * french - fr
38
+ * spanish - es
39
+ * slovakian - sk
40
+ * chinese - zh
41
+ * russian - ru
42
+ * korean - kor
43
+ * automatic source language detection - auto
44
+
45
+ [find all available language codes](http://developer.baidu.com/wiki/index.php?title=%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3%E9%A6%96%E9%A1%B5/%E7%99%BE%E5%BA%A6%E7%BF%BB%E8%AF%91/%E7%BF%BB%E8%AF%91API#.E8.AF.AD.E7.A7.8D.E7.BC.96.E7.A0.81)
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vaucn/bt. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
50
+
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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/bt ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib') unless $LOAD_PATH.include?(File.dirname(__FILE__) + '/../lib')
5
+
6
+ require 'bt'
7
+
8
+ begin
9
+ # find out the args user type
10
+ options = Bt::UserInputParser.new(ARGV).options
11
+ Bt::Main.new(options).translate
12
+ rescue Interrupt
13
+ STDERR.puts "\nBt: exiting due to user request"
14
+ exit 130
15
+ end
data/bt.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bt/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bt"
8
+ spec.version = Bt::VERSION
9
+ spec.authors = ["vaucn"]
10
+ spec.email = ["allenvae@gmail.com"]
11
+
12
+ spec.summary = %q{Baidu Translate in your terminal.}
13
+ spec.description = %q{Bt makes it easy to use baidu transltor in your terminal.}
14
+ spec.homepage = "https://github.com/vaucn/bt"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ # spec.bindir = "exe"
27
+ spec.executables = ["bt"]
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec"
33
+ spec.add_development_dependency "delegate_it"
34
+ end
data/data_fetcher.rb ADDED
@@ -0,0 +1 @@
1
+ datafetcher.rb
@@ -0,0 +1,11 @@
1
+ module Bt
2
+ module CanOutput
3
+ def self.included(klass)
4
+ klass.send(:extend, ::DelegateIt)
5
+ end
6
+
7
+ def output_manager
8
+ @output ||= Bt::OutputManager.new
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ #encoding: UTF-8
2
+ require 'net/http'
3
+ require 'uri'
4
+
5
+ module Bt
6
+ class DataFetcher
7
+ include CanOutput
8
+ delegate :display_no_internet_error, to: :output_manager
9
+
10
+ def initialize url
11
+ @url = url
12
+ end
13
+
14
+ def data
15
+ send_data
16
+ end
17
+
18
+ private
19
+
20
+ def send_data
21
+ uri = URI.parse @url
22
+ http = Net::HTTP.new(uri.host, uri.port)
23
+ request = Net::HTTP::Get.new(uri)
24
+ http.request(request)
25
+ rescue SocketError
26
+ display_no_internet_error
27
+ end
28
+ end
29
+ end
data/lib/bt/main.rb ADDED
@@ -0,0 +1,12 @@
1
+ module Bt
2
+ class Main
3
+ def initialize options
4
+ @options = options
5
+ end
6
+
7
+ def translate
8
+ translator = Bt::Translator.new(@options)
9
+ translator.call
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,57 @@
1
+ #encoding: UTF-8
2
+ module Bt
3
+ class OutputManager
4
+ def display_help_and_quit
5
+ puts <<-EOS
6
+ ======= 百度翻译 ======
7
+ 用法:
8
+ bt 'from' 'to' 'text'
9
+
10
+ bt 'text'
11
+
12
+ 例如:
13
+ bt en zh 'how are you'
14
+ => '你好吗?'
15
+
16
+ bt '你好吗?'
17
+ => 'how are you'
18
+
19
+ 选项:
20
+ -v - 版本
21
+ -h - 帮助
22
+
23
+ EOS
24
+ exit
25
+ end
26
+
27
+ def display_version_and_quit
28
+ puts "#{Bt::VERSION}"
29
+ exit
30
+ end
31
+
32
+ def display_no_internet_error
33
+ puts "出错了,请检查你的网络^^"
34
+ exit
35
+ end
36
+
37
+ def display_timeout_error
38
+ puts "糟糕,请求超时咯^^"
39
+ exit
40
+ end
41
+
42
+ def display_system_error
43
+ puts "糟糕,系统出错咯^^"
44
+ exit
45
+ end
46
+
47
+ def display_api_error
48
+ puts "请检查API KEY是否正确^^"
49
+ exit
50
+ end
51
+
52
+ def display_params_error
53
+ puts "参数错误~~"
54
+ exit
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,35 @@
1
+ require 'json'
2
+
3
+ module Bt
4
+ class ResponseHandler
5
+ include CanOutput
6
+
7
+ delegate :display_timeout_error, :display_system_error, :display_api_error, :display_params_error, to: :output_manager
8
+
9
+ def initialize text
10
+ @text = text
11
+ end
12
+
13
+ def call
14
+ results = JSON.parse(@text)
15
+ @text = results["trans_result"][0]["dst"]
16
+ puts @text
17
+ rescue
18
+ if results["error_code"] == "52001"
19
+ display_timeout_error
20
+ end
21
+
22
+ if results["error_code"] == "52002"
23
+ display_system_error
24
+ end
25
+
26
+ if results["error_code"] == "52003"
27
+ display_key_error
28
+ end
29
+
30
+ if results["error_code"] == "52004"
31
+ display_params_error
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,15 @@
1
+ module Bt
2
+ class Translator
3
+ def initialize options
4
+ @options = options
5
+ # initial url
6
+ @url = Bt::UrlConstructor.new(@options).url
7
+ end
8
+
9
+ def call
10
+ # fetch data then use ResponseHandler to handle it
11
+ response = Bt::DataFetcher.new(@url).data
12
+ @text = Bt::ResponseHandler.new(response.body).call
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ #encoding: UTF-8
2
+ module Bt
3
+ class UrlConstructor
4
+ include CanOutput
5
+ delegate :display_help_and_quit, to: :output_manager
6
+
7
+ # base url for Baidu Taranslate API
8
+ Host = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=Hb6NFkkAPueUIcfOYaWvVnVN"
9
+
10
+ def initialize options
11
+ @options = options
12
+ end
13
+
14
+ def url
15
+ # build url by it's api doc
16
+ # http://developer.baidu.com/wiki/index.php?title=%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3%E9%A6%96%E9%A1%B5/%E7%99%BE%E5%BA%A6%E7%BF%BB%E8%AF%91/%E7%BF%BB%E8%AF%91API
17
+ @url = "#{Host}&q=#{@options[:text]}&from=#{@options[:from]}&to=#{@options[:to]}"
18
+ rescue
19
+ puts "参数错误!"
20
+ display_help_and_quit
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,57 @@
1
+ #encoding: UTF-8
2
+ module Bt
3
+ class UserInputParser
4
+ include CanOutput
5
+
6
+ delegate :display_help_and_quit, :display_version_and_quit, to: :output_manager
7
+
8
+ def initialize user_input
9
+ @user_input = user_input
10
+ end
11
+
12
+ def options
13
+ check_input
14
+ end
15
+
16
+ private
17
+
18
+ def check_input
19
+ validate_args
20
+ quit_with_flags
21
+
22
+ case @user_input.length
23
+ when 1
24
+ auto_input
25
+ when 3
26
+ parse_input
27
+ end
28
+
29
+ end
30
+
31
+ def parse_input
32
+ {
33
+ from: @user_input.shift.to_sym,
34
+ to: @user_input.shift.to_sym,
35
+ text: CGI.escape(@user_input.shift)
36
+ }
37
+ end
38
+
39
+ def auto_input
40
+ {
41
+ from: 'auto',
42
+ to: 'auto',
43
+ text: CGI.escape(@user_input.shift)
44
+ }
45
+ end
46
+
47
+ def validate_args
48
+ raise ArgumentError unless @user_input.is_a? Array
49
+ raise ArgumentError unless @user_input.length >= 1
50
+ end
51
+
52
+ def quit_with_flags
53
+ display_help_and_quit if @user_input.index("-h") || @user_input.empty?
54
+ display_version_and_quit if @user_input.index("-v")
55
+ end
56
+ end
57
+ end
data/lib/bt/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Bt
2
+ VERSION = "0.1.0"
3
+ end
data/lib/bt.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'delegate_it'
2
+ require 'bt/can_output'
3
+ require 'bt/output_manager'
4
+ require 'bt/version'
5
+ require 'bt/data_fetcher'
6
+ require 'bt/main'
7
+ require 'bt/response_handler'
8
+ require 'bt/translator'
9
+ require 'bt/url_constructor'
10
+ require 'bt/user_input_parser'
11
+
12
+ module Bt
13
+ # Your code goes here...
14
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - vaucn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-10 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: delegate_it
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Bt makes it easy to use baidu transltor in your terminal.
70
+ email:
71
+ - allenvae@gmail.com
72
+ executables:
73
+ - bt
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/bt
86
+ - bt.gemspec
87
+ - data_fetcher.rb
88
+ - lib/bt.rb
89
+ - lib/bt/can_output.rb
90
+ - lib/bt/data_fetcher.rb
91
+ - lib/bt/main.rb
92
+ - lib/bt/output_manager.rb
93
+ - lib/bt/response_handler.rb
94
+ - lib/bt/translator.rb
95
+ - lib/bt/url_constructor.rb
96
+ - lib/bt/user_input_parser.rb
97
+ - lib/bt/version.rb
98
+ homepage: https://github.com/vaucn/bt
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.4.6
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Baidu Translate in your terminal.
122
+ test_files: []
123
+ has_rdoc: