Sha256: 147d4ab50aec712d3595d96bb52d7c20a6892ef6ba8664fd893344e7e0a02ef8

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module FaithTeams
  module API
    module V2
      module Resource
        # User resource
        class User < Base
          # @return [String] auth_token
          # @raise [Error::Request]
          def authenticate
            response = http.post("#{FaithTeams::API::V2::Connection::ENDPOINT_BASE_URLS["authenticate"]}/authenticate")

            # raise errors
            raise Error::Request.new(response: response, message: "Request unsuccessful (#{response.status})") unless response.status.success?

            # parse response
            begin
              response_body = response.parse(:json)
            rescue JSON::ParserError
              raise Error::Request.new(response: response, message: "Failed to parse JSON")
            end

            response_body.dig("data", "token")
          end

          private

          attr_reader :http

          # @return [HTTP::Client]
          def http
            user_credentials = Base64.strict_encode64("#{connection.user_id}:#{connection.password}")
            @http ||= HTTP.auth("Basic #{user_credentials}")
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
faithteams-api-2.0.2 lib/faithteams/api/v2/resource/user.rb
faithteams-api-2.0.1 lib/faithteams/api/v2/resource/user.rb