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 #2

Open
wants to merge 29 commits into
base: prme-full-review
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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)
[![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).

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 [email protected]: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)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
c, err := NewNorwayClient(resolver)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
w, err := c.GetForecast("Castlebar", "IE")
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