Sha256: f156376502c103ab621172dec8abe58995180e8cb47e40e9b377b34b8812f5ad

Contents?: true

Size: 1.68 KB

Versions: 6

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

# @abstract Client for querying Penn State's identity API: https://identity.apps.psu.edu/search-service/resources
module PsuIdentity::SearchService
  class Error < StandardError; end

  class NotFound < StandardError; end

  class Client
    attr_reader :base_url

    # @param [String] base_url
    def initialize(base_url: '/search-service/resources')
      @base_url = base_url
    end

    # @param [Hash] args of options to pass to the endpoint
    # @option args [String] :text to search for
    def search(**args)
      process_response connection.get("#{base_url}/people", args)
    end

    # @param [Hash] args of options to pass to the endpoint
    # @option args [String] :userid of the person
    def userid(userid)
      process_userid_response connection.get("#{base_url}/people/userid/#{userid}")
    end

    private

      # @return Array<PsuIdentity::SearchService::Person>
      def process_response(response)
        raise Error.new(response.body) unless response.success?

        JSON.parse(response.body).map { |result| Person.new(result) }
      rescue JSON::ParserError
        []
      end

      # @return [PsuIdentity::SearchService::Person, nil]
      def process_userid_response(response)
        raise NotFound if response.status == 404

        raise Error.new(response.body) unless response.success?

        Person.new(JSON.parse(response.body))
      rescue JSON::ParserError
      end

      def connection
        @connection ||= Faraday.new(url: endpoint) do |conn|
          conn.adapter :net_http
        end
      end

      def endpoint
        @endpoint ||= ENV.fetch('IDENTITY_ENDPOINT', 'https://identity.apps.psu.edu')
      end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
psu_identity-0.6.1 lib/psu_identity/search_service/client.rb
psu_identity-0.6.0 lib/psu_identity/search_service/client.rb
psu_identity-0.5.1 lib/psu_identity/search_service/client.rb
psu_identity-0.5.0 lib/psu_identity/search_service/client.rb
psu_identity-0.4.0 lib/psu_identity/search_service/client.rb
psu_identity-0.3.0 lib/psu_identity/search_service/client.rb