Sha256: a10f037aac4df3c5b3b64415cf519ee38e5efc02cbe93ef67c337b2383eac48a

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

require 'json'
require 'alephant/logger'
require 'rack'

module Alephant
  module Broker
    class Environment
      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 data
        parse(rack_input) if post?
      end

      def options
        Rack::Utils.parse_nested_query query
      end

      private

      def rack_input
        (settings['rack.input'].read).tap { settings['rack.input'].rewind }
      end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
alephant-broker-3.5.4 lib/alephant/broker/environment.rb
alephant-broker-3.5.3 lib/alephant/broker/environment.rb
alephant-broker-3.5.2 lib/alephant/broker/environment.rb