Class: Railsdav::Renderer::ResponseTypeSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/railsdav/renderer/response_type_selector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, request_format) ⇒ ResponseTypeSelector

Returns a new instance of ResponseTypeSelector.



8
9
10
11
12
13
14
# File 'lib/railsdav/renderer/response_type_selector.rb', line 8

def initialize(controller, request_format)
  @controller, @request_format = controller, request_format
  @subresources = []

  # TODO: somehow allow passing more options to current resource
  @resource_options = {:format => @request_format.to_sym}
end

Instance Attribute Details

#resource_optionsObject (readonly)

Returns the value of attribute resource_options.



6
7
8
# File 'lib/railsdav/renderer/response_type_selector.rb', line 6

def resource_options
  @resource_options
end

#subresourcesObject (readonly)

Returns the value of attribute subresources.



6
7
8
# File 'lib/railsdav/renderer/response_type_selector.rb', line 6

def subresources
  @subresources
end

Instance Method Details

#format(name, options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/railsdav/renderer/response_type_selector.rb', line 16

def format(name, options)
  if Mime::Type.lookup_by_extension(name.to_s)
    if @request_format.to_sym == name
      # TODO: somehow get the attributes (size, updated-at, ...) from the actual Mime responder block here
      @resource_options.merge! options
    end
  else
    raise UnknownMimeTypeExtension, "#{name} is not a valid MIME type file extension."
  end
end

#subresource(*subresources) ⇒ Object

loop over all urls marked as subresources in webdav responder block and add them with all their acceptable mime types



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/railsdav/renderer/response_type_selector.rb', line 29

def subresource(*subresources)
  return unless @request_format == :collection

  options = subresources.extract_options!
  subresources.each do |resource_url|
    relative_resource_url = resource_url.sub %r(^#{ActionController::Base.relative_url_root}), '' # HACK
    route = Rails.application.routes.recognize_path(relative_resource_url)

    if meta = Renderer.(route)
      # show the resource as a collection unless disabled
      if meta[:collection]
        @subresources << Renderer::ResourceDescriptor.new(resource_url, options.merge(:format => :collection))
      elsif meta[:accept].blank?
        # show the resource without .:format suffix, but not as a collection
        @subresources << Renderer::ResourceDescriptor.new(resource_url, options)
      end

      # show the resource with all the specified format suffixes
      if meta[:accept]
        [meta[:accept]].flatten.each do |type_name|
          mime_type = Mime::Type.lookup_by_extension(type_name)
          subresource_url = @controller.url_for(route.merge(:format => type_name))
          @subresources << Renderer::ResourceDescriptor.new(subresource_url, options.merge(:format => mime_type))
        end
      end
    else
      raise MissingWebDAVMetadata, "no WebDAV metadata found for #{resource_url}, please specify it using #enable_webdav_for"
    end
  end

  @resource_options[:size] = @subresources.size
end