open_dictionary 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fd062f530ca5319bc77aeaef692ea566a7c1221d71f320be2387a6de89ee70ac
4
+ data.tar.gz: 0b1df3dab0513e51f66ddc16f0a15d94312c8c6cdd285f986d1dcb778399933e
5
+ SHA512:
6
+ metadata.gz: 78de5f1b27bec2d16fc529f7830148b84e3eb9d8c143c5df5ab66b196c7448a4ed19678d017c152a31fb2f4b2b86250dbe32e7df3ae3728ea551263b99ae657b
7
+ data.tar.gz: ff7b3b44666bb8091fba8a5066d010ca20a0415bc2daacf4ce20fadcd27143ed01666a66928f4cda243bb203fde2eb7c6c48c757ec9eddb15371903bd97902b5
@@ -0,0 +1,10 @@
1
+ module OpenDictionary
2
+ class DefinitionEntry
3
+ attr_reader :meaning, :examples
4
+
5
+ def initialize(data)
6
+ @meaning = data['meaning']
7
+ @examples = data['examples']
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module OpenDictionary
2
+ class DefinitionGroup
3
+ attr_reader :description, :entries
4
+
5
+ def initialize(data)
6
+ @description = data['description']
7
+ @entries = data['entries'].map { |entry| DefinitionEntry.new(entry) }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,24 @@
1
+ module OpenDictionary
2
+ class Etymology
3
+ attr_reader :description, :nouns, :verbs, :adjectives, :prepositions,
4
+ :adverbs, :letter, :number
5
+
6
+ def initialize(data)
7
+ @description = data['description']
8
+ @nouns = parse_parts_of_speech(data['nouns'])
9
+ @verbs = parse_parts_of_speech(data['verbs'])
10
+ @adjectives = parse_parts_of_speech(data['adjectives'])
11
+ @prepositions = parse_parts_of_speech(data['prepositions'])
12
+ @adverbs = parse_parts_of_speech(data['adverbs'])
13
+ @letter = parse_parts_of_speech(data['letter'])
14
+ @number = parse_parts_of_speech(data['number'])
15
+ end
16
+
17
+ private
18
+
19
+ def parse_parts_of_speech(parts)
20
+ return [] if parts.nil?
21
+ parts.map { |part| PartOfSpeech.new(part) }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ module OpenDictionary
2
+ class PartOfSpeech
3
+ attr_reader :description, :definition_groups
4
+
5
+ def initialize(data)
6
+ @description = data['description']
7
+ @definition_groups = data['definitionGroups'].map { |group| DefinitionGroup.new(group) }
8
+ end
9
+ end
10
+ end
File without changes
@@ -0,0 +1,10 @@
1
+ module OpenDictionary
2
+ class Word
3
+ attr_reader :word, :etymologies
4
+
5
+ def initialize(data)
6
+ @word = data['word']
7
+ @etymologies = data['etymologies'].map { |etym| Etymology.new(etym) }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,47 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require_relative 'open_dictionary/word'
4
+ require_relative 'open_dictionary/etymology'
5
+ require_relative 'open_dictionary/part_of_speech'
6
+ require_relative 'open_dictionary/definition_group'
7
+ require_relative 'open_dictionary/definition_entry'
8
+
9
+ module OpenDictionary
10
+ class Error < StandardError; end
11
+
12
+ class Client
13
+ BASE_URL = 'https://raw.githubusercontent.com/vighnesh153/open-dictionary/main/data'
14
+
15
+ def self.lookup(word)
16
+ new.lookup(word)
17
+ end
18
+
19
+ def lookup(word)
20
+ response = fetch_definition(word)
21
+ parse_response(response, word)
22
+ rescue HTTParty::Error => e
23
+ raise Error, "HTTP Error: #{e.message}"
24
+ rescue JSON::ParserError => e
25
+ raise Error, "JSON parsing error: #{e.message}"
26
+ end
27
+
28
+ private
29
+
30
+ def fetch_definition(word)
31
+ path = word.downcase.chars.join('/')
32
+ url = "#{BASE_URL}/#{path}/_.json"
33
+ response = HTTParty.get(url)
34
+
35
+ unless response.success?
36
+ raise Error, "Failed to fetch definition (HTTP #{response.code})"
37
+ end
38
+
39
+ response.body
40
+ end
41
+
42
+ def parse_response(response_body, original_word)
43
+ data = JSON.parse(response_body)
44
+ Word.new(data)
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_dictionary
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: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.21.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.21.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.12'
41
+ description: A Ruby gem to fetch dictionary definitions from the open-dictionary GitHub
42
+ repository
43
+ email: ben@bendangelo.me
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/open_dictionary.rb
49
+ - lib/open_dictionary/definition_entry.rb
50
+ - lib/open_dictionary/definition_group.rb
51
+ - lib/open_dictionary/etymology.rb
52
+ - lib/open_dictionary/part_of_speech.rb
53
+ - lib/open_dictionary/version.rb
54
+ - lib/open_dictionary/word.rb
55
+ homepage: https://codeberg.org/bendangelo/open_dictionary
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.5.21
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Dictionary definitions from open-dictionary
78
+ test_files: []