Sha256: b67f2ebdc4993ddf52fe944b63bdd2a4514ff55618990a8a4c8001ed1d75ef71

Contents?: true

Size: 1.48 KB

Versions: 6

Compression:

Stored size: 1.48 KB

Contents

require 'artoo/adaptors/adaptor'

module Artoo
  module Adaptors
    # Connect to a Sphero device
    # @see http://gosphero.com Sphero information
    # @see http://rubydoc.info/gems/hybridgroup-sphero Sphero gem Documentation
    class Sphero < Adaptor
      finalizer :finalize
      attr_reader :sphero

      # Number of retries when connecting
      RETRY_COUNT = 5

      # Closes connection with device if connected
      # @return [Boolean]
      def finalize
        disconnect if connected?
      end

      # Creates a connection with Sphero object with retries
      # @return [Boolean]
      def connect
        @retries_left = RETRY_COUNT
        require 'sphero' unless defined?(::Sphero)
        begin
          @sphero = ::Sphero.new(connect_to)
          super
          return true
        rescue Errno::EBUSY, Errno::ECONNREFUSED => e
          @retries_left -= 1
          if @retries_left > 0
            retry
          else
            Logger.error e.message
            Logger.error e.backtrace.inspect
            return false
          end
        end
      end

      # Closes connection with device
      # @return [Boolean]
      def disconnect
        sphero.close if connected?

        super
      end

      # Uses method missing to call sphero actions
      # @see http://rubydoc.info/gems/hybridgroup-sphero/Sphero Sphero documentation
      def method_missing(method_name, *arguments, &block)
        sphero.send(method_name, *arguments, &block)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
artoo-sphero-1.1.1 lib/artoo/adaptors/sphero.rb
artoo-sphero-1.1.0 lib/artoo/adaptors/sphero.rb
artoo-sphero-1.0.1 lib/artoo/adaptors/sphero.rb
artoo-sphero-1.0.1.pre lib/artoo/adaptors/sphero.rb
artoo-sphero-1.0.0 lib/artoo/adaptors/sphero.rb
artoo-sphero-1.0.0.rc2 lib/artoo/adaptors/sphero.rb