fanyi_api 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ff7d13c1eecc30e7b8079ad40b1d8746ab89514
4
- data.tar.gz: 6a8123cc48afd24a16a952cb2046adeacd371389
3
+ metadata.gz: 5acb20d9fb0e14a8cacd63ddee7ed7e869aec194
4
+ data.tar.gz: 590dd6f6532468c99e46ad38487ba9e55c72ee7a
5
5
  SHA512:
6
- metadata.gz: b6a44dfadfb3e5bbfd0ce2d4674d8a64c047af50e680525cafe1e734426d8851824356a86d2b80dc31b4ce4d50086f67666c2bdc35566096228cbf3fad793415
7
- data.tar.gz: e2440d50b2214944710b99838048476b8ea0ccd5ed71694f07b05bc715192ed2ddd67a2b0cbe87a50967066366420474275035d7424ec9aac0299bb3e8884bfd
6
+ metadata.gz: 0a18358323939d825777fdd444cb05dd4bd194861287532b4c779f34fac6f378c565ef0ca94a5e7e21ecc16af20a0bde9af17de711857f9704dadaf68848a678
7
+ data.tar.gz: e01e652c752e223a37c31925b32d3f4cbd87343d0a1e356a59a6db8425d291a3a47d6080b2b00f2404934d617ba6c8dbd3886ef3a97e0d9face62921e5e410d2
@@ -6,6 +6,7 @@ module FanyiAPI
6
6
 
7
7
  require_relative 'fanyi_api/strategies/default'
8
8
  require_relative 'fanyi_api/strategies/youdao'
9
+ require_relative 'fanyi_api/strategies/iciba'
9
10
 
10
11
  require_relative 'fanyi_api/api'
11
12
  require_relative 'fanyi_api/requester'
@@ -9,8 +9,7 @@ module FanyiAPI
9
9
 
10
10
  def call(query)
11
11
  raise InvalidQuery, "Invalid query" if query.blank?
12
- url = URI.encode(request_url)
13
- resp = conn.get request_url, q: query
12
+ resp = conn.get request_url, { query_key => query }
14
13
  parse resp
15
14
  end
16
15
 
@@ -2,6 +2,7 @@ module FanyiAPI
2
2
  module Strategies
3
3
 
4
4
  InvalidStrategy = Class.new ArgumentError
5
+ InvalidKey = Class.new ArgumentError
5
6
 
6
7
  class Default
7
8
 
@@ -0,0 +1,30 @@
1
+ module FanyiAPI
2
+ module Strategies
3
+
4
+ class Iciba < Default
5
+
6
+ def initialize(params={})
7
+ @options = {
8
+ host: "http://dict-co.iciba.com/api/dictionary.php",
9
+ type: "json"
10
+ }.merge(params)
11
+ end
12
+
13
+ def api_url
14
+ @api_url ||= "#{@options[:host]}?type=#{@options[:type]}"
15
+ end
16
+
17
+ def request_url
18
+ raise InvalidKey, "Key is not provided!" if @options[:key].nil?
19
+
20
+ @request_url ||= api_url + "&key=" + @options[:key]
21
+ end
22
+
23
+ def query_key
24
+ :w
25
+ end
26
+
27
+ end
28
+ end
29
+
30
+ end
@@ -1,8 +1,6 @@
1
1
  module FanyiAPI
2
2
  module Strategies
3
3
 
4
- InvalidKey = Class.new ArgumentError
5
-
6
4
  class Youdao < Default
7
5
 
8
6
  def initialize(params={})
@@ -25,6 +23,10 @@ module FanyiAPI
25
23
  @request_url ||= api_url + "&keyfrom=" + @options[:keyfrom] + "&key=" + @options[:key]
26
24
  end
27
25
 
26
+ def query_key
27
+ :q
28
+ end
29
+
28
30
  end
29
31
  end
30
32
 
@@ -1,5 +1,5 @@
1
1
  module FanyiAPI
2
2
 
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
 
5
5
  end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe FanyiAPI::Strategies::Iciba do
4
+
5
+ let(:strategy) { FanyiAPI::Strategies::Iciba.new(key: "key") }
6
+
7
+ it "returns api url" do
8
+ expect(strategy.api_url).to eq \
9
+ "http://dict-co.iciba.com/api/dictionary.php?type=json"
10
+ end
11
+
12
+ it "returns request url" do
13
+ expect(strategy.request_url).to eq \
14
+ "http://dict-co.iciba.com/api/dictionary.php?type=json&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::Iciba.new.request_url }.to \
21
+ raise_error(FanyiAPI::Strategies::InvalidKey)
22
+ end
23
+
24
+ end
25
+
26
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fanyi_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Qi He
@@ -48,12 +48,14 @@ files:
48
48
  - lib/fanyi_api/api.rb
49
49
  - lib/fanyi_api/requester.rb
50
50
  - lib/fanyi_api/strategies/default.rb
51
+ - lib/fanyi_api/strategies/iciba.rb
51
52
  - lib/fanyi_api/strategies/youdao.rb
52
53
  - lib/fanyi_api/version.rb
53
54
  - lib/fanyi_api.rb
54
55
  - spec/fanyi_api/api_spec.rb
55
56
  - spec/fanyi_api/requester_spec.rb
56
57
  - spec/fanyi_api/strategies/default_spec.rb
58
+ - spec/fanyi_api/strategies/iciba_spec.rb
57
59
  - spec/fanyi_api/strategies/youdao_spec.rb
58
60
  - spec/spec_helper.rb
59
61
  homepage: http://github.com/he9qi/fanyi_api
@@ -84,5 +86,6 @@ test_files:
84
86
  - spec/fanyi_api/api_spec.rb
85
87
  - spec/fanyi_api/requester_spec.rb
86
88
  - spec/fanyi_api/strategies/default_spec.rb
89
+ - spec/fanyi_api/strategies/iciba_spec.rb
87
90
  - spec/fanyi_api/strategies/youdao_spec.rb
88
91
  - spec/spec_helper.rb