youdao_fanyi_api 0.1.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e6a2956b3a99d09d379cde70820fe9bae981d5b8
4
+ data.tar.gz: 486f5e54c41b7178926785b05909b5413bce3beb
5
+ SHA512:
6
+ metadata.gz: b6714b6f75c4461be9a81c12a0edca1c2d5082dd027d10c156d07bad6f2c2841b888debf47d40875a0ece27ee12ef10a8abae19cafdd2ac9d7bdde318dcc3121
7
+ data.tar.gz: 02324a439884febd22875895fdf2f0247ea3f0e9fa993544ac73616d0531224c7d985d6f561d61a5b3d9e88b88be52976f255550d673140a9aea7442d66e842c
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.swp
11
+ *.gem
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.6
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in youdao_fanyi_api.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,62 @@
1
+ # YoudaoFanyiAPI
2
+
3
+ A ruby wrapper for [youdao fanyi api][api].
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'youdao_fanyi_api'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install youdao_fanyi_api
20
+
21
+ ## Usage
22
+
23
+ First you need to configure the API
24
+
25
+ ```ruby
26
+ YoudaoFanyiAPI.configure do |config|
27
+ config.key = YOUR_KEY
28
+ config.keyfrom = YOUR_KEYFROM
29
+ end
30
+ ```
31
+
32
+ Then, you can use `translate` method.
33
+
34
+ ```ruby
35
+ puts YoudaoFanyiAPI.translate('hello world')['translation'] # 你好,世界
36
+ ```
37
+
38
+ You could use a client
39
+
40
+ ```ruby
41
+ client = YoudaoFanyiAPI::Client.new(key: YOUR_KEY, keyfrom: YOUR_KEYFROM)
42
+ puts client.t('ruby')['basic']['explains']
43
+ # n. 红宝石;红宝石色
44
+ # adj. 红宝石色的
45
+ # ...
46
+ ```
47
+
48
+ You can also use `t` method, which is an alias of `translate`. The `translate`
49
+ method returns a JSON object, you can find more details from [here][api].
50
+
51
+
52
+ ## Contributing
53
+
54
+ Feel free to file an issue or send a pr.
55
+
56
+
57
+
58
+
59
+
60
+
61
+
62
+ [api]: http://fanyi.youdao.com/openapi?path=data-mode
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "youdao_fanyi_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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+ require "youdao_fanyi_api/client"
2
+ require "youdao_fanyi_api/version"
3
+ require "youdao_fanyi_api/configuration"
4
+
5
+ module YoudaoFanyiAPI
6
+ extend YoudaoFanyiAPI::Configuration
7
+
8
+ class << self
9
+ def client
10
+ @client ||= Client.new
11
+ end
12
+
13
+ def method_missing(method, *args, &block)
14
+ return super unless client.respond_to?(method)
15
+ client.send(method, *args, &block)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,35 @@
1
+ require 'json'
2
+ require 'net/http'
3
+
4
+ module YoudaoFanyiAPI
5
+ class Client
6
+ PREFIX = 'http://fanyi.youdao.com/openapi.do?'
7
+
8
+ def initialize(user_options = {})
9
+ @options = YoudaoFanyiAPI.options.merge(user_options)
10
+ end
11
+
12
+ def translate(text)
13
+ http_response = get(text)
14
+ json_response = JSON.parse(http_response)
15
+ end
16
+
17
+ alias :t :translate
18
+
19
+ private
20
+
21
+ def post_form(h)
22
+ h.collect { |key, val| "#{key}=#{val}" }.join('&')
23
+ end
24
+
25
+ def uri(text)
26
+ escaped_text = URI.escape(text)
27
+ URI(PREFIX + post_form(@options.merge(q: escaped_text)))
28
+ end
29
+
30
+ def get(text)
31
+ query = uri(text)
32
+ Net::HTTP.get(query)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,32 @@
1
+ module YoudaoFanyiAPI
2
+ module Configuration
3
+ VALID_OPTIONS = %i[key keyfrom].freeze
4
+ DEFAULT_OPTIONS = %i[type doctype version].freeze
5
+ OPTIONS = VALID_OPTIONS + DEFAULT_OPTIONS
6
+ DEFAULT_TYPE = 'data'
7
+ DEFAULT_DOCTYPE = 'json'
8
+ DEFAULT_VERSION = '1.1'
9
+
10
+ attr_accessor *OPTIONS
11
+
12
+ def self.extended(base)
13
+ base.reset
14
+ end
15
+
16
+ def reset
17
+ self.key = nil
18
+ self.keyfrom = nil
19
+ self.type = DEFAULT_TYPE
20
+ self.doctype = DEFAULT_DOCTYPE
21
+ self.version = DEFAULT_VERSION
22
+ end
23
+
24
+ def configure
25
+ yield self
26
+ end
27
+
28
+ def options
29
+ OPTIONS.collect { |key| [key, send(key)] }.to_h
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module YoudaoFanyiAPI
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'youdao_fanyi_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "youdao_fanyi_api"
8
+ spec.version = YoudaoFanyiAPI::VERSION
9
+ spec.authors = ["delta"]
10
+ spec.email = ["delta4d@gmail.com"]
11
+ spec.license = 'MIT'
12
+
13
+ spec.summary = %q{a ruby wrapper for youdao fanyi api}
14
+ spec.description = %q{a ruby wrapper for youdao fanyi api}
15
+ spec.homepage = "https://github.com/delta4d/youdao_fanyi_api"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.10"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: youdao_fanyi_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - delta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-02 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
+ description: a ruby wrapper for youdao fanyi api
42
+ email:
43
+ - delta4d@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - Gemfile
51
+ - LICENSE
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - lib/youdao_fanyi_api.rb
57
+ - lib/youdao_fanyi_api/client.rb
58
+ - lib/youdao_fanyi_api/configuration.rb
59
+ - lib/youdao_fanyi_api/version.rb
60
+ - youdao_fanyi_api.gemspec
61
+ homepage: https://github.com/delta4d/youdao_fanyi_api
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.2.3
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: a ruby wrapper for youdao fanyi api
85
+ test_files: []
86
+ has_rdoc: