wiktionary_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 +53 -0
- data/lib/wiktionary_api/version.rb +3 -0
- data/lib/wiktionary_api/word.rb +24 -0
- data/lib/wiktionary_api.rb +66 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d197bdd3a4ca0783154ddee36287d5bf96b63693fc1ceb7e2161771fe5281559
|
4
|
+
data.tar.gz: 573bcc23d48203a941a4b8b5630409cb80271a97ec4b6d900dc6ae53d12553eb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2d37db76704da2f422f6d6e518696e94bacd437fe06bf9d2e2a87d10b77507bbe05ef201bdd44e1d761ccc3722613ecaa9ab5409a1666ea44a6d5d145d627c78
|
7
|
+
data.tar.gz: d6c7ad9b8abdb654a4956e67d57588d9c86a7652d4d3b13a820dabc502e10b026baaafca265a481316e2765080880173ae10afdf44b0377e75249b110e3654e6
|
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,53 @@
|
|
1
|
+
# Wiktionary API
|
2
|
+
|
3
|
+
A ruby gem to interface with the [Wiktionary API](https://en.wiktionary.org/api/rest_v1/).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```
|
8
|
+
gem install wiktionary_api
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```
|
14
|
+
# Initialize the client
|
15
|
+
client = WiktionaryAPI::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.part_of_speech # => "Interjection"
|
25
|
+
|
26
|
+
# Get all definitions
|
27
|
+
word.definitions.each do |definition|
|
28
|
+
puts definition # => "A greeting (salutation) said when meeting someone or acknowledging someone’s arrival or presence."
|
29
|
+
end
|
30
|
+
|
31
|
+
# Get examples
|
32
|
+
puts word.examples # => ["Hello, everyone.", ...]
|
33
|
+
|
34
|
+
# Works with multi-word phrases too
|
35
|
+
results = client.lookup('give up')
|
36
|
+
|
37
|
+
# Works with words with apostrophes
|
38
|
+
results = client.lookup("I'll")
|
39
|
+
```
|
40
|
+
|
41
|
+
## Development
|
42
|
+
|
43
|
+
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.
|
44
|
+
|
45
|
+
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).
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
Bug reports and pull requests are welcome on Codeberg at https://codeberg.org/bendangelo/wiktionary_api.
|
50
|
+
|
51
|
+
## License
|
52
|
+
|
53
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module WiktionaryAPI
|
2
|
+
class Word
|
3
|
+
attr_reader :part_of_speech, :language
|
4
|
+
|
5
|
+
def initialize(data)
|
6
|
+
@part_of_speech = data['partOfSpeech']
|
7
|
+
@language = data['language']
|
8
|
+
@definitions = data['definitions']
|
9
|
+
end
|
10
|
+
|
11
|
+
def definitions
|
12
|
+
@definitions.flat_map { |meaning| ::Nokogiri::HTML(meaning['definition']).text.strip }
|
13
|
+
end
|
14
|
+
|
15
|
+
def examples
|
16
|
+
@definitions.flat_map do |definition|
|
17
|
+
definition["examples"].map do |e|
|
18
|
+
::Nokogiri::HTML(e).text.strip
|
19
|
+
end
|
20
|
+
end.compact
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
require 'nokogiri'
|
4
|
+
require 'uri'
|
5
|
+
require 'cgi'
|
6
|
+
require_relative 'wiktionary_api/version'
|
7
|
+
require_relative 'wiktionary_api/word'
|
8
|
+
|
9
|
+
module WiktionaryAPI
|
10
|
+
class Error < StandardError; end
|
11
|
+
|
12
|
+
class Client
|
13
|
+
BASE_URL = 'https://en.wiktionary.org/api/rest_v1/page/definition'.freeze
|
14
|
+
|
15
|
+
def self.lookup word, lang: :en
|
16
|
+
self.new.lookup word
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.lookup! word, lang: :en
|
20
|
+
self.new.lookup! word
|
21
|
+
end
|
22
|
+
|
23
|
+
def lookup(word, lang: :en)
|
24
|
+
look_up_internal word, raise_error: false, lang: lang
|
25
|
+
end
|
26
|
+
|
27
|
+
def lookup!(word, lang: :en)
|
28
|
+
look_up_internal word, raise_error: true, lang: lang
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def look_up_internal word, raise_error: false, lang: :en
|
34
|
+
encoded_word = CGI.escape(word.gsub(" ", "_"))
|
35
|
+
uri = URI("#{BASE_URL}/#{encoded_word}")
|
36
|
+
response = Net::HTTP.get_response(uri)
|
37
|
+
|
38
|
+
case response
|
39
|
+
when Net::HTTPSuccess
|
40
|
+
parse_response(response.body, lang: lang)
|
41
|
+
else
|
42
|
+
handle_error(response, raise_error)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def parse_response(body, lang:)
|
47
|
+
data = JSON.parse(body)
|
48
|
+
|
49
|
+
lang_data = data[lang.to_s]
|
50
|
+
lang_data.map { |word_data| Word.new(word_data) }
|
51
|
+
end
|
52
|
+
|
53
|
+
def handle_error(response, raise_error = false)
|
54
|
+
case response.code
|
55
|
+
when '404'
|
56
|
+
if raise_error
|
57
|
+
raise Error, "Word not found"
|
58
|
+
else
|
59
|
+
return nil
|
60
|
+
end
|
61
|
+
else
|
62
|
+
raise Error, "API request failed with status #{response.code}"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wiktionary_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-12-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '13.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '13.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
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: webmock
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: vcr
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '6.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '6.0'
|
97
|
+
description: A Ruby wrapper for the Wiktionary API (https://en.wiktionary.org/api/rest_v1/)
|
98
|
+
email:
|
99
|
+
- ben@bendangelo.me
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- LICENSE.txt
|
105
|
+
- README.md
|
106
|
+
- lib/wiktionary_api.rb
|
107
|
+
- lib/wiktionary_api/version.rb
|
108
|
+
- lib/wiktionary_api/word.rb
|
109
|
+
homepage: https://codeberg.org/bendangelo/wiktionary_api
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata:
|
113
|
+
homepage_uri: https://codeberg.org/bendangelo/wiktionary_api
|
114
|
+
source_code_uri: https://codeberg.org/bendangelo/wiktionary_api
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: 2.6.0
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubygems_version: 3.5.21
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: Ruby client for the Wiktionary API
|
134
|
+
test_files: []
|