youdao-fanyi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'crack'
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # Youdao Fanyi #
2
+ This is a [Youdao Fanyi][YoudaoFanyi] API wrapper written in Ruby.
3
+
4
+ [Youdao Fanyi][YoudaoFanyi] is a translation service provided by Youdao.
5
+
6
+ ## Pre Job ##
7
+ Before coding, you should go to [Youdao Fanyi API][YoudaoFanyiAPI] for an API key.
8
+
9
+ **Caution**: Each key can only request less than 1000 times per hour.
10
+
11
+ ## Example ##
12
+
13
+ # coding: UTF-8
14
+ # example.rb
15
+ require 'youdao-fanyi'
16
+ # Configure the key first
17
+ YoudaoFanyi::Config.key_from = "youdao-fanyi"
18
+ YoudaoFanyi::Config.key = 1629987369
19
+ # Then use the 4 methods provided.
20
+ to_be_translated = "要翻译的词句"
21
+ YoudaoFanyi.search_json(to_be_translated) # returns a JSON string
22
+ YoudaoFanyi.search_xml(to_be_translated) # returns an XML string
23
+ YoudaoFanyi.search_jsonp(to_be_translated) # returns a JSONP string
24
+ YoudaoFanyi.search_result_obj(to_be_translated) # returns a YoudaoFanyi::Result object
25
+
26
+ ## About the API ##
27
+ [Youdao Fanyi API][YoudaoFanyiAPI] provides 3 types of data: *xml*, *json* and *jsonp*.
28
+
29
+ Here is an ***xml*** sample:
30
+
31
+ http://fanyi.youdao.com/fanyiapi.do?keyfrom=<keyfrom>&key=<key>&type=data&doctype=xml&version=1.1&q=这里是有道翻译API
32
+
33
+ <?xml version="1.0" encoding="UTF-8"?>
34
+ <youdao-fanyi>
35
+ <errorCode>0</errorCode>
36
+ <!-- 有道翻译 -->
37
+ <query><![CDATA[这里是有道翻译API]]></query>
38
+ <translation>
39
+ <paragraph><![CDATA[Here is the youdao translation API]]></paragraph>
40
+ </translation>
41
+ </youdao-fanyi>
42
+
43
+ Here is a ***json*** sample:
44
+
45
+ http://fanyi.youdao.com/fanyiapi.do?keyfrom=<keyfrom>&key=<key>&type=data&doctype=json&version=1.1&q=翻译
46
+
47
+ {
48
+ "errorCode":0
49
+ "query":"翻译",
50
+ "translation":["translation"], // 有道翻译
51
+ "basic":{ // 有道词典-基本词典
52
+ "phonetic":"fān yì",
53
+ "explains":[
54
+ "translate",
55
+ "interpret"
56
+ ]
57
+ },
58
+ "web":[ // 有道词典-网络释义
59
+ {
60
+ "key":"翻译",
61
+ "value":["translator","translation","translate","Interpreter"]
62
+ },
63
+ {...}
64
+ ]
65
+ }
66
+
67
+ Here is a ***jsonp*** sample:
68
+
69
+ http://fanyi.youdao.com/fanyiapi.do?keyfrom=<keyfrom>&key=<key>&type=data&doctype=jsonp&callback=show&version=1.1&q=API
70
+
71
+ show({
72
+ "errorCode":0
73
+ "query":"API",
74
+ "translation":["API"], // 有道翻译
75
+ "basic":{ // 有道词典-基本词典
76
+ "explains":[
77
+ "abbr. 应用程序界面(Application Program Interface);..."
78
+ ]
79
+ },
80
+ "web":[ // 有道词典-网络释义
81
+ {
82
+ "key":"API",
83
+ "value":["应用程序接口(Application Programming Interface)","应用编程接口","应用程序编程接口","美国石油协会"]
84
+ },
85
+ {...}
86
+ ]
87
+ });
88
+
89
+ ## Contributors ##
90
+ * ZHANG Hailong (https://github.com/zhhailon)
91
+
92
+
93
+ [YoudaoFanyi]: http://fanyi.youdao.com "Youdao Fanyi"
94
+ [YoudaoFanyiAPI]: http://fanyi.youdao.com/fanyiapi?path=data-mode "Youdao Fanyi API"
@@ -0,0 +1,61 @@
1
+ # Made for Youdao Fanyi (https://github.com/zhhailon/youdao-fanyi).
2
+ # Author: ZHANG Hailong <zhhailon@gmail.com>
3
+ # Date: Dev 1, 2011
4
+
5
+ require 'youdao-fanyi/config'
6
+ require 'youdao-fanyi/request'
7
+ require 'youdao-fanyi/result'
8
+
9
+ module YoudaoFanyi
10
+
11
+ # Query a word or sentence.
12
+ # Param: q The query word or sentence.
13
+ # Return: A String of doctype JSON.
14
+ def self.search_json(q)
15
+ if q.is_a?(String) && q.length > 0 && q.length <= 200
16
+ Request.get(q, :json)
17
+ else
18
+ puts "The query must be a String and its length should "+
19
+ "be between 1 to 200."
20
+ end
21
+ end
22
+
23
+ # Query a word or sentence.
24
+ # Param: q The query word or sentence.
25
+ # Return: A String of doctype XML.
26
+ def self.search_xml(q)
27
+ if q.is_a?(String) && q.length > 0 && q.length <= 200
28
+ Request.get(q, :xml)
29
+ else
30
+ puts "The query must be a String and its length should "+
31
+ "be between 1 to 200."
32
+ end
33
+ end
34
+
35
+ # Query a word or sentence.
36
+ # Param: q The query word or sentence.
37
+ # Return: A String of doctype JSONP.
38
+ def self.search_jsonp(q)
39
+ if q.is_a?(String) && q.length > 0 && q.length <= 200
40
+ Request.get(q, :jsonp)
41
+ else
42
+ puts "The query must be a String and its length should "+
43
+ "be between 1 to 200."
44
+ end
45
+ end
46
+
47
+ # Query a word or sentence.
48
+ # Param: q The query word or sentence.
49
+ # Return: A YoudaoFanyi::Result object.
50
+ def self.search_result_obj(q)
51
+ if q.is_a?(String) && q.length > 0 && q.length <= 200
52
+ Result.new(search_xml(q))
53
+ else
54
+ puts "The query must be a String and its length should "+
55
+ "be between 1 to 200."
56
+ end
57
+ end
58
+
59
+ class YoudaoFanyiError < StandardError; end
60
+
61
+ end
@@ -0,0 +1,25 @@
1
+ # Config the authorization.
2
+ # Go to http://fanyi.youdao.com/fanyiapi?path=data-mode for an API key.
3
+
4
+ module YoudaoFanyi
5
+
6
+ module Config
7
+
8
+ def self.key_from=(val)
9
+ @@key_from = val
10
+ end
11
+
12
+ def self.key_from
13
+ @@key_from
14
+ end
15
+
16
+ def self.key=(val)
17
+ @@key = val
18
+ end
19
+
20
+ def self.key
21
+ @@key
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,49 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ #require 'open-uri'
4
+
5
+ module YoudaoFanyi
6
+ class Request
7
+
8
+ def self.get(q, doctype)
9
+ new(q, doctype).request
10
+ end
11
+
12
+ def initialize(q, doctype)
13
+ @query = q
14
+ @doctype = doctype
15
+ end
16
+
17
+ def request
18
+ #open(url) do |f|
19
+ # f.read
20
+ #end
21
+ uri = URI.parse(url)
22
+ http = Net::HTTP.new(uri.host, uri.port)
23
+ request = Net::HTTP::Get.new(uri.request_uri)
24
+ response = http.request(request)
25
+ raise_errors(response)
26
+ response.body
27
+ end
28
+
29
+ private
30
+ # Return the URL
31
+ def url
32
+ url = "http://fanyi.youdao.com/fanyiapi.do?keyfrom=" +
33
+ YoudaoFanyi::Config.key_from + "&key=" +
34
+ YoudaoFanyi::Config.key.to_s + "&type=data&doctype=" +
35
+ @doctype.to_s
36
+ if @doctype == :jsonp
37
+ url = url + "&callback=show"
38
+ end
39
+ url = url + "&version=1.1&q=" + @query
40
+ URI.encode(url)
41
+ end
42
+
43
+ def raise_errors(response)
44
+ unless response.code.to_i == 200
45
+ raise YoudaoFanyiError.new, "(#{response.code}): #{response.message}"
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,22 @@
1
+ require 'crack'
2
+
3
+ module YoudaoFanyi
4
+ class Result
5
+ attr_reader :error_code, :query, :translation, :explains,
6
+ :web_explains
7
+
8
+ def initialize(xml_data)
9
+ data = parse(xml_data)["youdao_fanyi"]
10
+ @error_code = data["errorCode"]
11
+ @query = data["query"]
12
+ @translation = data["translation"]["paragraph"]
13
+ @explains = data["basic"]["explains"]["ex"]
14
+ @web_explains = data["web"]["explain"]
15
+ end
16
+
17
+ private
18
+ def parse(xml)
19
+ Crack::XML.parse(xml)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'youdao-fanyi'
3
+ s.version = "0.0.1"
4
+ s.authors = ["ZHANG Hailong"]
5
+ s.date = '2011-12-01'
6
+ s.summary = 'Youdao Fanyi (Translation) API Wrapper'
7
+ s.description = 'A gem to wrap the Youdao Fanyi (fanyi.youdao.com) API. "fanyi" is "translation" in Chinese.'
8
+ s.email = 'zhhailon@gmail.com'
9
+ s.homepage = 'https://github.com/zhhailon/youdao-fanyi'
10
+ s.files = [
11
+ "Gemfile",
12
+ "youdao-fanyi.gemspec",
13
+ "README.md",
14
+ "lib/youdao-fanyi.rb",
15
+ "lib/youdao-fanyi/config.rb",
16
+ "lib/youdao-fanyi/request.rb",
17
+ "lib/youdao-fanyi/result.rb"
18
+ ]
19
+ s.add_dependency('crack', [">= 0"])
20
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: youdao-fanyi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ZHANG Hailong
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-01 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: crack
16
+ requirement: &74031110 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *74031110
25
+ description: A gem to wrap the Youdao Fanyi (fanyi.youdao.com) API. "fanyi" is "translation"
26
+ in Chinese.
27
+ email: zhhailon@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - Gemfile
33
+ - youdao-fanyi.gemspec
34
+ - README.md
35
+ - lib/youdao-fanyi.rb
36
+ - lib/youdao-fanyi/config.rb
37
+ - lib/youdao-fanyi/request.rb
38
+ - lib/youdao-fanyi/result.rb
39
+ homepage: https://github.com/zhhailon/youdao-fanyi
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.11
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Youdao Fanyi (Translation) API Wrapper
63
+ test_files: []