Skip to content

Commit

Permalink
Making public on GitHub -RS
Browse files Browse the repository at this point in the history
  • Loading branch information
eightseventhreethree committed Aug 19, 2018
1 parent b6e02c6 commit f831e70
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .go_grub.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
yelp:
api_url: "https://api.yelp.com/v3/"
api_token: "apitokengoeshere"
debug: false
107 changes: 107 additions & 0 deletions yelp_me.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"fmt"
"os"
gabs "github.com/Jeffail/gabs"
pflag "github.com/spf13/pflag"
viper "github.com/spf13/viper"
"gopkg.in/resty.v1"
"strconv"
)

func ReadConfig() (string, string, bool) {
viper.SetConfigName(".go_grub")
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/")
viper.SetConfigType("yaml")
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
fatalStr := "Fatal error config file: %s \n" +
"Place config file in $HOME/.go_grub.yml\n" +
"yelp: \n api_url: apiurlgoeshere (not required has default set)\n" +
" api_token: apitokegoeshere \n" +
"debug: true (not required)"
panic(fmt.Errorf(fatalStr, err))
}
viper.SetDefault("yelp.api_url", "https://api.yelp.com/v3/")
viper.SetDefault("debug", false)
yelpAPIUrl := viper.Get("yelp.api_url")
yelpAPIToken := viper.Get("yelp.api_token")
debug := viper.GetBool("debug")
if debug {
fmt.Println("Here is the value of debugMode: ", debug)
}
return yelpAPIUrl.(string), yelpAPIToken.(string), debug
}

func ParseCLI(debugMode bool) (string, int, int) {
var searchArg string
var zipArg int
var distanceArg int
pflag.StringVarP(&searchArg, "search", "s", "", "Keyword to search for on Yelp. REQUIRED")
pflag.IntVarP(&zipArg, "zip", "z", 12345, "Zip code to search around. REQUIRED")
pflag.IntVarP(&distanceArg, "distance", "d", 10, "Distance in miles around the zip you are willing to look. NOT REQUIRED")
pflag.Parse()
if debugMode {
fmt.Println("Here is the value of flag searchArg: ", searchArg)
fmt.Println("Here is the value of flag zipArg: ", zipArg)
fmt.Println("Here is the value of flag distanceArg: ", distanceArg)
}
if searchArg == "" {
fmt.Println("No arguments passed. Use --help to find out more.")
os.Exit(1)
}
distanceArgMeter := distanceArg * 1609
return searchArg, zipArg, distanceArgMeter
}

func RestyConfig(yelpAPIUrl string, yelpAPIToken string, debug bool) {
// Host URL for all request. So you can use relative URL in the request
resty.SetHostURL(yelpAPIUrl)
resty.SetDebug(debug)

// Headers for all request
resty.SetHeader("Accept", "application/json")
resty.SetHeaders(map[string]string{
"Content-Type": "application/json",
"User-Agent": "go_grub",
})
resty.SetAuthToken(yelpAPIToken)
}

func RequestBuisnessSearch(keywordSearch string, zipSearch int, distanceSearch int) []byte {
resp, err := resty.R().
SetQueryParams(map[string]string{
"term": keywordSearch,
"location": strconv.Itoa(zipSearch),
"radius": strconv.Itoa(distanceSearch),
"open_now": "true",
"sort_by": "rating",
}).
Get("businesses/search")
//fmt.Printf("\nResponse Body: %v", resp.String())
if err != nil {
fmt.Printf("\nResponse Err: %v", err)
}
return resp.Body()
}

func ParseResponse(input []byte) {
jsonParsed, err := gabs.ParseJSON(input)
if err != nil {
fmt.Printf("\ngabs.ParseJson err: %v", err)
}
children, _ := jsonParsed.S("businesses").Children()
for _, child := range children {
fmt.Println(" Name:", child.Search("name").Data(), " Is closed?:", child.Search("is_closed").Data(), " Rating:", child.Search("rating").Data())
}
}

func main() {
yelpAPIUrl, yelpAPIToken, debug := ReadConfig()
keywordSearch, zipSearch, distanceSearch := ParseCLI(debug)
RestyConfig(yelpAPIUrl, yelpAPIToken, debug)
buisnessBody := RequestBuisnessSearch(keywordSearch, zipSearch, distanceSearch)
ParseResponse(buisnessBody)
}

0 comments on commit f831e70

Please sign in to comment.