Sha256: 7c045de9abd6dd2f3b50a9d7151369afa61dab3b1a07ba5bd1dfe770c9f5b526

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

require_relative 'team_member'
require_relative 'team_subscription'
require_relative 'team'
require_relative 'token'
require_relative 'user'

module Spearly
  module Auth
    class Client
      include Team
      include TeamMember
      include TeamSubscription
      include Token
      include User

      def initialize(token)
        @token = token

        base_url = ENV.fetch('SPEARLY_API_URL', nil)
        @client = Faraday.new(base_url)
      end

      def run_request(method:, path:, params: nil)
        path_parsed = Addressable::URI.parse(path).normalize.to_s

        @client.run_request(method.to_sym,
                            path_parsed,
                            params,
                            'Accept' => 'application/vnd.spearly.v2+json',
                            'Authorization' => @token)
      rescue Faraday::ConnectionFailed => e
        raise ServerError, e.message
      end

      def process_response(response)
        case response.status
        when 200, 201, 422
          JSON.parse(response.body)
        when 401
          raise AuthorizationError, 'Unauthorized'
        when 404
          raise NotFoundError, 'Not Found'
        when 503
          raise ServiceUnavailableError, 'Service Unavailable'
        else
          raise ServerError, 'Server Error'
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spearly-sdk-ruby-0.11.0 lib/spearly/auth/client.rb