Sha256: 64331db310cfcecdbc153843f05ebfd2c919aaf5df04c5ddf1d7ddf3a35d823c

Contents?: true

Size: 891 Bytes

Versions: 5

Compression:

Stored size: 891 Bytes

Contents

# frozen_string_literal: true

module JabberAdmin
  # Error that will be raised when API call is not successful
  class ApiError < StandardError; end

  # Handles the communication with the API
  class ApiCall
    attr_reader :command, :payload

    def self.perform(command, payload = {})
      new(command, payload).perform
    end

    def initialize(command, payload)
      @command = command
      @payload = payload
    end

    def perform
      raise(ApiError, response.to_s) if response.code != 200

      response
    end

    private

    def response
      @res ||= RestClient::Request.execute(
        method: :post,
        url: url,
        user: JabberAdmin.configuration.admin,
        password: JabberAdmin.configuration.password,
        payload: payload.to_json
      )
    end

    def url
      "#{JabberAdmin.configuration.api_host}/api/#{@command}"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
jabber_admin-0.1.4 lib/jabber_admin/api_call.rb
jabber_admin-0.1.3 lib/jabber_admin/api_call.rb
jabber_admin-0.1.2 lib/jabber_admin/api_call.rb
jabber_admin-0.1.1 lib/jabber_admin/api_call.rb
jabber_admin-0.1.0 lib/jabber_admin/api_call.rb