Rename functions
This commit is contained in:
parent
02a9e4e32f
commit
5d2130b6f5
6
hn.go
6
hn.go
|
@ -30,7 +30,7 @@ func init() {
|
||||||
HackerNewsFirebaseUrl = "https://hacker-news.firebaseio.com/v0"
|
HackerNewsFirebaseUrl = "https://hacker-news.firebaseio.com/v0"
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchTopStoriesFromHN() []Story {
|
func FetchHackerNewsTopStories() []Story {
|
||||||
// API docs: https://github.com/HackerNews/API
|
// API docs: https://github.com/HackerNews/API
|
||||||
|
|
||||||
url := fmt.Sprintf("%s/topstories.json", HackerNewsFirebaseUrl)
|
url := fmt.Sprintf("%s/topstories.json", HackerNewsFirebaseUrl)
|
||||||
|
@ -53,7 +53,7 @@ func fetchTopStoriesFromHN() []Story {
|
||||||
|
|
||||||
var stories [limit]Story
|
var stories [limit]Story
|
||||||
for i, id := range ids {
|
for i, id := range ids {
|
||||||
story := fetchStoryByID(id)
|
story := FetchStoryById(id)
|
||||||
stories[i] = story
|
stories[i] = story
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ func fetchTopStoriesFromHN() []Story {
|
||||||
return stories[:]
|
return stories[:]
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchStoryByID(id ItemID) Story {
|
func FetchStoryById(id ItemID) Story {
|
||||||
url := fmt.Sprintf("https://hacker-news.firebaseio.com/v0/item/%d.json", id)
|
url := fmt.Sprintf("https://hacker-news.firebaseio.com/v0/item/%d.json", id)
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
6
main.go
6
main.go
|
@ -1,9 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
stories := fetchTopStoriesFromHN()
|
stories := FetchHackerNewsTopStories()
|
||||||
filtered := filterByRelevanceForSN(&stories)
|
filtered := CurateContentForStackerNews(&stories)
|
||||||
for _, story := range *filtered {
|
for _, story := range *filtered {
|
||||||
postToSN(&story)
|
PostStoryToStackerNews(&story)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
14
sn.go
14
sn.go
|
@ -46,7 +46,7 @@ func init() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeGraphQLRequest(body GraphQLPayload) *http.Response {
|
func MakeStackerNewsRequest(body GraphQLPayload) *http.Response {
|
||||||
bodyJSON, err := json.Marshal(body)
|
bodyJSON, err := json.Marshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error during json.Marshal:", err)
|
log.Fatal("Error during json.Marshal:", err)
|
||||||
|
@ -70,14 +70,14 @@ func makeGraphQLRequest(body GraphQLPayload) *http.Response {
|
||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
|
|
||||||
func filterByRelevanceForSN(stories *[]Story) *[]Story {
|
func CurateContentForStackerNews(stories *[]Story) *[]Story {
|
||||||
// TODO: filter by relevance
|
// TODO: filter by relevance
|
||||||
|
|
||||||
slice := (*stories)[0:1]
|
slice := (*stories)[0:1]
|
||||||
return &slice
|
return &slice
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchDupes(url string) *[]Dupe {
|
func FetchStackerNewsDupes(url string) *[]Dupe {
|
||||||
body := GraphQLPayload{
|
body := GraphQLPayload{
|
||||||
Query: `
|
Query: `
|
||||||
query Dupes($url: String!) {
|
query Dupes($url: String!) {
|
||||||
|
@ -91,7 +91,7 @@ func fetchDupes(url string) *[]Dupe {
|
||||||
"url": url,
|
"url": url,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
resp := makeGraphQLRequest(body)
|
resp := MakeStackerNewsRequest(body)
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
var dupesResp DupesResponse
|
var dupesResp DupesResponse
|
||||||
|
@ -103,8 +103,8 @@ func fetchDupes(url string) *[]Dupe {
|
||||||
return &dupesResp.Data.Dupes
|
return &dupesResp.Data.Dupes
|
||||||
}
|
}
|
||||||
|
|
||||||
func postToSN(story *Story) {
|
func PostStoryToStackerNews(story *Story) {
|
||||||
dupes := fetchDupes(story.Url)
|
dupes := FetchStackerNewsDupes(story.Url)
|
||||||
if len(*dupes) > 0 {
|
if len(*dupes) > 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -122,6 +122,6 @@ func postToSN(story *Story) {
|
||||||
"title": story.Title,
|
"title": story.Title,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
resp := makeGraphQLRequest(body)
|
resp := MakeStackerNewsRequest(body)
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,6 @@ import (
|
||||||
func TestFetchDupes(t *testing.T) {
|
func TestFetchDupes(t *testing.T) {
|
||||||
// TODO: mock HTTP request
|
// TODO: mock HTTP request
|
||||||
url := "https://en.wikipedia.org/wiki/Dishwasher_salmon"
|
url := "https://en.wikipedia.org/wiki/Dishwasher_salmon"
|
||||||
dupes := fetchDupes(url)
|
dupes := FetchStackerNewsDupes(url)
|
||||||
assert.NotEmpty(t, *dupes, "Expected at least one duplicate")
|
assert.NotEmpty(t, *dupes, "Expected at least one duplicate")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue