Module: OnStomp

Defined in:
lib/onstomp.rb,
lib/onstomp/version.rb

Overview

Primary namespace for the onstomp gem

Defined Under Namespace

Modules: Components, Connections, Failover, Interfaces, OpenURI Classes: Client, ConnectFailedError, FatalConnectionError, FatalProtocolError, InvalidHeaderCharacterError, InvalidHeaderEscapeSequenceError, MalformedFrameError, MalformedHeaderError, OnStompError, StopReceiver, TransactionError, UnsupportedCommandError, UnsupportedProtocolVersionError

Constant Summary collapse

ENUMERATOR_KLASS =

Class to use for creating enumerator objects, which depends upon the version of Ruby being used.

(RUBY_VERSION >= '1.9') ? Enumerator : Enumerable::Enumerator
MAJOR =

Major / API version

1
MINOR =

Minor / feature version

0
PATCH =

Patch version

0
VERSION =

Complete version

"#{MAJOR}.#{MINOR}.#{PATCH}"

Class Method Summary collapse

Class Method Details

.connect(uri, options = {}) ⇒ Object Also known as: open

Creates a new connection and immediately connects it to the broker.

See Also:

  • #initialize


93
94
95
96
97
# File 'lib/onstomp.rb', line 93

def connect(uri, options={})
  conx = OnStomp::Client.new(uri, options)
  conx.connect
  conx
end

.constantize(klass) ⇒ Module

Converts a string to the Ruby constant it names. If the klass parameter is a kind of Module, this method will return klass directly.

Examples:

OnStomp.constantize('OnStomp::Frame') #=> OnStomp::Frame
OnStomp.constantize('This::Constant::DoesNotExist) #=> raises NameError
OnStomp.constantize(Symbol) #=> Symbol 

Parameters:

  • klass (String, Module)

Returns:

  • (Module)


137
138
139
140
141
142
143
144
# File 'lib/onstomp.rb', line 137

def constantize(klass)
  return klass if klass.is_a?(Module) || klass.nil? || klass.respond_to?(:new)
  klass.to_s.split('::').inject(Object) do |const, named|
    next const if named.empty?
    const.const_defined?(named) ? const.const_get(named) :
      const.const_missing(named)
  end
end

.keys_to_sym(hsh) ⇒ {Symbol => Object}

Duplicates an existing hash while transforming its keys to symbols. The keys must implement the to_sym method, otherwise an exception will be raised. This method is used internally to convert hashes keyed with Strings.

Examples:

hash = { '10' => nil, 'key2' => [3, 5, 8, 13, 21], :other => :value }
OnStomp.keys_to_sym(hash) #=> { :'10' => nil, :key2 => [3, 5, 8, 13, 21], :other => :value }
hash #=> { '10' => nil, 'key2' => [3, 5, 8, 13, 21], :other => :value }

Parameters:

  • hsh ({Object => Object})

    The hash to convert. It’s keys must respond to to_sym.

Returns:

  • ({Symbol => Object})


111
112
113
114
115
116
# File 'lib/onstomp.rb', line 111

def keys_to_sym(hsh)
  hsh.inject({}) do |new_hash, (k,v)|
    new_hash[k.to_sym] = v
    new_hash
  end
end

.next_serial(prefix = nil) ⇒ Object

Generates the next serial number in a thread-safe manner. This method merely initializes an instance variable to 0 if it has not been set, then increments this value and returns its string representation.



121
122
123
124
125
126
127
# File 'lib/onstomp.rb', line 121

def next_serial(prefix=nil)
  Thread.exclusive do
    @next_serial_sequence ||= 0
    @next_serial_sequence += 1
    @next_serial_sequence.to_s
  end
end