youdao-translate 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 163db4ee09cf33010d8731e0a1628bece3ae671f9c5c9d251f9210df147d4154
4
+ data.tar.gz: e67e8f9932e919e133e3ff5e7a24c5c0069fa874f89f693ba959b3bb16dd3334
5
+ SHA512:
6
+ metadata.gz: ad2ebc94f688b5d6a2f51004077d9baccb9e15ecd42ef70faaff614878fc4ea50e8cc140c2ffee346a28ae2c4686b50d49544f079817e4daf3b17746b31be15e
7
+ data.tar.gz: 7c84153cec840297ee5e7d23856c9e1403d8de0b9caa35d364815fce0f0992c9bc2ea62ac43518630401f11ed3c021b38362c49c05f404566508e8e0d7590e10
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in youdao-translate.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,19 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ youdao-translate (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (13.0.6)
10
+
11
+ PLATFORMS
12
+ x86_64-darwin-19
13
+
14
+ DEPENDENCIES
15
+ rake (~> 13.0)
16
+ youdao-translate!
17
+
18
+ BUNDLED WITH
19
+ 2.3.8
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Youdao::Translate
2
+
3
+ https://ai.youdao.com/product-fanyi-text.s
4
+
5
+ translate using youdao API
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'youdao-translate'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install youdao-translate
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ Translate.get("hello")
27
+
28
+ Translate.get("hello", from: "zh-CHS", to: "ja")
29
+
30
+ Translate.get("hello", to: "ko")
31
+
32
+ Translate.instance.get("hello")
33
+
34
+ Translate.instance.get("hello", from: "zh-CHS", to: "ja")
35
+ ```
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/youdao-translate.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Youdao
4
+ module Translate
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Translate
4
+ class Youdao
5
+ include Singleton
6
+
7
+ URL = "https://aidemo.youdao.com/trans"
8
+
9
+ class << self
10
+ extend Forwardable
11
+ delegate %i[get] => :instance
12
+ end
13
+
14
+ # Get a tranlsated string
15
+ #
16
+ # @example
17
+ # Translate::Youdao.get("hello")
18
+ #
19
+ # Translate::Youdao.get("hello", from: "zh-CHS", to: "ja")
20
+ #
21
+ # Translate::Youdao.get("hello", to: "ko")
22
+ #
23
+ # Translate::Youdao.instance.get("hello")
24
+ #
25
+ # Translate::Youdao.instance.get("hello", from: "zh-CHS", to: "ja")
26
+ #
27
+ # @param [String] content
28
+ # the content you want to translate
29
+ # @param [String] from
30
+ # the language from
31
+ #
32
+ # @param [String] to
33
+ # the language translate to
34
+ #
35
+ # @api public
36
+
37
+ def get(content, options={})
38
+ @content = content
39
+
40
+ form = payload.merge(options)
41
+
42
+ res = HTTP.post(URL, form: form)
43
+ result = res.parse :json
44
+
45
+ result["translation"].first
46
+ rescue StandardError
47
+ nil
48
+ end
49
+
50
+ private
51
+
52
+ attr_accessor :content
53
+
54
+ def payload
55
+ {
56
+ q: content,
57
+ from: "auto",
58
+ to: "en"
59
+ }
60
+ end
61
+ end
62
+ end
63
+
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "translate/version"
4
+ require_relative "translate/youdao"
5
+
6
+ module Youdao
7
+ module Translate
8
+ class Error < StandardError; end
9
+ # Your code goes here...
10
+ end
11
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/youdao/translate/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "youdao-translate"
7
+ spec.version = Youdao::Translate::VERSION
8
+ spec.authors = ["hfpp2012"]
9
+ spec.email = ["hfpp2012@gmail.com"]
10
+
11
+ spec.summary = "Translate using youdao"
12
+ spec.description = "Translate using youdao"
13
+ spec.homepage = "https://github.com/hfpp2012/youdao-translate"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
24
+ end
25
+ end
26
+ spec.require_paths = ["lib"]
27
+
28
+ # Uncomment to register a new dependency of your gem
29
+ # spec.add_dependency "example-gem", "~> 1.0"
30
+
31
+ # For more information and examples about making a new gem, check out our
32
+ # guide at: https://bundler.io/guides/creating_gem.html
33
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: youdao-translate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hfpp2012
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-03-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Translate using youdao
14
+ email:
15
+ - hfpp2012@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Gemfile
21
+ - Gemfile.lock
22
+ - README.md
23
+ - Rakefile
24
+ - lib/youdao/translate.rb
25
+ - lib/youdao/translate/version.rb
26
+ - lib/youdao/translate/youdao.rb
27
+ - youdao-translate.gemspec
28
+ homepage: https://github.com/hfpp2012/youdao-translate
29
+ licenses: []
30
+ metadata:
31
+ homepage_uri: https://github.com/hfpp2012/youdao-translate
32
+ source_code_uri: https://github.com/hfpp2012/youdao-translate
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 2.6.0
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 3.2.3
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Translate using youdao
52
+ test_files: []