Sha256: 2bd1268fc1dbd23fde91cccf7d863f5bc709a52cf45c68b0945bed00740360db

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

require 'restpack-core-client'

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

        options[:core_domain] ||= ENV['RESTPACK_CORE_SERVICE']
        options[:access_key] ||= ENV['RESTPACK_ACCESS_KEY']
        
        p "RestPack::Web::App.initialize: core_domain #{options[:core_domain]}"
        @options = options
        
        unless options[:core_domain] || options[:access_key]
          p "[WARNING] Missing RestPack configuration"
        else
          @core_cache = RestPack::Core::Client::Cache.create(options[:core_domain], options[:access_key])
        end
      end

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

        #TODO: GJ: ignore requests with a file extension
        request = Rack::Request.new(env)
        channel = @core_cache.get_channel(request.host)
        domain = channel.get_domain_by_host(request.host)
        
        env[:restpack][:request] = request
        env[:restpack][:host] = request.host
        env[:restpack][:channel] = channel
        env[:restpack][:application] = domain.application
        env[:restpack][:domain] = domain
        env[:restpack][:configurations] = @core_cache.configurations
        env[:restpack][:services] = @core_cache.get_configuration_value('services', [])
        
        add_user env

        @app.call(env)
      end
      
      private
      
      def add_user(env)
        user_id = env['rack.session'][:user_id]

        if user_id
          url = "http://:#{@options[:access_key]}@#{ENV['RESTPACK_USER_SERVICE']}/api/v1/users/#{user_id}.json" #TODO: GJ: build from config
          json = RestClient.get(url)
          user = Yajl::Parser.parse(json, :symbolize_keys => true)[:user]

          env[:restpack][:user] = 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.26 lib/restpack-web/app.rb