Sha256: 920f48c62ff5a7d8bae5708435591c3b691fc02db14657637bdb7180c8745780

Contents?: true

Size: 1.11 KB

Versions: 12

Compression:

Stored size: 1.11 KB

Contents

require 'json'
require 'alephant/logger'

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 options
        query.split('&').reduce({}) do |object, key_pair|
          key, value = key_pair.split('=')
          object.tap { |o| o.store(key.to_sym, value) }
        end
      end

      def path
        settings['PATH_INFO']
      end

      def data
        parse(rack_input) if post?
      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")
          nil
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
alephant-broker-2.0.1 lib/alephant/broker/environment.rb
alephant-broker-2.0.0 lib/alephant/broker/environment.rb
alephant-broker-1.3.1 lib/alephant/broker/environment.rb
alephant-broker-1.2.1 lib/alephant/broker/environment.rb
alephant-broker-1.2.0 lib/alephant/broker/environment.rb
alephant-broker-1.1.3 lib/alephant/broker/environment.rb
alephant-broker-1.1.2 lib/alephant/broker/environment.rb
alephant-broker-1.1.1 lib/alephant/broker/environment.rb
alephant-broker-1.1.0 lib/alephant/broker/environment.rb
alephant-broker-1.0.5 lib/alephant/broker/environment.rb
alephant-broker-1.0.4 lib/alephant/broker/environment.rb
alephant-broker-1.0.3 lib/alephant/broker/environment.rb