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, ConnectionTimeoutError, FatalConnectionError, FatalProtocolError, InvalidHeaderCharacterError, InvalidHeaderEscapeSequenceError, MalformedFrameError, MalformedHeaderError, OnStompError, StopReceiver, TransactionError, UnsupportedCommandError, UnsupportedProtocolVersionError

Constant Summary collapse

MAJOR =

Major / API version

1
MINOR =

Minor / feature version

0
PATCH =

Patch version

11
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


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

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)


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

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})


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

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.



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

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