fanyi_api 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/fanyi_api/api.rb +27 -0
- data/lib/fanyi_api/requester.rb +32 -0
- data/lib/fanyi_api/strategies/default.rb +15 -0
- data/lib/fanyi_api/strategies/youdao.rb +31 -0
- data/lib/fanyi_api/version.rb +5 -0
- data/lib/fanyi_api.rb +13 -0
- data/spec/fanyi_api/api_spec.rb +34 -0
- data/spec/fanyi_api/requester_spec.rb +25 -0
- data/spec/fanyi_api/strategies/default_spec.rb +14 -0
- data/spec/fanyi_api/strategies/youdao_spec.rb +26 -0
- data/spec/spec_helper.rb +3 -0
- metadata +88 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3ff7d13c1eecc30e7b8079ad40b1d8746ab89514
|
4
|
+
data.tar.gz: 6a8123cc48afd24a16a952cb2046adeacd371389
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b6a44dfadfb3e5bbfd0ce2d4674d8a64c047af50e680525cafe1e734426d8851824356a86d2b80dc31b4ce4d50086f67666c2bdc35566096228cbf3fad793415
|
7
|
+
data.tar.gz: e2440d50b2214944710b99838048476b8ea0ccd5ed71694f07b05bc715192ed2ddd67a2b0cbe87a50967066366420474275035d7424ec9aac0299bb3e8884bfd
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module FanyiAPI
|
2
|
+
module API
|
3
|
+
|
4
|
+
StrategyNotRegistered = Class.new StandardError
|
5
|
+
|
6
|
+
extend self
|
7
|
+
|
8
|
+
# register a strategy
|
9
|
+
def register(strategy, options={})
|
10
|
+
stra_sym = :"@#{strategy}"
|
11
|
+
stra = instance_variable_get stra_sym
|
12
|
+
if stra.nil?
|
13
|
+
strategy_class = eval("FanyiAPI::Strategies::#{strategy.camel_case}")
|
14
|
+
instance_variable_set stra_sym, strategy_class.new(options).extend(FanyiAPI::Requester)
|
15
|
+
define_method(strategy) { instance_variable_get stra_sym }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# call strategy with action and arguments
|
20
|
+
def call(strategy="youdao", query)
|
21
|
+
stra = instance_variable_get :"@#{strategy}"
|
22
|
+
raise StrategyNotRegistered if stra.nil?
|
23
|
+
stra.(query)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module FanyiAPI
|
2
|
+
module Requester
|
3
|
+
|
4
|
+
InvalidQuery = Class.new ArgumentError
|
5
|
+
|
6
|
+
def conn
|
7
|
+
@conn ||= Faraday.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(query)
|
11
|
+
raise InvalidQuery, "Invalid query" if query.blank?
|
12
|
+
url = URI.encode(request_url)
|
13
|
+
resp = conn.get request_url, q: query
|
14
|
+
parse resp
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
# get response body, if the body is nil or we fail
|
20
|
+
# to parse the body, return nil
|
21
|
+
def parse(resp)
|
22
|
+
resp = resp[0] if resp.is_a?(Array)
|
23
|
+
resp = resp.body
|
24
|
+
begin
|
25
|
+
JSON.parse resp
|
26
|
+
rescue JSON::ParserError, TypeError
|
27
|
+
nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module FanyiAPI
|
2
|
+
module Strategies
|
3
|
+
|
4
|
+
InvalidKey = Class.new ArgumentError
|
5
|
+
|
6
|
+
class Youdao < Default
|
7
|
+
|
8
|
+
def initialize(params={})
|
9
|
+
@options = {
|
10
|
+
host: "http://fanyi.youdao.com/openapi.do",
|
11
|
+
version: "1.1",
|
12
|
+
doctype: "json",
|
13
|
+
type: "data"
|
14
|
+
}.merge(params)
|
15
|
+
end
|
16
|
+
|
17
|
+
def api_url
|
18
|
+
@api_url ||= "#{@options[:host]}?type=#{@options[:type]}&doctype=#{@options[:doctype]}&version=#{@options[:version]}"
|
19
|
+
end
|
20
|
+
|
21
|
+
def request_url
|
22
|
+
raise InvalidKey, "Key is not provided!" \
|
23
|
+
if @options[:keyfrom].nil? || @options[:key].nil?
|
24
|
+
|
25
|
+
@request_url ||= api_url + "&keyfrom=" + @options[:keyfrom] + "&key=" + @options[:key]
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/lib/fanyi_api.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module FanyiAPI
|
2
|
+
require 'faraday'
|
3
|
+
require 'extlib'
|
4
|
+
require 'ostruct'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
require_relative 'fanyi_api/strategies/default'
|
8
|
+
require_relative 'fanyi_api/strategies/youdao'
|
9
|
+
|
10
|
+
require_relative 'fanyi_api/api'
|
11
|
+
require_relative 'fanyi_api/requester'
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FanyiAPI::API do
|
4
|
+
|
5
|
+
describe "#call" do
|
6
|
+
|
7
|
+
it "raise strategy not registered error" do
|
8
|
+
expect { FanyiAPI::API.("ciba", "hello") }.to \
|
9
|
+
raise_error(FanyiAPI::API::StrategyNotRegistered)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#registed strategy" do
|
13
|
+
|
14
|
+
before do
|
15
|
+
FanyiAPI::API.register "youdao", key: "key", keyfrom: "keyfrom"
|
16
|
+
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
|
17
|
+
stub.get("/url") { [200, {}, 'foo'] }
|
18
|
+
end
|
19
|
+
allow(Faraday).to receive(:new).and_return(stubs)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "calls youdao" do
|
23
|
+
expect(FanyiAPI::API.youdao).to \
|
24
|
+
receive(:call).with("hello").and_return("/url")
|
25
|
+
|
26
|
+
FanyiAPI::API.("youdao", "hello")
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FanyiAPI::Requester do
|
4
|
+
|
5
|
+
let(:request_url) { "http://fanyi.youdao.com/openapi.do?type=data&doctype=json&version=1.1&keyfrom=keyfrom&key=key" }
|
6
|
+
let(:youdao) { FanyiAPI::Strategies::Youdao.new key: "key", keyfrom: "keyfrom" }
|
7
|
+
let(:requester) { youdao.extend(FanyiAPI::Requester) }
|
8
|
+
let(:conn) { requester.conn }
|
9
|
+
let(:file) { File.read File.join(File.expand_path(File.dirname(__FILE__)), "../fixtures/youdao.json") }
|
10
|
+
let(:response) { OpenStruct.new body: file }
|
11
|
+
|
12
|
+
describe "#fanyi" do
|
13
|
+
|
14
|
+
it "calls only when find is present" do
|
15
|
+
expect(conn).to receive(:get).with(request_url, {:q=>"hello"}).and_return(response)
|
16
|
+
requester.("hello")
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'returns error if strategy do not respond to the action' do
|
20
|
+
expect{ requester.find_unkown }.to raise_error
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FanyiAPI::Strategies::Default do
|
4
|
+
|
5
|
+
describe "#raise_error" do
|
6
|
+
|
7
|
+
it "raise key not found if type is not provided" do
|
8
|
+
expect { FanyiAPI::Strategies::Default.new.request_url }.to \
|
9
|
+
raise_error(FanyiAPI::Strategies::InvalidStrategy)
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FanyiAPI::Strategies::Youdao do
|
4
|
+
|
5
|
+
let(:strategy) { FanyiAPI::Strategies::Youdao.new(key: "key", keyfrom: "keyfrom") }
|
6
|
+
|
7
|
+
it "returns api url" do
|
8
|
+
expect(strategy.api_url).to eq \
|
9
|
+
"http://fanyi.youdao.com/openapi.do?type=data&doctype=json&version=1.1"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns request url" do
|
13
|
+
expect(strategy.request_url).to eq \
|
14
|
+
"http://fanyi.youdao.com/openapi.do?type=data&doctype=json&version=1.1&keyfrom=keyfrom&key=key"
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#raise_error" do
|
18
|
+
|
19
|
+
it "raise key not found if type is not provided" do
|
20
|
+
expect { FanyiAPI::Strategies::Youdao.new.request_url }.to \
|
21
|
+
raise_error(FanyiAPI::Strategies::InvalidKey)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fanyi_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Qi He
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.9.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.9.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: extlib
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.9.16
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.9.16
|
41
|
+
description: |2
|
42
|
+
An English to Chinese translation tool using open APIs, such as Youdao, iCiba, etc.
|
43
|
+
email: qhe@heyook.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- lib/fanyi_api/api.rb
|
49
|
+
- lib/fanyi_api/requester.rb
|
50
|
+
- lib/fanyi_api/strategies/default.rb
|
51
|
+
- lib/fanyi_api/strategies/youdao.rb
|
52
|
+
- lib/fanyi_api/version.rb
|
53
|
+
- lib/fanyi_api.rb
|
54
|
+
- spec/fanyi_api/api_spec.rb
|
55
|
+
- spec/fanyi_api/requester_spec.rb
|
56
|
+
- spec/fanyi_api/strategies/default_spec.rb
|
57
|
+
- spec/fanyi_api/strategies/youdao_spec.rb
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
homepage: http://github.com/he9qi/fanyi_api
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.0.14
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: English to Chinese Translation APIs Wrapper in Ruby.
|
83
|
+
test_files:
|
84
|
+
- spec/fanyi_api/api_spec.rb
|
85
|
+
- spec/fanyi_api/requester_spec.rb
|
86
|
+
- spec/fanyi_api/strategies/default_spec.rb
|
87
|
+
- spec/fanyi_api/strategies/youdao_spec.rb
|
88
|
+
- spec/spec_helper.rb
|