Skip to content

Commit

Permalink
Merge branch 'release/v0.11.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanRada committed May 23, 2014
2 parents 93fb1e0 + 9397038 commit 376a459
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 64 deletions.
89 changes: 66 additions & 23 deletions app/controllers/washout_builder/washout_builder_controller.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,77 @@
require_relative "../../../lib/washout_builder/document/generator"
class WashoutBuilder::WashoutBuilderController < ActionController::Base
protect_from_forgery

def all
# get a list of unique controller names
controllers = Rails.application.routes.routes.map do |route|
if route.defaults[:action] == "_generate_doc"
{:class => "#{route.defaults[:controller]}_controller".camelize.constantize, :name => route.defaults[:controller] }
end
end.uniq.compact

@services = []
unless controllers.blank?
controllers.map do |hash|
namespace = hash[:class].soap_config.namespace
@services << {
'service_name' => hash[:class].to_s.underscore.gsub("_controller", "").camelize ,
'namespace' => namespace,
'endpoint' => namespace.gsub("/wsdl", "/action"),
'documentation_url' => "#{request.protocol}#{request.host_with_port}/#{hash[:name]}/doc",
}
end
route = params[:name].present? ? controller_is_a_service?(params[:name]) : nil
if route.present?
@document = initialize_service_generator(route)
render :template => "wash_with_html/doc", :layout => false,
:content_type => 'text/html'
else
all_services
end


end


private


def initialize_service_generator(route)
controller_class_name = controller_class(route.defaults[:controller])
WashoutBuilder::Document::Generator.new(
:config => controller_class_name.soap_config,
:service_class => controller_class_name,
:soap_actions => controller_class_name.soap_actions
)
end


def all_services
@map_controllers = map_controllers
@services = @map_controllers.blank? ? [] : @map_controllers.map do |controller_name|
{
'service_name' => controller_name.camelize ,
'namespace' => service_namespace(controller_name),
'endpoint' => service_endpoint(controller_name),
'documentation_url' => service_documentation_url(controller_name),
}
end

render :template => "wash_with_html/all_services", :layout => false,
:content_type => 'text/html'

end

def all_controllers
Rails.application.routes.routes
end

def map_controllers
all_controllers.map do |route|
route.defaults[:controller] if route.defaults[:action] == "_generate_doc"
end.uniq.compact
end

def controller_is_a_service?(controller)
route = all_controllers.detect do |route|
route.defaults[:controller].try(:camelize) == controller.camelize && route.defaults[:action] == "_generate_doc"
end
end



def controller_class(controller)
"#{controller}_controller".camelize.constantize
end

def service_namespace(controller_name)
controller_class(controller_name).soap_config.namespace
end

def service_endpoint(controller_name)
service_namespace(controller_name).gsub("/wsdl", "/action")
end

def service_documentation_url(controller_name)
"#{request.protocol}#{request.host_with_port}/#{controller_name}/doc"
end

end
3 changes: 1 addition & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
WashoutBuilder::Engine.routes.draw do
root :to =>"washout_builder#all"


match '*name' => "washout_builder#all", :as => :washout_builder_service, :via => :get
end
44 changes: 32 additions & 12 deletions lib/washout_builder/document/complex_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ module ComplexType


def get_complex_class_name(defined = [])
complex_class = struct? ? basic_type : nil
complex_class = complex_class.include?(".") ? complex_class.gsub(".","/").camelize : complex_class.classify unless complex_class.nil?

complex_class = struct? ? basic_type.gsub(".","/").camelize : nil

unless complex_class.nil? || defined.blank?

complex_obj_found = defined.detect {|hash| hash[:class] == complex_class}
Expand All @@ -17,9 +16,15 @@ def get_complex_class_name(defined = [])
end
end

return complex_class
complex_class
end


def complex_type_ancestors(config, complex_class, defined)
classified? ? get_class_ancestors(config, complex_class, defined) : nil
end


def get_param_structure
map.inject({}) {|h,element| h[element.name] = element.type;h }
end
Expand Down Expand Up @@ -66,31 +71,46 @@ def get_ancestors(class_name)
end


def complex_type_descendants(config,defined)
if struct?
c_names = []
map.each { |obj| c_names.concat(obj.get_nested_complex_types(config, defined)) }
defined.concat(c_names)
end
defined
end


