module MMonad::Client::Macros

Public Instance Methods

<<(data)
Alias for: message
endpoint(address) click to toggle source

@params address [String] describes the socket,

including protocol and networking port, if applicable
# File lib/m_monad/client.rb, line 12
def endpoint(address)
  @endpoint = address
end
message(data) click to toggle source

@params data [Hash] provides the caller a method to send

data into the 0MQ socket and receive a parsed response
as defined by the `pattern` Proc provided above. The
expectation is that the `pattern` finalizes as a Hash.

@returns [Hash]

# File lib/m_monad/client.rb, line 43
def message(data)
  Timeout.timeout(@timeout) do
    interaction = @pattern.new(@endpoint)
    interaction << data.to_json
    unwrap(interaction.receive)
  end
end
Also aliased as: <<
pattern(interface) click to toggle source

@params interface [Symbol] describes the 0MQ wrapper

interface for connecting to the socket address. Only wrappers
for explicitly supported 0MQ patterns are accepted.

@raises MMonad::PatternNotAllowed

# File lib/m_monad/client.rb, line 20
def pattern(interface)
  @pattern = MMonad::Pattern.find_client(interface)
end
response(&handling) click to toggle source

@params response [Proc] provides the DSL hook for

extending classes to define the message response
processing behavior

@returns [Hash]

# File lib/m_monad/client.rb, line 28
def response(&handling)
  @response = handling
end
timeout(seconds = 5) click to toggle source

@params seconds [Integer] provides the maximum number of

seconds a client should wait for data from the socket
# File lib/m_monad/client.rb, line 34
def timeout(seconds = 5)
  @timeout = seconds
end

Private Instance Methods

unwrap(resp) click to toggle source
# File lib/m_monad/client.rb, line 54
def unwrap(resp)
  raw = JSON.parse(resp.to_a.first)

  @response.call(raw)
end