Skip to content

Commit 43067ef

Browse files
committed
add name-string widget (close #104)
1 parent 47148bb commit 43067ef

File tree

11 files changed

+942
-60
lines changed

11 files changed

+942
-60
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Changelog
22

3+
## [v1.1.0] - 2023-05-12 Fri
4+
35
- Add [#104]: name-string widget.
46
- Add [#103]: name-string endpoint.
57

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ module github.com/gnames/gnverifier
22

33
go 1.20
44

5-
replace github.com/gnames/gnlib => ../gnlib
6-
75
require (
86
github.com/dnaeon/go-vcr v1.2.0
97
github.com/dustin/go-humanize v1.0.1
108
github.com/gnames/gnfmt v0.4.1
11-
github.com/gnames/gnlib v0.14.0
9+
github.com/gnames/gnlib v0.15.0
1210
github.com/gnames/gnquery v0.3.3
1311
github.com/gnames/gnsys v0.2.2
1412
github.com/gnames/gnuuid v0.1.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4
6666
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
6767
github.com/gnames/gnfmt v0.4.1 h1:U27POe/jSnkPym21juQq4NLha0tW0/eIo6BXF19hMDU=
6868
github.com/gnames/gnfmt v0.4.1/go.mod h1:W/3MKRRQMhiwb0kKXTMqpMbjt3zW+mSztHXoxkJmC5k=
69-
github.com/gnames/gnlib v0.14.0 h1:urDtkupQMpsiZ/7/nkI+ZtxlVE3CTVQFI0fkGZLfIaU=
70-
github.com/gnames/gnlib v0.14.0/go.mod h1:mzrTHNzYkTYLA2JmJ6UqJ/dk4eaQws+r3YQI4AAcOic=
69+
github.com/gnames/gnlib v0.15.0 h1:k2IbGM2pKJLnu92p7Fmlo7eolr9ryJuWVoC2v9I7a9U=
70+
github.com/gnames/gnlib v0.15.0/go.mod h1:mzrTHNzYkTYLA2JmJ6UqJ/dk4eaQws+r3YQI4AAcOic=
7171
github.com/gnames/gnquery v0.3.3 h1:Pcq3i67KoOAA0fkvUvRKq7ZMstJb8YZbZPU0LUz9/Jk=
7272
github.com/gnames/gnquery v0.3.3/go.mod h1:TAF0yt2mGcKv5awQvw1xYROZEp4O0Ce1wU5WRFlNR5s=
7373
github.com/gnames/gnstats v0.1.0 h1:qycaVt1ot3MtAZFnguSPXtND/BrKmw5Zhd1vfwcKxOs=

internal/io/web/server.go

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func Run(gnv gnverifier.GNverifier, port int) {
6262
e.GET("/data_sources", dataSources(gnv))
6363
e.GET("/data_sources/:id", dataSource(gnv))
6464
e.GET("/name_strings/:id", nameString(gnv))
65+
e.GET("/name_strings/widget/:id", nameStringWidget(gnv))
6566
e.GET("/about", about(gnv))
6667
e.GET("/api", api(gnv))
6768

@@ -105,49 +106,11 @@ func api(gnv gnverifier.GNverifier) func(echo.Context) error {
105106

106107
func nameString(gnv gnverifier.GNverifier) func(echo.Context) error {
107108
return func(c echo.Context) error {
108-
id, _ := url.QueryUnescape(c.Param("id"))
109-
var ds []int
110-
var allMatches bool
111-
dsStr := c.QueryParam("data_sources")
112-
if dsStr != "" {
113-
dss := strings.Split(dsStr, ",")
114-
for i := range dss {
115-
num, err := strconv.Atoi(dss[i])
116-
if err == nil {
117-
ds = append(ds, num)
118-
}
119-
}
120-
}
121-
allMatches = c.QueryParam("all_matches") == "true"
122-
inp := vlib.NameStringInput{
123-
ID: id,
124-
DataSources: ds,
125-
WithAllMatches: allMatches,
126-
}
127-
res, err := gnv.NameString(inp)
109+
data, err := getNameString(c, gnv)
128110
if err != nil {
129111
return err
130112
}
131-
var names []vlib.Name
132-
if res.Name != nil {
133-
names = []vlib.Name{*res.Name}
134-
}
135-
136-
data := Data{
137-
Input: inp.ID,
138-
Format: "html",
139-
DataSourceIDs: inp.DataSources,
140-
AllMatches: inp.WithAllMatches,
141-
Page: "home",
142-
Verified: names,
143-
Version: gnv.GetVersion().Version,
144-
}
145-
format := c.QueryParam("format")
146-
if format == "csv" || format == "json" || format == "tsv" {
147-
data.Format = format
148-
}
149113

150-
fmt.Printf("NAME: %#v\n\n", data)
151114
switch data.Format {
152115
case "json":
153116
return c.JSON(http.StatusOK, data.Verified)
@@ -163,6 +126,66 @@ func nameString(gnv gnverifier.GNverifier) func(echo.Context) error {
163126
}
164127
}
165128

129+
func nameStringWidget(gnv gnverifier.GNverifier) func(echo.Context) error {
130+
return func(c echo.Context) error {
131+
data, err := getNameString(c, gnv)
132+
if err != nil {
133+
return err
134+
}
135+
return c.Render(http.StatusOK, "name_string_widget", data)
136+
}
137+
}
138+
139+
func getNameString(
140+
c echo.Context,
141+
gnv gnverifier.GNverifier,
142+
) (Data, error) {
143+
var res Data
144+
id, _ := url.QueryUnescape(c.Param("id"))
145+
var ds []int
146+
var allMatches bool
147+
dsStr := c.QueryParam("data_sources")
148+
if dsStr != "" {
149+
dss := strings.Split(dsStr, ",")
150+
for i := range dss {
151+
num, err := strconv.Atoi(dss[i])
152+
if err == nil {
153+
ds = append(ds, num)
154+
}
155+
}
156+
}
157+
allMatches = c.QueryParam("all_matches") == "true"
158+
inp := vlib.NameStringInput{
159+
ID: id,
160+
DataSources: ds,
161+
WithAllMatches: allMatches,
162+
}
163+
out, err := gnv.NameString(inp)
164+
if err != nil {
165+
return res, err
166+
}
167+
var names []vlib.Name
168+
if out.Name != nil {
169+
names = []vlib.Name{*out.Name}
170+
}
171+
172+
res = Data{
173+
Input: inp.ID,
174+
Format: "html",
175+
DataSourceIDs: inp.DataSources,
176+
AllMatches: inp.WithAllMatches,
177+
Page: "home",
178+
Verified: names,
179+
Version: gnv.GetVersion().Version,
180+
}
181+
format := c.QueryParam("format")
182+
if format == "csv" || format == "json" || format == "tsv" {
183+
res.Format = format
184+
}
185+
186+
return res, nil
187+
}
188+
166189
func dataSources(gnv gnverifier.GNverifier) func(echo.Context) error {
167190
return func(c echo.Context) error {
168191
var err error

internal/io/web/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestAPI(t *testing.T) {
5555

5656
assert.Nil(t, api(gnv)(c))
5757
assert.Equal(t, http.StatusOK, rec.Code)
58-
assert.Contains(t, rec.Body.String(), "OpenAPI Schema")
58+
assert.Contains(t, rec.Body.String(), "API Documentation")
5959
}
6060

6161
func TestHomeGET(t *testing.T) {

0 commit comments

Comments
 (0)