Skip to content

Commit

Permalink
Refactor to allow search
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiecollinson committed Feb 18, 2018
1 parent 01f11cb commit 60e406c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 26 deletions.
51 changes: 25 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,37 @@ package main

import (
"flag"
"github.com/bobesa/chalk"
"github.com/dghubble/sling"
"fmt"
"log"
)

const quotesUrl string = "https://favqs.com/api/"
const qotd string = "qotd.json"

type Quote struct {
Id int `json:"id"`
Body string `json:"body"`
Author string `json:"author"`
Tags []string `json:"tags"`
}

type QOTDResponse struct {
Quote Quote `json:"quote"`
}

func main() {
// qotd := flag.Bool("qotd", false, "display quote of the day")
// Define & parse flags
search := flag.String("search", "", "search for quotes on a topic")
author := flag.String("author", "", "search for quotes by author")
tag := flag.String("tag", "", "search for quotes with tag")
showTags := flag.Bool("showtags", false, "display tags for each quote")
max := flag.Int("n", 10, "maximum number of results")

flag.Parse()

quotesBase := sling.New().Base(quotesUrl)

qotdResponse := QOTDResponse{}
_, err := quotesBase.Get(qotd).ReceiveSuccess(&qotdResponse)
if err != nil {
log.Fatal(err)
// Search or get QOTD depending on flags
if *search != "" || *author != "" || *tag != "" {
quotes, err := GetQuotes(*search, *author, *tag, *max)
if err != nil {
log.Fatal(err)
}
for i, quote := range quotes {
printQuote(quote, *showTags)
if i != len(quotes)-1 {
fmt.Println()
}
}
} else {
quote, err := GetRandomQuote()
if err != nil {
log.Fatal(err)
}
printQuote(quote, *showTags)
}

chalk.Cyan().Print(qotdResponse.Quote.Body)
chalk.Magenta().Italic().Print(" ", qotdResponse.Quote.Author, "\n")
}
15 changes: 15 additions & 0 deletions print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import "github.com/bobesa/chalk"

func printQuote(quote Quote, showTags bool) {
if showTags {
tagString := chalk.White().Faint().Sprint(quote.Tags)
chalk.Cyan().Italic().Printf("%s %s\n", quote.Body, tagString)
} else {
chalk.Cyan().Italic().Printf("%s\n", quote.Body)
}
if quote.Author != "" {
chalk.Magenta().Printf("%s\n", quote.Author)
}
}
76 changes: 76 additions & 0 deletions quotes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"fmt"
"github.com/dghubble/sling"
"os"
)

var (
quoteApi = sling.New().Base("https://favqs.com/api/")
qotdPath = "qotd.json"
quotesPath = "quotes"
defaultApiKey = "1501a5726d3be7d5839dc9085ba47848"
favqsApiKey string
)

func init() {
var ok bool
favqsApiKey, ok = os.LookupEnv("FAVQS_API_KEY")
if !ok {
favqsApiKey = defaultApiKey
}
}

type Quote struct {
Id int `json:"id"`
Body string `json:"body"`
Author string `json:"author"`
Tags []string `json:"tags"`
}

type QOTDResponse struct {
Quote Quote `json:"quote"`
}

type QuotesResponse struct {
Quotes []Quote `json:"quotes"`
}

// Quotes params
type QuoteParams struct {
Filter string `url:"filter,omitempty"`
Type string `url:"type,omitempty"`
Page int `url:"page,omitempty"`
}

func GetRandomQuote() (Quote, error) {
qotdResponse := QOTDResponse{}
_, err := quoteApi.Get(qotdPath).ReceiveSuccess(&qotdResponse)
return qotdResponse.Quote, err
}

func GetQuotes(search, author, tag string, max int) ([]Quote, error) {
quotesResponse := QuotesResponse{}
quotesQuery := QuoteParams{
Filter: search,
}
if author != "" {
quotesQuery.Filter = author
quotesQuery.Type = "author"
}
if tag != "" {
quotesQuery.Filter = tag
quotesQuery.Type = "tag"
}
_, err := quoteApi.Get(quotesPath).Add("Authorization", fmt.Sprintf("Token token=%s", favqsApiKey)).QueryStruct(&quotesQuery).ReceiveSuccess(&quotesResponse)
if err != nil {
return nil, err
}

quotes := quotesResponse.Quotes
if max < len(quotes) {
quotes = quotes[:max]
}
return quotes, nil
}

0 comments on commit 60e406c

Please sign in to comment.