Sha256: bf1ae6729100c0f05bbb1e17c1c36cb1a6c2845646a3488c4846b446ae3bfd53

Contents?: true

Size: 1.68 KB

Versions: 8

Compression:

Stored size: 1.68 KB

Contents

require 'rack'

module Helios
  class Backend < Rack::Builder
    DEFAULT_PATHS = {
      data: '/'
    }

    require 'rails-database-url' if const_defined?(:Rails)

    def initialize(*args, &block)
      raise ArgumentError, "Missing block" unless block_given?
      super(&nil)

      @services = {}

      instance_eval(&block)
    end

    def call(env)
      return super(env) unless env["REQUEST_METHOD"] == "OPTIONS" and env["REQUEST_PATH"] == "/"

      links = []
      @services.each do |path, middleware|
        links << %{<#{path}>; rel="#{middleware}"}
      end

      [206, {"Link" => links.join("\n")}, []]
    end

    private

    def service(identifier, options = {}, &block)
      if identifier.is_a?(Class)
        middleware = identifier
      else
        begin
          middleware = Helios::Backend.const_get(constantize(identifier))
        rescue NameError
          raise LoadError, "Could not find matching service for #{identifier.inspect} (Helios::Backend::#{constantize(identifier)}). You may need to install an additional gem (such as helios-#{identifier})."
        end
      end

      path = "/#{(options.delete(:root) || DEFAULT_PATHS[identifier] || identifier)}".squeeze("/")

      map path do
        instance_eval(&block) if block_given?
        run middleware.new(self, options)
      end

      @services[path] = middleware
    end

    def constantize(identifier)
      identifier.to_s.split(/([[:alpha:]]*)/).select{|c| /[[:alpha:]]/ === c}.map(&:capitalize).join("")
    end
  end
end

require 'helios/backend/data'
require 'helios/backend/in-app-purchase'
require 'helios/backend/passbook'
require 'helios/backend/push-notification'
require 'helios/backend/newsstand'

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
helios-0.4.1 ./lib/helios/backend.rb
helios-0.4.0 ./lib/helios/backend.rb
helios_aim-0.2.2 ./lib/helios/backend.rb
helios-0.3.0 ./lib/helios/backend.rb
helios-0.2.5 ./lib/helios/backend.rb
helios-0.2.4 ./lib/helios/backend.rb
helios-0.2.3 ./lib/helios/backend.rb
helios-0.2.2 ./lib/helios/backend.rb