Sha256: ee9339e9d04d10c072fa1f0ddf291a1e5f94fdb9af45852ce2296af03258834e

Contents?: true

Size: 1.5 KB

Versions: 13

Compression:

Stored size: 1.5 KB

Contents

module PlatformSdk
  module Central
    class Client
      attr_reader :access_token, :base_url, :conn

      def initialize(base_url, access_token, conn: nil)
        @access_token = access_token
        @base_url = base_url
        @conn = conn || build_connection
      end

      def user(uid)
        resource_path = "/api/users/#{uid}"
        response = get(resource_path)
        response.body
      end

      def teacher_enrollments(external_course_id = nil)
        resource_path = '/api/teacher_enrollments'
        resource_path += "?external_course_id=#{external_course_id}" if external_course_id
        response = get(resource_path)
        response.body
      end

      def create_message(message_params: {})
        resource_path = '/api/messages'
        response = post(resource_path, message_params)
        response.body
      end

      private

      def build_connection
        Faraday.new(@base_url) do |faraday|
          faraday.headers = default_headers
          faraday.adapter Faraday.default_adapter
          faraday.response :json, content_type: /\bjson$/, parser_options: { symbolize_names: true }
          faraday.response :raise_error
        end
      end

      def default_headers
        {
          'Content-Type' => 'application/json',
          'Authorization' => "Bearer #{access_token}"
        }
      end

      def get(path, params = {})
        @conn.get(path, params)
      end

      def post(path, params = {})
        @conn.post(path, params.to_json)
      end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
strongmind-platform-sdk-3.22.0 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.21.8 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.21.7 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.21.6 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.21.5 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.21.4 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.21.3 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.21.2 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.21.1 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.21.0 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.20.3 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.20.2 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.20.1 lib/platform_sdk/central/client.rb