Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full Review #5

Open
wants to merge 33 commits into
base: umang-review
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
03d060e
Initial commit
qba73 Oct 25, 2019
962f6f6
Update README.md
qba73 Mar 30, 2021
29e94ec
initial commit
qba73 Mar 30, 2021
0344b29
Create go.yml
qba73 Mar 30, 2021
90a7890
Update README.md
qba73 Mar 30, 2021
55a0d25
gofmt
qba73 Mar 30, 2021
a20b210
Merge branch 'master' of github.com:qba73/meteo
qba73 Mar 30, 2021
23d424d
Update README.md
qba73 Mar 30, 2021
538a6f8
Update README.md
qba73 Jun 2, 2021
68404db
Update README.md
qba73 Sep 2, 2021
fa913ed
initial lib structure
qba73 Oct 8, 2021
d2390eb
Merge branch 'master' of github.com:qba73/meteo
qba73 Oct 8, 2021
67fb5a4
update docs
qba73 Oct 8, 2021
7c2ffc4
wip - construct a client
qba73 Oct 14, 2021
adacdc5
Add test data
qba73 Oct 29, 2021
16cf779
Update dependencies
qba73 Oct 29, 2021
ffcb0c8
Get weather for Lat Lon
qba73 Oct 29, 2021
f9f6d2d
Merge pull request #1 from qba73/latlon
qba73 Oct 29, 2021
aad1523
Add Geo coordinates lookup
qba73 Nov 1, 2021
cd9d95f
Merge branch 'request'
qba73 Nov 1, 2021
11ea492
Implement geolookup interface
qba73 Nov 1, 2021
a0f37ad
Added initial docs
qba73 Nov 1, 2021
d9492b3
Update go.yml
qba73 Nov 2, 2021
5c49400
Update README.md
qba73 Nov 2, 2021
c6e68ef
Update documentation
qba73 Nov 2, 2021
6621d7a
Update docs
qba73 Nov 2, 2021
03a65d2
Update README.md
qba73 Nov 2, 2021
bf677f8
Make tests more robust
qba73 Nov 3, 2021
fdd0911
Merge master into umang-full-content
bitfield Nov 10, 2021
6637f6b
Update meteo.go
qba73 Nov 11, 2021
6c01d6a
Update meteo.go
qba73 Nov 11, 2021
5ddc66e
Update norway_test.go
qba73 Nov 11, 2021
6c68270
Update norway_test.go
qba73 Nov 11, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Go

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout main
uses: actions/checkout@v2

- name: Run unit tests
run: |
make test-ci
env:
CC_TEST_REPORTER_ID: ${{secrets.CC_TEST_REPORTER_ID}}
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test
*.html


# Output of the go coverage tool, specifically when used with LiteIDE
*.out
vendor/

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Jakub Jarosz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
82 changes: 82 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
.PHONY: help check cover test tidy

ROOT := $(PWD)
GO_HTML_COV := ./coverage.html
GO_TEST_OUTFILE := ./c.out
GO_DOCKER_IMAGE := golang:1.17
GO_DOCKER_CONTAINER := meteo-container
CC_TEST_REPORTER_ID := ${CC_TEST_REPORTER_ID}
CC_PREFIX := github.com/qba73/meteo


define PRINT_HELP_PYSCRIPT
import re, sys

for line in sys.stdin:
match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line)
if match:
target, help = match.groups()
print("%-20s %s" % (target, help))
endef
export PRINT_HELP_PYSCRIPT

default: help

help:
@python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST)

check: ## Run static check analyzer
staticcheck ./...

cover: ## Run unit tests and generate test coverage report
go test -v ./... -count=1 -covermode=count -coverprofile=coverage.out
go tool cover -html coverage.out
staticcheck ./...

test: ## Run unit tests locally
go test -v ./... -count=1
staticcheck ./...

# MODULES
tidy: ## Run go mod tidy and vendor
go mod tidy
go mod vendor


clean: ## Remove docker container if exist
docker rm -f ${GO_DOCKER_CONTAINER} || true

testdocker: ## Run unittests inside a container
docker run -w /app -v ${ROOT}:/app ${GO_DOCKER_IMAGE} go test ./... -coverprofile=${GO_TEST_OUTFILE}
docker run -w /app -v ${ROOT}:/app ${GO_DOCKER_IMAGE} go tool cover -html=${GO_TEST_OUTFILE} -o ${GO_HTML_COV}

lint: ## Run linter inside container
docker run --rm -v ${ROOT}:/data cytopia/golint .

# Custom logic for code climate
_before-cc:
# Download CC test reported
docker run -w /app -v ${ROOT}:/app ${GO_DOCKER_IMAGE} \
/bin/bash -c \
"curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter"

# Make reporter executable
docker run -w /app -v ${ROOT}:/app ${GO_DOCKER_IMAGE} chmod +x ./cc-test-reporter

# Run before build
docker run -w /app -v ${ROOT}:/app \
-e CC_TEST_REPORTER_ID=${CC_TEST_REPORTER_ID} ${GO_DOCKER_IMAGE} \
./cc-test-reporter before-build

