Sha256: 32cc80c737d02371ed1d3d7a8ea3208eb982c753a819163e3b5e61f8bd27e843

Contents?: true

Size: 1.31 KB

Versions: 9

Compression:

Stored size: 1.31 KB

Contents

# encoding: utf-8

module Hyperdrive
  class HATEOAS
    extend Hyperdrive::Values

    def self.call(env)
      if hyperdrive.resources.empty? || env['PATH_INFO'] != '/'
        raise Hyperdrive::Errors::NotFound
      end

      endpoints = hyperdrive.resources.map do |_,resource|
        resource.to_hash
      end

      api = {
              _links: { self: { href: '/' } },
              name: hyperdrive.config[:name],
              description: hyperdrive.config[:description],
              vendor: hyperdrive.config[:vendor],
              resources: endpoints
            }

      media_types = %w(hal+json json).map do |media_type|
        "application/vnd.#{hyperdrive.config[:vendor]}+#{media_type}"
      end

      media_types += %w(application/hal+json application/json)
      content_type = env['hyperdrive.accept'].best_of(media_types)
      body = if content_type =~ /json$/
                MultiJson.dump(api)
              else
                raise Errors::NotAcceptable.new(env['HTTP_ACCEPT'])
              end

      status = 200
      headers = {}
      headers['Access-Control-Allow-Origin'] = '*'
      headers['Access-Control-Allow-Methods'] = 'GET, HEAD, OPTIONS'
      headers['Allow'] = 'GET, HEAD, OPTIONS'
      headers['Content-Type'] = content_type
      [status, headers, [body]]
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
hyperdrive-0.0.15 lib/hyperdrive/hateoas.rb
hyperdrive-0.0.14 lib/hyperdrive/hateoas.rb
hyperdrive-0.0.13 lib/hyperdrive/hateoas.rb
hyperdrive-0.0.12 lib/hyperdrive/hateoas.rb
hyperdrive-0.0.11 lib/hyperdrive/hateoas.rb
hyperdrive-0.0.10 lib/hyperdrive/hateoas.rb
hyperdrive-0.0.9 lib/hyperdrive/hateoas.rb
hyperdrive-0.0.8 lib/hyperdrive/hateoas.rb
hyperdrive-0.0.7 lib/hyperdrive/hateoas.rb