hpi-swa-teaching / MensaWidget

Mensa Widget
MIT License
2 stars 0 forks source link

research the API calls #1

Closed lchristus closed 4 months ago

lchristus commented 5 months ago

check out the different sources to find out how to gain knowledge on how to pull the information.

OscarUlb commented 4 months ago

This is working workspace code to fetch, parse and output the meal information of a day and outlet in the Transkript:

| url response jsonData parsedData name studentPrice |
url := 'https://openmensa.org/api/v2/canteens/62/days/2024-05-21/meals'.  
response := WebClient httpGet: url.
jsonData := response content.
parsedData := STON fromString: jsonData.
parsedData do: [:each | 
    name := each at: 'name'.
    studentPrice := (each at: 'prices') at: 'students'.
    Transcript show: name, ': ', studentPrice printString; cr.
].
OscarUlb commented 4 months ago

To get the meals of the day, we need the current date in the format: YYYY-MM-DD. I could not find a Squeak function that returns the date in the required format, so I wrote this code to transform the date and ensure that our requirements are satisfied.

| currentDate year month day monthMapping |
year := (Date today year) asString.
month := (Date today month asString copyFrom: 1 to: (Date today month asString indexOf: $ ) - 1).
day := (Date today dayOfMonth) asString.
monthMapping := Dictionary newFrom: {
    'January' -> '01'.
    'February' -> '02'.
    'March' -> '03'.
    'April' -> '04'.
    'May' -> '05'.
    'June' -> '06'.
    'July' -> '07'.
    'August' -> '08'.
    'September' -> '09'.
    'October' -> '10'.
    'November' -> '11'.
    'December' -> '12'
}.
month := monthMapping at: month.
day := day size = 1 ifTrue: ['0', day] ifFalse: [day].
currentDate := year, '-', month, '-', day.
Transcript show: currentDate; cr.
lchristus commented 4 months ago

To get the meals of the day, we need the current date in the format: YYYY-MM-DD. I could not find a Squeak function that returns the date in the required format, so I wrote this code to transform the date and ensure that our requirements are satisfied.

| currentDate year month day monthMapping |
year := (Date today year) asString.
month := (Date today month asString copyFrom: 1 to: (Date today month asString indexOf: $ ) - 1).
day := (Date today dayOfMonth) asString.
monthMapping := Dictionary newFrom: {
    'January' -> '01'.
    'February' -> '02'.
    'March' -> '03'.
    'April' -> '04'.
    'May' -> '05'.
    'June' -> '06'.
    'July' -> '07'.
    'August' -> '08'.
    'September' -> '09'.
    'October' -> '10'.
    'November' -> '11'.
    'December' -> '12'
}.
month := monthMapping at: month.
day := day size = 1 ifTrue: ['0', day] ifFalse: [day].
currentDate := year, '-', month, '-', day.
Transcript show: currentDate; cr.

Yes there is not one function that returns it directly but by truncating strings you can do the following

DateAndTime now asString truncateTo: 10

and it will return a string looking like this

 '2024-05-21' 

I think we should use that since it is simpler and already build in