_after-cc:
# Handle custom prefix
$(eval PREFIX=${CC_PREFIX})
ifdef prefix
$(eval PREFIX=${prefix})
endif
# Upload data to CC
docker run -w /app -v ${ROOT}:/app \
-e CC_TEST_REPORTER_ID=${CC_TEST_REPORTER_ID} \
${GO_DOCKER_IMAGE} ./cc-test-reporter after-build --prefix ${PREFIX}

test-ci: _before-cc testdocker _after-cc
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
![Go](https://github.com/qba73/meteo/workflows/Go/badge.svg)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These badges look nice!

[![Go Report Card](https://goreportcard.com/badge/github.com/qba73/meteo)](https://goreportcard.com/report/github.com/qba73/meteo)
[![Maintainability](https://api.codeclimate.com/v1/badges/4afc34a390da95ed9327/maintainability)](https://codeclimate.com/github/qba73/meteo/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/4afc34a390da95ed9327/test_coverage)](https://codeclimate.com/github/qba73/meteo/test_coverage)


# meteo

Meteo is a Go client library for the weather and meteorological forecast from [Yr](https://www.yr.no/en).
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great README! Maybe move the example up to the top, so it's the first thing users see?


Disclaimer:

Weather forecast from Yr, delivered by the Norwegian Meteorological Institute and NRK.

# Usage

## Preconditions

You must register your user agent string in the [YR.NO service](https://developer.yr.no/doc/TermsOfService/) and your user name in the [GeoNames service](https://www.geonames.org/login) to use the package.


## Installation
```
$ go get git@github.com:qba73/meteo.git
```

## Default

Export ```GEO_USERNAME``` env var that you registered with GeoNames.org.

Example:
```
$ export GEO_USERNAME=Jane123
```
Use the ```meteo``` package in your application:
```go
package main

import (
"fmt"
"log"
"github.com/qba73/meteo"
)

func main() {
// Get weather status for Castlebar in Ireland:
weather, err := meteo.GetWeather("Castlebar", "IE")
if err != nil {
log.Println(err)
}
// Print out weather string.
// Example: Lightrain 8.3°C
fmt.Println(weather)
}
```

7 changes: 7 additions & 0 deletions cmd/meteo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/qba73/meteo"

func main() {
meteo.RunCLI()
}
3 changes: 3 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package meteo is a client library for the weather
// API data provided by the Norwegian Meteorological Institute.
package meteo
7 changes: 7 additions & 0 deletions examples/basic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Println("Example 1")
}
20 changes: 20 additions & 0 deletions examples/default/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"
"log"

"github.com/qba73/meteo"
)

func main() {
// Get weather status for city Castlebar in Ireland:
weather, err := meteo.GetWeather("Castlebar", "IE")
if err != nil {
log.Println(err)
}

// Print out weather string.
// Example: Lightrain 8.3°C
fmt.Println(weather)
}
22 changes: 22 additions & 0 deletions examples/geoplaces/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"
"os"

"github.com/qba73/meteo"
)

func main() {
// Get coordinates using a default Geo client
user := os.Getenv("GEO_USERNAME")

coord, err := meteo.GetCoordinates("Castlebar", "IE", user)
if err != nil {
println(err)
}

fmt.Printf("Lat: %.2f, Lng: %.2f for %s in country %s\n", coord.Lat, coord.Lng, coord.PlaceName, coord.CountryCode)
// It returns:
// Lat: 53.85, Lng: -9.30 for Castlebar in country IE
}
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/qba73/meteo

go 1.17

require github.com/google/go-cmp v0.5.6

require golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
52 changes: 52 additions & 0 deletions meteo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package meteo

import (
"fmt"
"os"
"strings"
)

const (
userAgent = "Meteo/0.1 https://github.com/qba73/meteo"
)

// Weather represents weather conditions
// in a geographical region.
type Weather struct {
Summary string
Temp float64
}

// String implements stringer interface.
func (w Weather) String() string {
return fmt.Sprintf("%s %.1f°C", strings.Title(w.Summary), w.Temp)
}

// NameResolver interface is used by an Meteo Client
// to obtain geo coordinates for given place located in
// a country identified by country id.
type NameResolver interface {
// GetCoordinates takes place and country code
// and returns geo information like lat and lng.
GetCoordinates(placeName, country string) (Place, error)
}

// RunCLI is a main function that runs the cli machinery.
func RunCLI() {
uname := os.Getenv("GEO_USERNAME")
resolver, err := NewWikipediaClient(uname)
qba73 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
fmt.Fprintln(os.Stderr, err)
qba73 marked this conversation as resolved.
Show resolved Hide resolved
}
c, err := NewNorwayClient(resolver)
if err != nil {
fmt.Fprintln(os.Stderr, err)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Printing to standard error makes sense!

os.Exit(1)
}
w, err := c.GetForecast("Castlebar", "IE")
qba73 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println(w)
}
24 changes: 24 additions & 0 deletions meteo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package meteo_test

import (
"bytes"
"fmt"
"testing"

"github.com/qba73/meteo"
)

func TestWeatherStringFormat(t *testing.T) {
t.Parallel()
w := meteo.Weather{
Summary: "sunny",
Temp: -3.12,
}
out := bytes.Buffer{}
fmt.Fprint(&out, w)
got := out.String()
want := "Sunny -3.1°C"
if want != got {
t.Errorf("want %s, got %s", want, got)
}
}
Loading