def get_nested_complex_types(config,defined)
defined = [] if defined.blank?
complex_class = get_complex_class_name( defined)
fix_descendant_wash_out_type( config, complex_class)
defined << {:class =>complex_class, :obj => self, :ancestors => classified? ? get_class_ancestors(config, complex_class, defined) : nil } unless complex_class.nil?
if struct?
c_names = []
map.each { |obj| c_names.concat(obj.get_nested_complex_types(config, defined)) }
defined.concat(c_names)
unless complex_class.nil?
defined << complex_type_hash(complex_class, self, complex_type_ancestors(config, complex_class, defined))
end
defined = complex_type_descendants(config, defined)
defined.sort_by { |hash| hash[:class].to_s.downcase }.uniq unless defined.blank?
end


def ancestor_structure(ancestors)
{ ancestors[0].to_s.downcase => ancestors[0].wash_out_param_map }
end

def complex_type_hash(class_name, object, ancestors)
{:class =>class_name, :obj =>object , :ancestors => ancestors }
end


def get_class_ancestors( config, class_name, defined)
bool_the_same = false
ancestors = get_ancestors(class_name)
unless ancestors.blank?
ancestor_structure = { ancestors[0].to_s.downcase => ancestors[0].wash_out_param_map }
ancestor_object = WashOut::Param.parse_def(config,ancestor_structure)[0]
ancestor_object = WashOut::Param.parse_def(config,ancestor_structure(ancestors))[0]
bool_the_same = same_structure_as_ancestor?( ancestor_object)
unless bool_the_same
top_ancestors = get_class_ancestors(config, ancestors[0], defined)
defined << {:class =>ancestors[0], :obj =>ancestor_object , :ancestors => top_ancestors }
defined << complex_type_hash(ancestors[0],ancestor_object , top_ancestors)
end
ancestors unless bool_the_same
end
Expand Down
51 changes: 31 additions & 20 deletions lib/washout_builder/document/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,30 @@ def operations
soap_actions.map { |operation, formats| operation }
end

def sort_fault_types(types)
types.sort_by { |hash| hash[:fault].to_s.downcase }.uniq {|hash| hash[:fault] } unless types.blank?
end

def input_types

def argument_types(type)
format_type = (type == "input") ? "builder_in" : ":builder_out"
types = []
unless soap_actions.blank?
soap_actions.each do |operation, formats|
(formats[:builder_in]).each do |p|
(formats[format_type.to_sym]).each do |p|
types << p
end
end
end
types
end

def input_types
argument_types("input")
end

def output_types
types = []
unless soap_actions.blank?
soap_actions.each do |operation, formats|
(formats[:builder_out]).each do |p|
types << p
end
end
end
types
argument_types("output")
end

def get_soap_action_names
Expand All @@ -73,20 +74,30 @@ def complex_types
end


def fault_types
defined = soap_actions.select{|operation, formats| !formats[:raises].blank? }
defined = defined.collect {|operation, formats| formats[:raises].is_a?(Array) ? formats[:raises] : [formats[:raises]] }.flatten.select { |x| (x.is_a?(Class) && x.ancestors.detect{ |fault| WashoutBuilder::Type.get_fault_classes.include?(fault) }.present?) || (x.is_a?(Class) && WashoutBuilder::Type.get_fault_classes.include?(x)) } unless defined.blank?
fault_types = []
if defined.blank?
defined = [WashoutBuilder::Type.get_fault_classes.first]
def actions_with_exceptions
soap_actions.select{|operation, formats| !formats[:raises].blank? }
end

def exceptions_raised
actions = actions_with_exceptions
faults= actions.collect {|operation, formats| formats[:raises].is_a?(Array) ? formats[:raises] : [formats[:raises]] }.flatten.select { |x| (x.is_a?(Class) && x.ancestors.detect{ |fault| WashoutBuilder::Type.get_fault_classes.include?(fault) }.present?) || (x.is_a?(Class) && WashoutBuilder::Type.get_fault_classes.include?(x)) } unless actions.blank?
if faults.blank?
faults = [WashoutBuilder::Type.get_fault_classes.first]
else
defined << WashoutBuilder::Type.get_fault_classes.first
faults << WashoutBuilder::Type.get_fault_classes.first
end
faults
end


