Sha256: f849043d3964bf364266202b93b6c885064e4b4c6d4aab49a9f08059718233bb

Contents?: true

Size: 1.3 KB

Versions: 37

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Decidim
  module Etherpad
    # This class allows you to interact with pad stored in an Etherpad Lite server.
    class Pad
      def initialize(pad_id)
        @id = pad_id
        @api_key = Decidim.etherpad.fetch(:api_key)
        @api_version = Decidim.etherpad.fetch(:api_version, "1.2.1")
        @uri = URI.parse(Decidim.etherpad.fetch(:server))
      end

      attr_reader :id

      # Read only means that pad is not writable.
      def read_only_id
        @read_only_id ||= resolve(:getReadOnlyID, { padID: id })[:readOnlyID]
      end

      def text
        resolve(:getText, { padID: id })[:text]
      end

      private

      attr_reader :api_key, :uri, :api_version

      def resolve(method, params = {})
        path = "/api/#{api_version}/#{method}"
        result = get(path, params)
        response = JSON.parse(result.body.to_s, symbolize_names: true)

        case response[:code]
        when 0 then response[:data]
        when (1..4) then raise StandardError, response[:message]
        else raise Error, "An unknown error ocurred while handling the API response: #{response}"
        end
      end

      # Makes a GET request
      def get(path, params = {})
        params[:apikey] = api_key
        Faraday.get("#{uri}#{path}", params)
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
decidim-core-0.29.1 lib/decidim/etherpad/pad.rb
decidim-core-0.28.4 lib/decidim/etherpad/pad.rb
decidim-core-0.27.9 lib/decidim/etherpad/pad.rb
decidim-core-0.29.0 lib/decidim/etherpad/pad.rb
decidim-core-0.28.3 lib/decidim/etherpad/pad.rb
decidim-core-0.27.8 lib/decidim/etherpad/pad.rb
decidim-core-0.29.0.rc4 lib/decidim/etherpad/pad.rb
decidim-core-0.29.0.rc3 lib/decidim/etherpad/pad.rb
decidim-core-0.29.0.rc2 lib/decidim/etherpad/pad.rb
decidim-core-0.29.0.rc1 lib/decidim/etherpad/pad.rb
decidim-core-0.28.2 lib/decidim/etherpad/pad.rb
decidim-core-0.27.7 lib/decidim/etherpad/pad.rb
decidim-core-0.28.1 lib/decidim/etherpad/pad.rb
decidim-core-0.27.6 lib/decidim/etherpad/pad.rb
decidim-core-0.26.10 lib/decidim/etherpad/pad.rb
decidim-core-0.26.9 lib/decidim/etherpad/pad.rb
decidim-core-0.28.0 lib/decidim/etherpad/pad.rb
decidim-core-0.27.5 lib/decidim/etherpad/pad.rb
decidim-core-0.28.0.rc5 lib/decidim/etherpad/pad.rb
decidim-core-0.28.0.rc4 lib/decidim/etherpad/pad.rb