Sha256: f6c0ffffe05aaf43a2a4293f33807a2ebc500d67ed5db459361feb5691773003

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

module Pleiades
  module Client
    class Wrapper
      attr_reader :histories

      def initialize(client)
        @obj = client
        @histories = []
      end

      private

      def method_missing(method, *args, &block)
        return super unless @obj.respond_to?(method)

        execute(method, *args)
      end

      def respond_to_missing?(method, *_, &_)
        @obj.respond_to?(method) || super
      end

      def respond_to?(method)
        @obj.respond_to?(method) || super
      end

      def execute(method, *args)
        res = @obj.__send__ method, *args
        code = res.code
        body = JSON.parse(res.body)

        @histories << response.new(method, code, body)

        [code, body]
      end

      def method_assigns(method, *args)
        return {} if args.empty?

        @obj.method(method)
            .parameters
            .map(&:last)
            .each_with_index
            .each_with_object({}) do |(arg_name, at), hash|
              hash.store(arg_name, args[at])
            end
      end

      def response
        attributes = %i[method_name code body]
        Struct.new(*attributes)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pleiades-0.1.6 lib/pleiades/core/client/wrapper.rb
pleiades-0.1.5 lib/pleiades/core/client/wrapper.rb
pleiades-0.1.4 lib/pleiades/core/client/wrapper.rb
pleiades-0.1.3 lib/pleiades/core/client/wrapper.rb
pleiades-0.1.2 lib/pleiades/core/client/wrapper.rb