Sha256: 384e20be787e3b1a6f3c03641c433382144aef6a6ef869f061d68b141e3516a2

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

require 'restpack_core_client'

module RestPack
  module Web
    class App
      def initialize(app, options = {})
        @app = app

        options[:core_domain] ||= ENV['RESTPACK_CORE_SERVICE']
        options[:access_key] ||= ENV['RESTPACK_ACCESS_KEY']

        raise "missing RestPack configuration" unless options[:core_domain] || options[:access_key]

        @core_cache = RestPack::Core::Client::Cache.create(options[:core_domain], options[:access_key])
      end

      def call(env)
        env[:restpack] ||= {}

        request = Rack::Request.new(env)

        domain = @core_cache.get_domain(request.host)

        env[:restpack][:request] = request
        env[:restpack][:host] = request.host
        env[:restpack][:channel] = domain.channel
        env[:restpack][:application] = domain.application
        env[:restpack][:domain] = domain
        env[:restpack][:configurations] = @core_cache.configurations

        add_user env

        @app.call(env)
      end

      private

      def add_user(env)
        return unless env['rack.session']

        user_id = env['rack.session'][:user_id]

        if user_id
          context = RestPack::Web::Context.new(env)

          user_service_domain = context.get_service_domain('restpack-user-service')
          json = RestClient.get("http://:#{ENV['RESTPACK_ACCESS_KEY']}@#{user_service_domain}/api/v1/users/#{user_id}.json")
          env[:restpack][:user] = Yajl::Parser.parse(json, :symbolize_keys => true)[:user]
        end

        env[:restpack][:user_id] = user_id
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
restpack_web-0.2.21 lib/restpack_web/app.rb