Skip to content

Commit

Permalink
parse_request based on content_type
Browse files Browse the repository at this point in the history
  • Loading branch information
NileshPanchal committed Jan 18, 2016
1 parent 08e85ad commit 6d899a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
7 changes: 3 additions & 4 deletions app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class ApiController < ApplicationController
def execute_route

def execute_route
if request.method == "GET" || request.method == "PUT" || request.method == "DELETE"
input_data = request.query_parameters
elsif request.method == "POST"
Expand All @@ -14,7 +13,7 @@ def execute_route
else
route = Route.where(:uri => request.path).where.not(:kind => 'SOAP').first
end

if route.nil?
log = {:route_id => nil, :status_code => '404', :response => nil}
render status: 404, text: "#{request.path} not found."
Expand All @@ -24,7 +23,7 @@ def execute_route
log = {:route_id => route.id, :status_code => '405', :response => nil}
render status: 405, text: "#{request.method} not allowed for #{params[:uri]} route."
else
req_obj = route.parse_request(input_data)
req_obj = route.parse_request(input_data, request.content_type)
if req_obj.instance_of?(Oga::XML::Document) or route.kind == 'PLAIN-TEXT'
headers = {'Accept' => request.env['HTTP_ACCEPT'],
'X-QG-CI-URI' => request.env['HTTP_X_QG_CI_URI'],
Expand Down
38 changes: 22 additions & 16 deletions app/models/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Route < ActiveRecord::Base
validates_presence_of :uri, :kind, :http_method

def self.options_for_kind
[['SOAP','SOAP'],['JSON','JSON'],['PLAIN-TEXT','PLAIN-TEXT']]
[['SOAP','SOAP'],['XML','XML'],['JSON','JSON'],['PLAIN-TEXT','PLAIN-TEXT']]
end

def self.options_for_http_method
Expand All @@ -18,26 +18,32 @@ def self.options_for_http_method



def parse_request(req)
def parse_request(req, content_type)

# parsing of query_params is not yet supported
if req.empty?
return Oga.parse_xml('<todo/>')
end

if self.kind == "SOAP"
begin
document = Oga.parse_xml(req, :strict => true)
return document
rescue Exception => e
return {error: e.message}
end
elsif self.kind == "JSON"
begin
xml_str = Gyoku.xml(Oj.load(req))
document = Oga.parse_xml(xml_str)
return document
rescue Exception => e
return {error: e.message}
parsed_application_type = content_type.split("/").first
parsed_content_type = content_type.split("/").last

if (parsed_application_type == "application")
if (parsed_content_type.include?("soap") == true) || (parsed_content_type.include?("xml") == true)
begin
document = Oga.parse_xml(req, :strict => true)
return document
rescue Exception => e
return {error: e.message}
end
elsif (parsed_content_type.include?("json") == true)
begin
xml_str = Gyoku.xml(Oj.load(req))
document = Oga.parse_xml(xml_str)
return document
rescue Exception => e
return {error: e.message}
end
end
else
return req
Expand Down

0 comments on commit 6d899a3

Please sign in to comment.