dictionary_lookup 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -1
- data/lib/dictionary_lookup.rb +2 -2
- data/lib/dictionary_lookup/pearson.rb +22 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 629803c605ace4605aad3c5f4acc1c74ee34a91a
|
4
|
+
data.tar.gz: 7ae85ec0098a806397647320f568fb1b29783bdf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ac5bd92602355c61f1f9c02e4603ff8f7b285edebbda6c03108e81835ff83aeb2dd12d93ff3ba32b4da1ea71b51626fb6749b1d8af0a6d752782ed29321a6d0
|
7
|
+
data.tar.gz: 9eb97bb7109382ed2b7c9542da30607e21b8a63094c47d303845f57b621e55d25840fb8e8127e0aeb4b7e9ad8cbdaad70a3af310228944e6a392799caf54bf9b
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/dictionary_lookup.svg)](http://badge.fury.io/rb/dictionary_lookup)
|
4
4
|
[![Build Status](https://travis-ci.org/nitishparkar/dictionary-lookup-rb.svg?branch=master)](https://travis-ci.org/nitishparkar/dictionary-lookup-rb)
|
5
5
|
|
6
|
-
A ruby gem that wraps
|
6
|
+
A ruby gem that wraps [Pearson dictionary API](http://developer.pearson.com/apis/dictionaries)
|
7
7
|
|
8
8
|
## Getting started
|
9
9
|
|
@@ -37,3 +37,9 @@ results.first.part_of_speech # => "noun"
|
|
37
37
|
results.first.denotation # => "used as a greeting when you see or meet someone"
|
38
38
|
results.first.examples # => ["Hello, John! How are you?"]
|
39
39
|
```
|
40
|
+
|
41
|
+
You can specify different dictionary by passing `dictionary` in config hash. List of available dictionaries could be found [here](http://developer.pearson.com/apis/dictionaries#!//listDictionaryEntries)
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
results = DictionaryLookup::Base.define("hello", {dictionary: "wordwise"})
|
45
|
+
```
|
data/lib/dictionary_lookup.rb
CHANGED
@@ -4,13 +4,29 @@ require "dictionary_lookup/definition"
|
|
4
4
|
|
5
5
|
module DictionaryLookup
|
6
6
|
class Pearson
|
7
|
+
|
8
|
+
VALID_DICTIONARIES = ["ldoce5", "lasde", "ldec", "wordwise", "laesd", "leasd", "laad3", "laes", "lase", "brep", "brpe"]
|
9
|
+
|
10
|
+
DEFAULT_DICTIONARY = VALID_DICTIONARIES.first
|
11
|
+
|
12
|
+
# Helper method. Returns either a valid dictionary specified through config
|
13
|
+
# or the default dictionary
|
14
|
+
def self.get_dictionary(config)
|
15
|
+
VALID_DICTIONARIES.include?(config[:dictionary]) ?
|
16
|
+
config[:dictionary] : DEFAULT_DICTIONARY
|
17
|
+
end
|
18
|
+
|
7
19
|
# Fetches term definitions from Pearson dictionary API
|
8
20
|
#
|
9
21
|
# @param term [String] term to be defined
|
10
|
-
# @
|
22
|
+
# @param config [Hash] configuration hash, for now supports :dictionary key.
|
23
|
+
# For list of valid values see {DictionaryLookup::Pearson::VALID_DICTIONARIES}
|
24
|
+
# @return [Array] an array of {DictionaryLookup::Definition} objects
|
11
25
|
# @raise SocketError if not connected to the internet
|
12
|
-
def self.define(term)
|
13
|
-
|
26
|
+
def self.define(term, config)
|
27
|
+
dictionary = get_dictionary(config)
|
28
|
+
|
29
|
+
url = "https://api.pearson.com:443/v2/dictionaries/#{dictionary}/entries?headword=#{term}"
|
14
30
|
uri = URI(URI.escape(url))
|
15
31
|
|
16
32
|
response = Net::HTTP.get(uri)
|
@@ -23,7 +39,9 @@ module DictionaryLookup
|
|
23
39
|
|
24
40
|
results.each do |result|
|
25
41
|
part_of_speech = result["part_of_speech"]
|
26
|
-
|
42
|
+
# In wordwise dictionary, value of definition is String. Hence the is_a? test
|
43
|
+
_definitions = result["senses"].first["definition"]
|
44
|
+
denotation = _definitions.is_a?(Array) ? _definitions.first : _definitions
|
27
45
|
if result["senses"].first["examples"].nil?
|
28
46
|
examples = []
|
29
47
|
else
|