openmensa

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2023 License: MIT Imports: 9 Imported by: 2

README

go-openmensa

Go Go Reference Go Report Card

Go API for OpenMensa

Install

The openmensa module functions purely as a library with no executables. To use it in a project, run

$ go get github.com/j0hax/go-openmensa

Example

Following code snippet fetches today's menu and prices for a cafeteria in Hannover:

package main

import (
	"fmt"
	"github.com/j0hax/go-openmensa"
	"log"
)

func main() {
	// Contine Hannover has ID 7
	contine, err := openmensa.GetCanteen(7)
	if err != nil {
		log.Fatal(err)
	}

	// Retrieve the current menu
	menu, err := contine.CurrentMenu()
	if err != nil {
		log.Fatal(err)
	}

	// Print out structured data
	fmt.Printf("%s: %s\n", contine.Name, menu.Day)
	for _, meal := range menu.Meals {
		price := meal.Prices["students"]
		fmt.Printf("- %s (%0.2f€)\n", meal, price)
	}
}

See Also

Documentation

Overview

Package openmensa provides an API to interface with OpenMensa.org.

Index

Constants

View Source
const DateLayout = "2006-01-02"

Simplified ISO 8601 date layout used by OpenMensa.

Can be used for Time.Format() et al.

Variables

View Source
var Endpoint = "https://openmensa.org/api/v2"

API endpoint URL

Functions

This section is empty.

Types

type Canteen

type Canteen struct {
	// Id is a unique identifier of the canteen.
	Id int `json:"id"`
	// Name of the canteen.
	Name string `json:"name"`
	// City the canteen is located in.
	City string `json:"city"`
	// Address of the canteen.
	Address string `json:"address"`
	// Geographic coordinates of the canteen.
	Coordinates []float64 `json:"coordinates"`
}

Canteen contains information associated with a specific canteen, cafe, cafeteria, etc.

func AllCanteens added in v0.3.0

func AllCanteens() ([]Canteen, error)

AllCanteens returns all canteens listed in OpenMensa

func CanteensNear added in v0.3.0

func CanteensNear(latitude, longitude, distance float64) ([]Canteen, error)

CanteensNear returns canteens in the radius of the given latitude and longitude

func GetCanteen

func GetCanteen(canteenId int) (*Canteen, error)

GetCanteen returns data about a specific canteen.

func GetCanteens

func GetCanteens(canteenIds ...int) ([]Canteen, error)

GetCanteens retrieves multiple canteens specified by their IDs.

func SearchCanteens added in v0.3.0

func SearchCanteens(pattern string) ([]Canteen, error)

SearchCanteens returns a slice of canteens whose names match the given pattern

func (*Canteen) AllMenus added in v0.4.0

func (c *Canteen) AllMenus() ([]Menu, error)

AllMenus returns all meals for all upcoming dates

func (*Canteen) CurrentMenu added in v0.4.0

func (c *Canteen) CurrentMenu() (*Menu, error)

CurrentMenu returns returns all meals served by a canteen on today's date.

func (*Canteen) Day added in v0.3.0

func (c *Canteen) Day(date time.Time) (*Day, error)

Day returns specific opening information of a given canteen on a given date.

func (*Canteen) Days added in v0.3.0

func (c *Canteen) Days() ([]Day, error)

Days returns upcoming open/closed dates of a canteen.

func (*Canteen) Meal added in v0.3.0

func (c *Canteen) Meal(date time.Time, mealId int) (*Meal, error)

Meal returns a specific meal.

A single meal is identified by the day it is served on and its ID.

func (*Canteen) MenuOn added in v0.4.0

func (c *Canteen) MenuOn(date time.Time) (*Menu, error)

MenuOn returns returns all meals served by a canteen on a given date.

func (Canteen) String

func (c Canteen) String() string

String returns a human-readable representation of the canteen.

Currently, this is simply the canteen's name.

type Day

type Day struct {
	// Date is the given date of operation.
	Date Opening `json:"date"`
	// Closed indicates if the canteen is closed on the given date.
	Closed bool `json:"closed"`
}

Day represents a canteen's opening status.

func (Day) String

func (d Day) String() string

String returns a human-readable representation of a canteen's opening data.

type Meal

type Meal struct {
	// Id is a unique identifier for the meal.
	Id int `json:"id"`
	// Name is the title of the meal.
	Name string `json:"name"`
	// Optional category metadata
	Category string `json:"category"`
	// Notes include extra information, such as allergens.
	Notes []string `json:"notes"`
	// Prices vary for different groups of patrons.
	//
	// Note that the groups vary by canteen operator.
	// Typically these include "students", "employees", "others", and "pupils".
	Prices map[string]float64 `json:"prices"`
}

Meal is the representation of a canteen's menu item.

func (Meal) String

func (m Meal) String() string

String returns a human-readable representation of a meal.

Currently, this is simply the meal's name.

func (*Meal) UnmarshalJSON added in v0.4.1

func (m *Meal) UnmarshalJSON(data []byte) error

UnmarshalJSON is a custom unmarshaller for Meals. This function acts as a regular json.Unmarshal, but removes duplicate note entries.

This function may be removed should the official OpenMensa API perform server-side duplicate handling one day.

type Menu struct {
	Day   Day    `json:"date"`
	Meals []Meal `json:"meals"`
}

Menu represents all meals served by a canteen on a given day.

In German, this is the semantic equivalent to a "Speiseplan"

func (m *Menu) UnmarshalJSON(data []byte) error

Custom unmarshaller to get around the inconsistency between /canteens/{id}/days/{date}, which returns an object with two attributes, and /canteens/{id}/meals, which returns two attributes and meals directly

This function may be removed should the official OpenMensa API return opening information as a single JSON object one day.

type Opening

type Opening time.Time

Opening is a wrapper type for dates.

func (Opening) String

func (o Opening) String() string

String returns a human-readable representation of a canteen's opening status.

func (*Opening) UnmarshalJSON

func (o *Opening) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a YYYY-MM-DD date to an Opening type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL