Sha256: 081ed270908aa0cb24f934d102a305763c21f921907bac3618a0e7e3624e0f0f

Contents?: true

Size: 1.34 KB

Versions: 14

Compression:

Stored size: 1.34 KB

Contents

module HaveAPI::Client
  # Represents action's state as returned from API resource ActionState.Show/Poll.
  class ActionState
    Progress = Struct.new(:current, :total, :unit) do
      def percent
        100.0 / total * current
      end

      def to_s
        "#{current}/#{total} #{unit}"
      end
    end

    attr_reader :progress, :cancel_block

    def initialize(response)
      @data = response.response

      @progress = Progress.new(@data[:current], @data[:total], @data[:unit])
    end

    def label
      @data[:label]
    end

    def status
      @data[:status] === true
    end

    def finished?
      @data[:finished] === true
    end

    def can_cancel?
      @data[:can_cancel] === true
    end

    # Stop monitoring the action's state and attempt to cancel it. The `block`
    # is given to Action.wait_for_completion for the cancel operation. The block
    # is used only if the cancel operation is blocking.
    def cancel(&block)
      unless can_cancel?
        raise "action ##{@data[:id]} (#{label}) cannot be cancelled"
      end

      @cancel = true
      @cancel_block = block
    end

    def cancel?
      @cancel === true
    end

    # Stop monitoring the action's state, the call from Action.wait_for_completion
    # will return.
    def stop
      @stop = true
    end

    def stop?
      !@stop.nil? && @stop
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
haveapi-client-0.25.0 lib/haveapi/client/action_state.rb
haveapi-client-0.24.0 lib/haveapi/client/action_state.rb
haveapi-client-0.23.7 lib/haveapi/client/action_state.rb
haveapi-client-0.23.6 lib/haveapi/client/action_state.rb
haveapi-client-0.23.5 lib/haveapi/client/action_state.rb
haveapi-client-0.23.4 lib/haveapi/client/action_state.rb
haveapi-client-0.23.3 lib/haveapi/client/action_state.rb
haveapi-client-0.23.2 lib/haveapi/client/action_state.rb
haveapi-client-0.23.1 lib/haveapi/client/action_state.rb
haveapi-client-0.23.0 lib/haveapi/client/action_state.rb
haveapi-client-0.22.1 lib/haveapi/client/action_state.rb
haveapi-client-0.22.0 lib/haveapi/client/action_state.rb
haveapi-client-0.21.1 lib/haveapi/client/action_state.rb
haveapi-client-0.21.0 lib/haveapi/client/action_state.rb