-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathword.go
64 lines (54 loc) · 1.62 KB
/
word.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package dictionaryapi
import (
"fmt"
"github.com/privatesquare/bkst-go-utils/utils/errors"
)
const (
wordEntriesApiPath = "/api/v2/entries/en/%s"
)
type (
// Word represents a Word who's meaning is fetched from the dictionary API.
Word struct {
Word string `json:"word"`
Phonetic string `json:"phonetic"`
Phonetics []Phonetics `json:"phonetics"`
Origin string `json:"origin"`
Meanings []Meanings `json:"meanings"`
}
// Phonetics represents the sound of the Word.
Phonetics struct {
Text string `json:"text"`
Audio string `json:"audio,omitempty"`
}
// Definitions represent a statement of the exact meaning of the Word.
Definitions struct {
Definition string `json:"definition"`
Example string `json:"example"`
Synonyms []interface{} `json:"synonyms"`
Antonyms []interface{} `json:"antonyms"`
}
// Meanings represents the different meanings of the Word
Meanings struct {
PartOfSpeech string `json:"partOfSpeech"`
Definitions []Definitions `json:"definitions"`
}
WordsManager interface {
Get(string) (*Word, *errors.RestErr)
}
wordManager struct {
Client *Client
}
)
// Get queries the dictionary API to fetch the meaning of a word which is passed as input.
// The method returns the meaning of the word if the request is successful.
// The method returns a error if
// - the API request fails.
// - the word is not found.
func (wm *wordManager) Get(word string) (*Word, *errors.RestErr) {
result := new([]Word)
err := wm.Client.get(fmt.Sprintf(wordEntriesApiPath, word), result)
if err != nil {
return nil, err
}
return &(*result)[0], nil
}