free_dictionary_api 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +59 -0
- data/lib/free_dictionary_api/version.rb +3 -0
- data/lib/free_dictionary_api/word.rb +33 -0
- data/lib/free_dictionary_api.rb +43 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 32c267cbbe88cdf72ef360bef124776f2374c5cdb812948f534792b31081cc2a
|
4
|
+
data.tar.gz: 57f87b53a62f638ff1d96996a5962609ac315f2bb7c11d57195c4239cdcb6710
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bdcd6366d5b330dee4511ce2fd79a66c521edf503f0eb27cf1fff0e01d18114e59c24ffedba1dc87a861b3140116a58e5f425eccd820fd9ba95280e54053bd31
|
7
|
+
data.tar.gz: 06f95c0aec6f9adecc609fa1d23edc5baec1436010082db2b20f9c43f0c11bd45578344fac4e6e72068484a5b3c04d9221f58784f83743a99db9a2d6b96e9370
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Ben D'Angelo
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Free Dictionary API
|
2
|
+
|
3
|
+
A ruby gem to interface with the [Free Dictionary API](https://github.com/meetDeveloper/freeDictionaryAPI).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```
|
8
|
+
gem install free_dictionary_api
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```
|
14
|
+
# Initialize the client
|
15
|
+
client = FreeDictionaryApi::Client.new
|
16
|
+
|
17
|
+
# Look up a word
|
18
|
+
results = client.lookup('hello')
|
19
|
+
|
20
|
+
# Access the first result
|
21
|
+
word = results.first
|
22
|
+
|
23
|
+
# Get basic information
|
24
|
+
puts word.word # => "hello"
|
25
|
+
puts word.phonetic # => "həˈləʊ"
|
26
|
+
puts word.origin # => "early 19th century: variant of earlier hollo..."
|
27
|
+
|
28
|
+
# Get all definitions
|
29
|
+
word.definitions.each do |definition|
|
30
|
+
puts definition['definition']
|
31
|
+
end
|
32
|
+
|
33
|
+
# Get parts of speech
|
34
|
+
puts word.parts_of_speech # => ["exclamation", "noun", "verb"]
|
35
|
+
|
36
|
+
# Get examples
|
37
|
+
puts word.examples # => ["hello there, Katie!", ...]
|
38
|
+
|
39
|
+
# Get synonyms and antonyms
|
40
|
+
puts word.synonyms
|
41
|
+
puts word.antonyms
|
42
|
+
|
43
|
+
# Works with multi-word phrases too
|
44
|
+
results = client.lookup('give up')
|
45
|
+
```
|
46
|
+
|
47
|
+
## Development
|
48
|
+
|
49
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
50
|
+
|
51
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
Bug reports and pull requests are welcome on Codeberg at https://codeberg.org/bendangelo/free_dictionary_api.
|
56
|
+
|
57
|
+
## License
|
58
|
+
|
59
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module FreeDictionaryApi
|
2
|
+
class Word
|
3
|
+
attr_reader :word, :phonetic, :phonetics, :origin, :meanings
|
4
|
+
|
5
|
+
def initialize(data)
|
6
|
+
@word = data['word']
|
7
|
+
@phonetic = data['phonetic']
|
8
|
+
@phonetics = data['phonetics']
|
9
|
+
@origin = data['origin']
|
10
|
+
@meanings = data['meanings']
|
11
|
+
end
|
12
|
+
|
13
|
+
def definitions
|
14
|
+
meanings.flat_map { |meaning| meaning['definitions'] }
|
15
|
+
end
|
16
|
+
|
17
|
+
def parts_of_speech
|
18
|
+
meanings.map { |meaning| meaning['partOfSpeech'] }.uniq
|
19
|
+
end
|
20
|
+
|
21
|
+
def examples
|
22
|
+
definitions.map { |definition| definition['example'] }.compact
|
23
|
+
end
|
24
|
+
|
25
|
+
def synonyms
|
26
|
+
definitions.flat_map { |definition| definition['synonyms'] }.compact
|
27
|
+
end
|
28
|
+
|
29
|
+
def antonyms
|
30
|
+
definitions.flat_map { |definition| definition['antonyms'] }.compact
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
require 'uri'
|
4
|
+
require_relative 'free_dictionary_api/version'
|
5
|
+
require_relative 'free_dictionary_api/word'
|
6
|
+
|
7
|
+
module FreeDictionaryApi
|
8
|
+
class Error < StandardError; end
|
9
|
+
|
10
|
+
class Client
|
11
|
+
BASE_URL = 'https://api.dictionaryapi.dev/api/v2/entries/en'.freeze
|
12
|
+
PARSER = URI::Parser.new
|
13
|
+
|
14
|
+
def lookup(word)
|
15
|
+
encoded_word = PARSER.escape(word)
|
16
|
+
uri = URI("#{BASE_URL}/#{encoded_word}")
|
17
|
+
response = Net::HTTP.get_response(uri)
|
18
|
+
|
19
|
+
case response
|
20
|
+
when Net::HTTPSuccess
|
21
|
+
parse_response(response.body)
|
22
|
+
else
|
23
|
+
handle_error(response)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def parse_response(body)
|
30
|
+
data = JSON.parse(body)
|
31
|
+
data.map { |word_data| Word.new(word_data) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def handle_error(response)
|
35
|
+
case response.code
|
36
|
+
when '404'
|
37
|
+
raise Error, "Word not found"
|
38
|
+
else
|
39
|
+
raise Error, "API request failed with status #{response.code}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: free_dictionary_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben D'Angelo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-10-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '13.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '13.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: webmock
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: vcr
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '6.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '6.0'
|
83
|
+
description: A Ruby wrapper for the Free Dictionary API (https://dictionaryapi.dev/)
|
84
|
+
email:
|
85
|
+
- ben@bendangelo.me
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- LICENSE.txt
|
91
|
+
- README.md
|
92
|
+
- lib/free_dictionary_api.rb
|
93
|
+
- lib/free_dictionary_api/version.rb
|
94
|
+
- lib/free_dictionary_api/word.rb
|
95
|
+
homepage: https://codeberg.org/bendangelo/free_dictionary_api
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata:
|
99
|
+
homepage_uri: https://codeberg.org/bendangelo/free_dictionary_api
|
100
|
+
source_code_uri: https://codeberg.org/bendangelo/free_dictionary_api
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 2.6.0
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubygems_version: 3.5.21
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: Ruby client for the Free Dictionary API
|
120
|
+
test_files: []
|