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