Sha256: 2a71ead599700ecd55ddf25ac705b5ca6cc4959bcf25be477ebb4a976693719b

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

require 'active_support/string_inquirer'
require 'active_support/hash_with_indifferent_access'

module SubZero
  class Client
    autoload :Configuration, 'sub_zero/client/configuration'
    autoload :Socket,        'sub_zero/client/socket'

    include Socket

    attr_reader :sid, :config

    def initialize sid
      @sid, @config = sid.to_s.upcase, Configuration.default
      yield @config if block_given?
    end

    def call verb, payload = {}, options = {}
      sz_verb = verb.to_s.upcase.gsub('_', '/')
      status, result = zeromq_fetch(sz_verb, payload, options)
      result = handle_result(status, result).with_indifferent_access
      block_given? ? yield(result) : result
    end

    def call! verb, payload = {}, options = {}, &block
      call verb, payload, options, &block
    end

    def [] verb
      call verb
    end

    def method_missing method, payload = {}, options = {}, &block
      if method.to_s.end_with? '!'
        call! method.delete('!'), payload, options, &block
      else
        call method, payload, options, &block
      end
    end

    def handle_result status, result
      case status
      when :ok
        result
      when :error
        fail Error.new(result['error'])
      end
    end

    class Error < ::StandardError

      attr_reader :result

      def initialize result
        super result['message']
        set_backtrace caller
        @result = result
      end

      def code
        result['code']
      end

      def status
        ActiveSupport::StringInquirer.new self.code.to_s.downcase
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sub_zero-0.0.4 lib/sub_zero/client.rb