Sha256: 548c756817b5bbb66cd17d02c81c2748acb8fd31ca319d3e7f90a2b902429952

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

require 'http'

require_relative './attendance_report'

module Zoho
  module People
    class API
      URL = 'https://people.zoho.com/people/api'

      attr_reader :auth_token

      def initialize(auth_token: ENV['ZOHO_PEOPLE_AUTH_TOKEN'])
        raise 'You must provide Zoho People authentication token' unless auth_token

        @auth_token = auth_token
      end

      def post_attendance(attendance)
        params = {
          'authtoken' => auth_token,
          'dateFormat' => 'dd/MM/yyyy HH:mm:ss',
          'emailId' => attendance.email,
          'checkIn' => attendance.check_in.strftime('%d/%m/%Y %H:%M:%S'),
          'checkOut' => attendance.check_out.strftime('%d/%m/%Y %H:%M:%S')
        }

        request(:post, 'attendance', params: params)
      end

      def get_attendance_report(email:, from:, to:)
        params = {
          'authtoken' => auth_token,
          'dateFormat' => 'yyyy-MM-dd',
          'emailId' => email,
          'sdate' => from.strftime('%Y-%m-%d'),
          'edate' => to.strftime('%Y-%m-%d')
        }

        data = request(:get, 'attendance/getUserReport', params: params)

        AttendanceReport.parse(data)
      end

      private

      def request(method, endpoint, options)
        response = HTTP.method(method).call("#{URL}/#{endpoint}", options)

        if response.status.success?
          response.parse
        elsif response
          raise "Failed to #{method.to_s.upcase} #{endpoint}. Error: #{response.body}"
        else
          raise 'Failed to request Zoho API'
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
attend-1.0.0 lib/zoho/people/api.rb
attend-0.2.1 lib/zoho/people/api.rb