Module: ActionWebService::Scaffolding::ClassMethods

Defined in:
lib/action_web_service/scaffolding.rb

Overview

Web service invocation scaffolding provides a way to quickly invoke web service methods in a controller. The generated scaffold actions have default views to let you enter the method parameters and view the results.

Example:

class ApiController < ActionController
  web_service_scaffold :invoke
end

This example generates an invoke action in the ApiController that you can navigate to from your browser, select the API method, enter its parameters, and perform the invocation.

If you want to customize the default views, create the following views in “app/views”:

  • action_name/methods.html.erb

  • action_name/parameters.html.erb

  • action_name/result.html.erb

  • action_name/layout.html.erb

Where action_name is the name of the action you gave to ClassMethods#web_service_scaffold.

You can use the default views in RAILS_DIR/lib/action_web_service/templates/scaffolds as a guide.

Instance Method Summary collapse

Instance Method Details

#web_service_scaffold(action_name) ⇒ Object

Generates web service invocation scaffolding for the current controller. The given action name can then be used as the entry point for invoking API methods from a web browser.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/action_web_service/scaffolding.rb', line 40

def web_service_scaffold(action_name)
  add_template_helper(Helpers)
  module_eval "    def \#{action_name}\n      if request.method == :get\n        setup_invocation_assigns\n        render_invocation_scaffold 'methods'\n      end\n    end\n\n    def \#{action_name}_method_params\n      if request.method == :get\n        setup_invocation_assigns\n        render_invocation_scaffold 'parameters'\n      end\n    end\n\n    def \#{action_name}_submit\n      if request.method == :post\n        setup_invocation_assigns\n        protocol_name = params['protocol'] ? params['protocol'].to_sym : :soap\n        case protocol_name\n        when :soap\n          @protocol = Protocol::Soap::SoapProtocol.create(self)\n        when :xmlrpc\n          @protocol = Protocol::XmlRpc::XmlRpcProtocol.create(self)\n        end\n        bm = Benchmark.measure do\n          @protocol.register_api(@scaffold_service.api)\n          post_params = params['method_params'] ? params['method_params'].dup : nil\n          params = []\n          @scaffold_method.expects.each_with_index do |spec, i|\n            params << post_params[i.to_s]\n          end if @scaffold_method.expects\n          params = @scaffold_method.cast_expects(params)\n          method_name = public_method_name(@scaffold_service.name, @scaffold_method.public_name)\n          @method_request_xml = @protocol.encode_request(method_name, params, @scaffold_method.expects)\n          new_request = @protocol.encode_action_pack_request(@scaffold_service.name, @scaffold_method.public_name, @method_request_xml)\n          prepare_request(new_request, @scaffold_service.name, @scaffold_method.public_name)\n          self.request = new_request\n          if @scaffold_container.dispatching_mode != :direct\n            request.parameters['action'] = @scaffold_service.name\n          end\n          dispatch_web_service_request\n          @method_response_xml = response.body\n          method_name, obj = @protocol.decode_response(@method_response_xml)\n          return if handle_invocation_exception(obj)\n          @method_return_value = @scaffold_method.cast_returns(obj)\n        end\n        @method_elapsed = bm.real\n        reset_invocation_response\n        render_invocation_scaffold 'result'\n      end\n    end\n\n    private\n      def setup_invocation_assigns\n        @scaffold_class = self.class\n        @scaffold_action_name = \"\#{action_name}\"\n        @scaffold_container = WebServiceModel::Container.new(self)\n        if params['service'] && params['method']\n          @scaffold_service = @scaffold_container.services.find{ |x| x.name == params['service'] }\n          @scaffold_method = @scaffold_service.api_methods[params['method']]\n        end\n      end\n\n      def render_invocation_scaffold(action)\n        customized_template = \"\\\#{self.class.controller_path}/\#{action_name}/\\\#{action}\"\n        default_template = scaffold_path(action)\n        begin\n          content = @template.render(:file => customized_template)\n        rescue ActionView::MissingTemplate\n          content = @template.render(:file => default_template)\n        end\n        @template.instance_variable_set(\"@content_for_layout\", content)\n        if self.active_layout.nil?\n          render :file => scaffold_path(\"layout\")\n        else\n          render :file => self.active_layout, :use_full_path => true\n        end\n      end\n\n      def scaffold_path(template_name)\n        File.dirname(__FILE__) + \"/templates/scaffolds/\" + template_name + \".html.erb\"\n      end\n\n      def reset_invocation_response\n        erase_render_results\n        response.instance_variable_set :@header, Rack::Utils::HeaderHash.new(::ActionController::Response::DEFAULT_HEADERS.merge(\"cookie\" => []))\n      end\n\n      def public_method_name(service_name, method_name)\n        if web_service_dispatching_mode == :layered && @protocol.is_a?(ActionWebService::Protocol::XmlRpc::XmlRpcProtocol)\n          service_name + '.' + method_name\n        else\n          method_name\n        end\n      end\n\n      def prepare_request(new_request, service_name, method_name)\n        new_request.parameters.update(request.parameters)\n        request.env.each{ |k, v| new_request.env[k] = v unless new_request.env.has_key?(k) }\n        if web_service_dispatching_mode == :layered && @protocol.is_a?(ActionWebService::Protocol::Soap::SoapProtocol)\n          new_request.env['HTTP_SOAPACTION'] = \"/\\\#{controller_name()}/\\\#{service_name}/\\\#{method_name}\"\n        end\n      end\n\n      def handle_invocation_exception(obj)\n        exception = nil\n        if obj.respond_to?(:detail) && obj.detail.respond_to?(:cause) && obj.detail.cause.is_a?(Exception)\n          exception = obj.detail.cause\n        elsif obj.is_a?(XMLRPC::FaultException)\n          exception = obj\n        end\n        return unless exception\n        reset_invocation_response\n        rescue_action(exception)\n        true\n      end\n  end_eval\nend\n", __FILE__, __LINE__ + 1