veterejo

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.swab.dev/veterejo.git
Log | Files | Refs | README | LICENSE

commit 12af6619f23b2a91bc0d3e20216fe844649df7a3
parent 9e6b3df6c78a013fd4a0deac2b8d6dc731ef3204
Author: Jaron Swab <jrswab@gmail.com>
Date:   Sun,  8 Mar 2020 14:03:17 -0400

Updated Readme and added url error check

Diffstat:
MREADME.md | 7++++---
Mtests/veterejo_test.go | 38++++++++++++++++++++++++++++----------
Mveterejo.go | 14++++++++++----
3 files changed, 42 insertions(+), 17 deletions(-)

diff --git a/README.md b/README.md @@ -2,14 +2,15 @@ Veterejo is a Go package for querying weathe data from Open Weather Maps. Vetero is "weather" in Esperanto and "ejo" is place. -By replacing the **O** of a noun with **ejo** the word discribes a place of that thing. +By replacing the **o** of a noun with **ejo** the word discribes a place of that thing. (eg: Computilejo = computer-place or in English a computer lab) ## Using This Package: 1. Get an API Key from (OpenWeatheMap)[https://openweathermap.org] 2. Import this package 3. Create a new `WeatherData` struct: `w := new(veterejo.WeatherData)` -4. Call the data: `w.getData(yourCityID, unitsOfMeasure, yourApiId)` -5. Use the method for temperature to get the most recent temperature data on OpenWeatherMap +4. Make parse the URL: `url, err := veterejo.MakeURL(yourCityID, unitsOfMeasure, yourApiId)` +5. Call the data: `w.getData(url)` +6. Use the method for temperature to get the most recent temperature data on OpenWeatherMap *veterejo is licensed under the BSD 3-Clause "New" or "Revised" License* diff --git a/tests/veterejo_test.go b/tests/veterejo_test.go @@ -16,11 +16,11 @@ func TestMakeURL(t *testing.T) { apiID string } tests := []struct { - name string - args args - want string + name string + args args + want string + wantErr bool }{ - // TODO: Add test cases. { name: "Test with all args", args: args{ @@ -28,7 +28,8 @@ func TestMakeURL(t *testing.T) { units: "metric", apiID: "0x0p0q", }, - want: "https://api.openweathermap.org/data/2.5/weather/?id=0000&units=metric&appid=0x0p0q", + want: "https://api.openweathermap.org/data/2.5/weather/?id=0000&units=metric&appid=0x0p0q", + wantErr: false, }, { name: "Test without CityID", @@ -37,21 +38,38 @@ func TestMakeURL(t *testing.T) { units: "metric", apiID: "0x0p0q", }, - want: "https://api.openweathermap.org/data/2.5/weather/?units=metric&appid=0x0p0q", + want: "https://api.openweathermap.org/data/2.5/weather/?units=metric&appid=0x0p0q", + wantErr: false, }, { - name: "Test without unitis", + name: "Test without units", args: args{ cityID: "0000", units: "", apiID: "0x0p0q", }, - want: "https://api.openweathermap.org/data/2.5/weather/?id=0000&appid=0x0p0q", + want: "https://api.openweathermap.org/data/2.5/weather/?id=0000&appid=0x0p0q", + wantErr: false, + }, + { + name: "Test without API Key", + args: args{ + cityID: "0000", + units: "imperial", + apiID: "", + }, + want: "", + wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := v.MakeURL(tt.args.cityID, tt.args.units, tt.args.apiID); got != tt.want { + got, err := v.MakeURL(tt.args.cityID, tt.args.units, tt.args.apiID) + if (err != nil) != tt.wantErr { + t.Errorf("MakeURL() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { t.Errorf("MakeURL() = %v, want %v", got, tt.want) } }) @@ -78,7 +96,7 @@ func TestGetData(t *testing.T) { wantErr: false, }, { - name: "Test with invalid url", + name: "Test with an empty string as the url", url: fakeURL, wantErr: true, }, diff --git a/veterejo.go b/veterejo.go @@ -50,14 +50,20 @@ type WeatherData struct { } // MakeURL formats the URL for the Open Weather Map API call -func MakeURL(cityID, units, apiID string) string { +func MakeURL(cityID, units, apiID string) (string, error) { + if apiID == "" { + return "", fmt.Errorf("no API Key provided to create url") + } + if cityID == "" { - return fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather/?units=%s&appid=%s", units, apiID) + return fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather/?units=%s&appid=%s", units, apiID), nil } + if units == "" { - return fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather/?id=%s&appid=%s", cityID, apiID) + return fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather/?id=%s&appid=%s", cityID, apiID), nil } - return fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather/?id=%s&units=%s&appid=%s", cityID, units, apiID) + + return fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather/?id=%s&units=%s&appid=%s", cityID, units, apiID), nil } // GetData calls the OpenWeatherMap API and adds the date to the struct.