Sha256: 581503a62ec3f0404b8b47b5828a36a11fdc9dc42af1bef8da2bdc8d9fdadc15

Contents?: true

Size: 1.73 KB

Versions: 97

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module PlatformSdk
  module Edkey
    class Client
      attr_reader :base_url, :client_id, :client_secret, :token, :token_expires_at, :conn

      def initialize(base_url, client_id, client_secret)
        @client_id = client_id
        @client_secret = client_secret
        @base_url = base_url

        # @token = "q8XNbZJRJjB4AkCgVlGAxjvEFYm7xeK9"

        @conn = Faraday.new(base_url) do |faraday|
          faraday.adapter :net_http
          faraday.request :json
          faraday.response :json
          faraday.response :raise_error
        end
      end

      def headers
        {
          "Authorization" => "Bearer #{access_token}",
          "Content-Type" => "application/json",
          "Accept" => "application/json"
        }
      end

      def access_token
        if expired?
          response = @conn.post("api/v2/auth/request-token",
                                token_request_body.to_json,
                                { "Content-Type" => "application/json", "Accept" => "*/*" })
          @token = response.body["token"]
          @token_expires_at = DateTime.strptime(response.body["expires_at"], "%Y-%m-%d %H:%M:%S")
        end

        @token
      end

      def expired?
        return true if @token.nil?

        @token_expires_at < DateTime.now
      end

      def attendance(start_date, end_date)
        path = "/api/v2/attendance-exporter/long-term"
        body = {
          startDate: start_date,
          endDate: end_date
        }.to_json
        response = @conn.post(path, body, headers)
        response.body
      end

      private

      def token_request_body
        {
          clientId: @client_id,
          clientSecret: @client_secret
        }
      end
    end
  end
end

Version data entries

97 entries across 97 versions & 1 rubygems

Version Path
strongmind-platform-sdk-3.26.0 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.25.0 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.24.0 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.23.7 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.23.6 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.23.5 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.23.4 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.23.3 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.23.2 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.23.1 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.23.0 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.22.1 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.22.0 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.21.8 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.21.7 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.21.6 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.21.5 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.21.4 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.21.3 lib/platform_sdk/edkey/client.rb
strongmind-platform-sdk-3.21.2 lib/platform_sdk/edkey/client.rb