def fault_types
defined = exceptions_raised
fault_types = []
defined.each{ |exception_class| exception_class.get_fault_class_ancestors( fault_types, true)} unless defined.blank?
complex_types = extract_nested_complex_types_from_exceptions(fault_types)
complex_types.delete_if{ |hash| fault_types << hash if (hash[:fault].is_a?(Class) && hash[:fault].ancestors.detect{ |fault| WashoutBuilder::Type.get_fault_classes.include?(fault) }.present?) || (hash[:fault].is_a?(Class) && WashoutBuilder::Type.get_fault_classes.include?(hash[:fault])) } unless complex_types.blank?
fault_types = fault_types.sort_by { |hash| hash[:fault].to_s.downcase }.uniq {|hash| hash[:fault] } unless fault_types.blank?
complex_types = complex_types.sort_by { |hash| hash[:fault].to_s.downcase }.uniq {|hash| hash[:fault] } unless complex_types.blank?
fault_types = sort_fault_types(fault_types)
complex_types = sort_fault_types(complex_types)
[fault_types, complex_types]
end

Expand Down
29 changes: 23 additions & 6 deletions lib/washout_builder/document/virtus_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,47 @@ module VirtusModel

def get_fault_class_ancestors( defined, debug = false)
bool_the_same = false
ancestors = (self.ancestors - self.included_modules).delete_if{ |x| x.to_s.downcase == self.to_s.downcase || x.to_s == "ActiveRecord::Base" || x.to_s == "Object" || x.to_s =="BasicObject" || x.to_s == "Exception" }
ancestors = fault_ancestors
if ancestors.blank?
defined << {:fault => self,:structure =>get_virtus_model_structure ,:ancestors => [] }
defined << fault_ancestor_hash(get_virtus_model_structure, [])
else
fault_structure = remove_fault_type_inheritable_elements( ancestors[0].get_virtus_model_structure.keys)
defined << {:fault => self,:structure =>fault_structure ,:ancestors => ancestors }
defined << fault_ancestor_hash(fault_without_inheritable_elements(ancestors), ancestors)
ancestors[0].get_fault_class_ancestors( defined)
end
ancestors unless bool_the_same
end


def fault_without_inheritable_elements(ancestors)
remove_fault_type_inheritable_elements( ancestors[0].get_virtus_model_structure.keys)
end

def fault_ancestors
(self.ancestors - self.included_modules).delete_if{ |x| x.to_s.downcase == self.to_s.downcase || x.to_s == "ActiveRecord::Base" || x.to_s == "Object" || x.to_s =="BasicObject" || x.to_s == "Exception" }
end

def fault_ancestor_hash( structure, ancestors)
{:fault => self,:structure =>structure ,:ancestors => ancestors }
end

def remove_fault_type_inheritable_elements( keys)
get_virtus_model_structure.delete_if{|key,value| keys.include?(key) }
end


def attr_details_array?(attr_details)
attr_details[:primitive].to_s.downcase == "array"
end

def attr_details_basic_type?(attr_details, field)
WashoutBuilder::Type::BASIC_TYPES.include?(attr_details[field.to_sym].to_s.downcase)
end

def get_virtus_member_type_primitive(attr_details)
complex_class = nil
if attr_details[:primitive].to_s.downcase == "array" && !WashoutBuilder::Type::BASIC_TYPES.include?(attr_details[:member_type].to_s.downcase)
if attr_details_array?(attr_details) && !attr_details_basic_type?(attr_details, "member_type")
complex_class = attr_details[:member_type]
elsif attr_details[:primitive].to_s.downcase != "array" && !WashoutBuilder::Type::BASIC_TYPES.include?(attr_details[:primitive].to_s.downcase)
elsif !attr_details_array?(attr_details) && !attr_details_basic_type?(attr_details, "primitive")
complex_class = attr_details[:primitive]
end
complex_class
Expand Down
2 changes: 1 addition & 1 deletion lib/washout_builder/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module WashoutBuilder
VERSION = "0.11.5"
VERSION = "0.11.6"
end

0 comments on commit 376a459

Please sign in to comment.