Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An Example Employee Time Clock GUI using Tk Backed by Square's SDK
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'date'
require 'pp'
require 'tk'
require 'square_connect'
SquareConnect.configure { |config| config.access_token = ENV.fetch 'SQUARE_CONNECT_TOKEN' }
api_instance = SquareConnect::V1EmployeesApi.new
employees = api_instance.list_employees.map { |employee| [employee.first_name, employee.id] }.to_h
##
# A GUI for Submitting Timecards
app_title = 'Submit a Timecard'
root = TkRoot.new
root.title = app_title
##
# Fonts
header_label_font = TkFont.new size: 20, weight: 'bold'
response_label_font = TkFont.new size: 19
regular_font = TkFont.new size: 15
##
# Labels and Fields
header_label = TkLabel.new root, text: app_title, font: header_label_font
option_menu = TkOptionMenubutton.new root, values: employees.keys, font: regular_font
clockin_field_label = TkLabel.new root, text: 'Clock-in', font: regular_font
clockin_field = TkEntry.new root
clockin_field.value = DateTime.now.iso8601
clockout_field_label = TkLabel.new root, text: 'Clock-out', font: regular_font
clockout_field = TkEntry.new root
response_label = TkLabel.new root, text: '⚗️', font: response_label_font
##
# Submit Button
submit_button = TkButton.new root do
text 'Submit'
font regular_font
command -> do
response_label.text = begin
options = {employee_id: employees[option_menu.value]}
options.merge!(clockin_time: clockin_field.value) unless clockin_field.value.empty?
options.merge!(clockout_time: clockout_field.value) unless clockout_field.value.empty?
timecard = SquareConnect::V1Timecard.new options
result = api_instance.create_timecard timecard
pp result
"Success! Confirmation code: #{result.id}"
rescue SquareConnect::ApiError => e
warn "#{e.class}: #{e.message}"
e.class.name.rpartition('::').last
end
end
end
clockin_field.bind 'Return', ->{ submit_button.invoke }
##
# The View Grid
padding = {padx: 20, pady: 20}
Tk.grid header_label, columnspan: 4, **padding
Tk.grid option_menu, columnspan: 4
Tk.grid clockin_field_label, clockin_field, columnspan: 2, **padding
Tk.grid clockout_field_label, clockout_field, columnspan: 2, **padding
Tk.grid response_label, columnspan: 4, **padding
Tk.grid submit_button, sticky: 'we', columnspan: 4, **padding
Tk.mainloop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment