Sha256: 67d3864156bcf825e31cff4ddb5f65abe3e159068aad13646ec26a1f6fc62ffe

Contents?: true

Size: 889 Bytes

Versions: 1

Compression:

Stored size: 889 Bytes

Contents

# frozen_string_literal: true

require "base64"

module Whats
  module Actions
    class Login
      PATH = "/v1/users/login"

      def initialize
        @user = Whats.configuration.user
        @password = Whats.configuration.password
        @client = Whats::Client.new(encoded_auth, :basic)
      end

      def call
        client.request(PATH)
      end

      def token
        return @token if valid?

        extract_atributes call

        @token
      end

      private

      attr_reader :client

      def extract_atributes(response)
        @token = response["users"].first["token"]
        @expires_after = response["users"].first["expires_after"]
      end

      def encoded_auth
        Base64.encode64("#{@user}:#{@password}").chomp
      end

      def valid?
        return false if @expires_after.nil?

        @expires_after > Time.now
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
whatsapp-1.0.0 lib/whats/actions/login.rb