Class: Mack::Rendering::Type::Url

Inherits:
Base show all
Defined in:
lib/mack/rendering/type/url.rb

Overview

This class will render the contents of a url.

Examples: This is considered a ‘remote’ request and will be made using GET.

<%= render(:url, "http://www.mackframework.com") %>

This is considered a ‘remote’ request and will be made using POST.

<%= render(:url, "http://www.mackframework.com", :method => :post) %>

This is considered a ‘remote’ request and will be made using GET, and will have query string parameters.

<%= render(:url, "http://www.mackframework.com", :parameters => {:name => "mark"}) %> # http://www.mackframework.com?name=mark

This is considered a ‘remote’ request and will be made using POST, and will have form parameters.

<%= render(:url, "http://www.mackframework.com", :method => :post, :parameters => {:name => "mark"}) %>

‘Local’ requests can also be made: This is considered a ‘local’ request and will be made using GET.

<%= render(:url, "/users") %>

This is considered a ‘local’ request and will be made using POST.

<%= render(:url, "/users", :method => :post) %>

This is considered a ‘local’ request and will be made using GET, and will have query string parameters.

<%= render(:url, "/users", :parameters => {:name => "mark"}) %> # /users?name=mark

This is considered a ‘local’ request and will be made using POST, and will have form parameters.

<%= render(:url, "/users", :method => :post, :parameters => {:name => "mark"}) %>

Instance Attribute Summary

Attributes inherited from Base

#view_template

Instance Method Summary collapse

Methods inherited from Base

#capture, #controller_view_path, #find_engine, #find_file, #initialize, #method_missing

Constructor Details

This class inherits a constructor from Mack::Rendering::Type::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mack::Rendering::Type::Base

Instance Method Details

#allow_layout?Boolean

No layouts should be used with this Mack::Rendering::Type

Returns:

  • (Boolean)


29
30
31
# File 'lib/mack/rendering/type/url.rb', line 29

def allow_layout?
  false
end

#renderObject

Retrieves the contents of the url using either GET or POST, passing along any specified parameters.



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
61
62
# File 'lib/mack/rendering/type/url.rb', line 34

def render
  options = {:method => :get, :raise_exception => false}.merge(self._options)
  url = self._render_value
  remote = url.match(/^[a-zA-Z]+:\/\//)
  case options[:method]
  when :get
    if remote
      do_render_remote_url(url_with_query(url, options[:parameters]), options) do |uri, options|
        Net::HTTP.get_response(uri)
      end
    else
      do_render_local_url(url, options) do |url, options|
        Rack::MockRequest.new(self._app_for_rendering).get(url, options)
      end
    end
  when :post
    if remote
      do_render_remote_url(url, options) do |uri, options|
        Net::HTTP.post_form(uri, options[:parameters] || {})
      end
    else
      do_render_local_url(url, options) do |url, options|
        Rack::MockRequest.new(self._app_for_rendering).post(url, options)
      end
    end
  else
    raise Mack::Errors::UnsupportRenderUrlMethodType.new(options[:method])
  end
end