Sha256: 311d85d458938b9d7273b306c443cd2b3a638befd3dd8611a2fcf6e493c54d1a

Contents?: true

Size: 1012 Bytes

Versions: 6

Compression:

Stored size: 1012 Bytes

Contents

require 'json'

module Alephant
  module Broker
    class CallEnvironment
      include Logger
      attr_reader :settings

      def initialize(env)
        @settings = env
      end

      def method
        settings['REQUEST_METHOD']
      end

      def post?
        settings['REQUEST_METHOD'] == 'POST'
      end

      def get?
        settings['REQUEST_METHOD'] == 'GET'
      end

      def query
        settings['QUERY_STRING']
      end

      def path
        settings['PATH_INFO']
      end

      def request_type
        path.split('/')[1]
      end

      def data
        parse(rack_input) if post?
      end

      private

      def rack_input
          (settings['rack.input'].read).tap { settings['rack.input'].rewind } # http://rack.rubyforge.org/doc/SPEC.html
      end

      def parse(json)
        begin
          JSON.parse(json)
        rescue JSON::ParserError => e
          logger.info("Broker.environment#data: ParserError")
          nil
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
alephant-broker-0.1.6 lib/alephant/broker/call_environment.rb
alephant-broker-0.1.5 lib/alephant/broker/call_environment.rb
alephant-broker-0.1.4 lib/alephant/broker/call_environment.rb
alephant-broker-0.1.3 lib/alephant/broker/call_environment.rb
alephant-broker-0.1.2 lib/alephant/broker/call_environment.rb
alephant-broker-0.1.1 lib/alephant/broker/call_environment.rb