Sha256: 904ccdee2f685e793e4f035be73098375bb9f3a6a4701af26cf756d9fc32ebfa

Contents?: true

Size: 1.38 KB

Versions: 31

Compression:

Stored size: 1.38 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

    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?
        fail "action ##{@data[:id]} (#{label}) cannot be cancelled"
      end

      @cancel = true
      @cancel_block = block
    end

    def cancel?
      @cancel === true
    end

    def cancel_block
      @cancel_block
    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

31 entries across 31 versions & 1 rubygems

Version Path
haveapi-client-0.20.0 lib/haveapi/client/action_state.rb
haveapi-client-0.19.3 lib/haveapi/client/action_state.rb
haveapi-client-0.19.2 lib/haveapi/client/action_state.rb
haveapi-client-0.19.1 lib/haveapi/client/action_state.rb
haveapi-client-0.19.0 lib/haveapi/client/action_state.rb
haveapi-client-0.18.2 lib/haveapi/client/action_state.rb
haveapi-client-0.18.1 lib/haveapi/client/action_state.rb
haveapi-client-0.18.0 lib/haveapi/client/action_state.rb
haveapi-client-0.17.0 lib/haveapi/client/action_state.rb
haveapi-client-0.16.3 lib/haveapi/client/action_state.rb
haveapi-client-0.16.2 lib/haveapi/client/action_state.rb
haveapi-client-0.16.1 lib/haveapi/client/action_state.rb
haveapi-client-0.16.0 lib/haveapi/client/action_state.rb
haveapi-client-0.15.1 lib/haveapi/client/action_state.rb
haveapi-client-0.15.0 lib/haveapi/client/action_state.rb
haveapi-client-0.14.2 lib/haveapi/client/action_state.rb
haveapi-client-0.14.1 lib/haveapi/client/action_state.rb
haveapi-client-0.14.0 lib/haveapi/client/action_state.rb
haveapi-client-0.13.3 lib/haveapi/client/action_state.rb
haveapi-client-0.13.2 lib/haveapi/client/action_state.rb