Sha256: ce77b581ceda6ce922be7184d5b6bee63832cd122a29a19e6325a299cc1d1bc9

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

#!/usr/bin/env ruby

require 'clamp'
require 'date'
require 'httparty'

AUTH_TOKEN = ENV['ZOHO_PEOPLE_AUTH_TOKEN']
DATE_FORMAT = 'dd/MM/yyyy HH:mm:ss'

parse_date = ->(v) { Date.parse(v) }

Clamp do
  option '--email', 'EMAIL', 'employee email id', required: true
  option '--check_in', 'CHECK_IN', 'check in time', required: true
  option '--check_out', 'CHECK_OUT', 'check out time', required: true
  option '--from', 'FROM', 'date of the first work day', default: Date.today, &parse_date
  option '--to', 'TO', 'date of the las work day', default: Date.today, &parse_date
  option '--dry', :flag, 'enables dry run'

  def execute
    days_between = from.step(to).reject(&:sunday?).reject(&:saturday?)

    days_between.map(&:to_datetime).each do |date_time|
      check_in_time = date_time.new_offset("+#{check_in}")
      check_out_time = date_time.new_offset("+#{check_out}")

      if dry?
        puts "DRY RUN: Committing attendance from #{check_in_time} to #{check_out_time}"
        next
      end

      response = post_attendance(check_in_time, check_out_time)

      if response.ok?
        puts "Successfully registered attendance from #{check_in_time} to #{check_out_time}"
      elsif response
        raise "Failed to register attendance. Error: #{response.body}"
      end
    end
  end

  def post_attendance(check_in_time, check_out_time)
    parameters = {
      'authtoken' => AUTH_TOKEN,
      'dateFormat' => DATE_FORMAT,
      'checkIn' => check_in_time.strftime('%d/%m/%Y %H:%M:%S'),
      'checkOut' => check_out_time.strftime('%d/%m/%Y %H:%M:%S'),
      'emailId' => email,
    }

    HTTParty.post('https://people.zoho.com/people/api/attendance', query: parameters)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
attend-0.1.0 exe/attend