Class: MediaTypes::Serialization::Serializers::ApiViewer
- Defined in:
- lib/media_types/serialization/serializers/api_viewer.rb
Class Method Summary collapse
- .allowed_replies(context, actions) ⇒ Object
- .escape_javascript(value) ⇒ Object
- .to_input_identifiers(serializers) ⇒ Object
- .to_output_identifiers(serializers) ⇒ Object
- .viewerify(uri, current_host, type: 'last') ⇒ Object
Methods inherited from Base
Class Method Details
.allowed_replies(context, actions) ⇒ Object
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/media_types/serialization/serializers/api_viewer.rb', line 36 def self.allowed_replies(context, actions) request_path = context.request.original_fullpath.split('?')[0] path_prefix = ENV.fetch('RAILS_RELATIVE_URL_ROOT') { '' } request_path = request_path.sub(path_prefix, '') my_controller = Rails.application.routes.recognize_path request_path possible_replies = ['POST', 'PUT', 'DELETE'] enabled_replies = {} possible_replies.each do |m| begin found_controller = Rails.application.routes.recognize_path request_path, method: m if found_controller[:controller] == my_controller[:controller] enabled_replies[m] = found_controller[:action] end rescue ActionController::RoutingError # not available end end input_definitions = actions[:input] || {} output_definitions = actions[:output] || {} result = {} global_in = input_definitions['all_actions'] || [] global_out = output_definitions['all_actions'] || [] viewer_uri = URI.parse(context.request.original_url) query_parts = viewer_uri.query&.split('&') || [] query_parts = query_parts.select { |q| !q.start_with? 'api_viewer=' } viewer_uri.query = (query_parts + ["api_viewer=last"]).join('&') enabled_replies.each do |method, action| input_serializers = global_in + (input_definitions[action] || []) output_serializers = global_out + (output_definitions[action] || []) result[method] = { input: to_input_identifiers(input_serializers), output: to_output_identifiers(output_serializers), } end result end |
.escape_javascript(value) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/media_types/serialization/serializers/api_viewer.rb', line 80 def self.escape_javascript(value) escape_map = { "\\" => "\\\\", "</" => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'", "`" => "\\`", "$" => "\\$" } escape_map[(+"\342\200\250").force_encoding(Encoding::UTF_8).encode!] = "
" escape_map[(+"\342\200\251").force_encoding(Encoding::UTF_8).encode!] = "
" value ||= "" return value.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"']|[`]|[$])/u, escape_map).html_safe end |
.to_input_identifiers(serializers) ⇒ Object
25 26 27 28 29 |
# File 'lib/media_types/serialization/serializers/api_viewer.rb', line 25 def self.to_input_identifiers(serializers) serializers.flat_map do |s| s[:serializer].inputs_for(views: [s[:view]]).registrations.keys end end |
.to_output_identifiers(serializers) ⇒ Object
30 31 32 33 34 |
# File 'lib/media_types/serialization/serializers/api_viewer.rb', line 30 def self.to_output_identifiers(serializers) serializers.flat_map do |s| s[:serializer].outputs_for(views: [s[:view]]).registrations.keys end end |
.viewerify(uri, current_host, type: 'last') ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/media_types/serialization/serializers/api_viewer.rb', line 13 def self.viewerify(uri, current_host, type: 'last') viewer = URI.parse(uri) return uri unless viewer.host == current_host query_parts = viewer.query&.split('&') || [] query_parts = query_parts.reject { |p| p.starts_with?('api_viewer=') } query_parts.append("api_viewer=#{type}") viewer.query = query_parts.join('&') viewer.to_s end |