Class: Mack::Rendering::Type::Url
- 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
Instance Method Summary collapse
-
#allow_layout? ⇒ Boolean
No layouts should be used with this Mack::Rendering::Type.
-
#render ⇒ Object
Retrieves the contents of the url using either GET or POST, passing along any specified parameters.
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
29 30 31 |
# File 'lib/mack/rendering/type/url.rb', line 29 def allow_layout? false end |
#render ⇒ Object
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 = {:method => :get, :raise_exception => false}.merge(self.) url = self._render_value remote = url.match(/^[a-zA-Z]+:\/\//) case [:method] when :get if remote do_render_remote_url(url_with_query(url, [:parameters]), ) do |uri, | Net::HTTP.get_response(uri) end else do_render_local_url(url, ) do |url, | Rack::MockRequest.new(self._app_for_rendering).get(url, ) end end when :post if remote do_render_remote_url(url, ) do |uri, | Net::HTTP.post_form(uri, [:parameters] || {}) end else do_render_local_url(url, ) do |url, | Rack::MockRequest.new(self._app_for_rendering).post(url, ) end end else raise Mack::Errors::UnsupportRenderUrlMethodType.new([:method]) end end |