vocabulary 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/vocabulary.rb +31 -11
- data/lib/vocabulary/version.rb +1 -1
- metadata +4 -4
data/lib/vocabulary.rb
CHANGED
@@ -2,16 +2,34 @@ require 'httparty'
|
|
2
2
|
require "vocabulary/version"
|
3
3
|
|
4
4
|
module Vocabulary
|
5
|
+
class LookUpError < ::StandardError
|
6
|
+
attr_accessor :summary
|
7
|
+
def initialize(summary)
|
8
|
+
super "Error while parsing Dictionary API response, may be you requested non-existent word. Response: #{summary}"
|
9
|
+
@summary = summary
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
5
13
|
class Word
|
6
14
|
attr_accessor :part_of_speech, :meanings
|
7
15
|
|
8
|
-
def initialize summary
|
9
|
-
|
10
|
-
|
16
|
+
def initialize summary, source_lang
|
17
|
+
begin
|
18
|
+
@meanings = []
|
19
|
+
|
20
|
+
if source_lang == :en
|
21
|
+
@part_of_speech = summary["primaries"][0]["terms"][0]["labels"][0]["text"]
|
22
|
+
collection = summary["primaries"][0]["entries"]
|
23
|
+
else
|
24
|
+
@part_of_speech = summary["primaries"][0]["entries"][0]["labels"][0]["text"]
|
25
|
+
collection = summary["primaries"][0]["entries"][0]["entries"]
|
26
|
+
end
|
11
27
|
|
12
|
-
|
13
|
-
|
14
|
-
|
28
|
+
collection.each do |meaning|
|
29
|
+
@meanings << meaning["terms"][0]["text"]
|
30
|
+
end
|
31
|
+
rescue
|
32
|
+
raise Vocabulary::LookUpError.new(summary)
|
15
33
|
end
|
16
34
|
end
|
17
35
|
end
|
@@ -28,8 +46,8 @@ module Vocabulary
|
|
28
46
|
options[:query] = {
|
29
47
|
:callback => "substr",
|
30
48
|
:q => word,
|
31
|
-
:sl => target_lang,
|
32
|
-
:tl => source_lang
|
49
|
+
:sl => target_lang, # source lang
|
50
|
+
:tl => source_lang # target lang
|
33
51
|
}
|
34
52
|
|
35
53
|
response = self.class.get("/dictionary/json", options)
|
@@ -43,7 +61,9 @@ module Vocabulary
|
|
43
61
|
|
44
62
|
def self.lookup(word, source_lang, target_lang = nil)
|
45
63
|
target_lang ||= :en
|
46
|
-
|
47
|
-
|
64
|
+
raise ArgumentError, "Word can't be blank" unless word.present?
|
65
|
+
data = Vocabulary::Dictionary.new.get(word, source_lang.to_s, target_lang.to_s)
|
66
|
+
# return data
|
67
|
+
Vocabulary::Word.new(data, source_lang)
|
48
68
|
end
|
49
|
-
end
|
69
|
+
end
|
data/lib/vocabulary/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vocabulary
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
12
|
+
date: 2011-11-28 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: httparty
|
16
|
-
requirement: &
|
16
|
+
requirement: &70110412886780 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70110412886780
|
25
25
|
description:
|
26
26
|
email:
|
27
27
|
- razor.psp@gmail.com
|