Sha256: f4d7461007fa513f14a5352ff7d3153ad14236540cf64534999482d01e185124

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true
require 'faraday'
require 'json'

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

    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<PennState::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 [PennState::SearchService::Person, nil]
    def process_userid_response(response)
      return 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

1 entries across 1 versions & 1 rubygems

Version Path
psu_identity-0.1.2 lib/penn_state/search_service/client.rb