Sha256: 7324e99b276504e7a9379f3a4b61a8331d15b057fe1945705af0d443b49de8c3

Contents?: true

Size: 1.74 KB

Versions: 5

Compression:

Stored size: 1.74 KB

Contents

module MobileWorkflow
  module OpenApiSpec
    class Parser
  
      # Schemas to avoid generating models for (static items from MW)
      SKIP_SCHEMAS = ["attachment", "ListItem", "DisplayItem", "DisplayText", "DisplayButton", "DisplayImage", "DisplayVideo"]
  
      def initialize(open_api_spec_string)
        @open_api_spec_string = open_api_spec_string
      end
  
      def model_name_to_properties
        @model_properties = {}
        schemas.each_pair do |model_name, schema|
          next if SKIP_SCHEMAS.include? model_name # Don't generate schemas for MW schemas
    
          model_name = model_name.underscore
          model_properties = schema_model_properties(model_name, schema)
      
          @model_properties[model_name] = model_properties
        end
        @model_properties
      end
  
      def controller_names
        @controller_names ||= paths.collect{|url_path| url_path.split('/')[1] }.uniq
      end
  
      def paths
        @paths ||= open_api_spec[:paths].keys
      end
  
      def schemas
        @schemas ||= open_api_spec[:components][:schemas]
      end
  
      private
      def open_api_spec
        @open_api_spec ||= read_openapi_spec
      end
  
      def schema_model_properties(name, schema)
        schema["properties"].keys.collect{|key| "#{key}:#{model_property_type(schema["properties"][key])}" }.join(" ")  
      end
  
      def model_property_type(property)
        return property["type"] unless property["type"].blank?
        return 'attachment' if property['$ref'] == "#/components/schemas/attachment"
    
        raise 'Unknown property type'
      end
  
      def read_openapi_spec
        @read_openapi_spec ||= JSON.parse(@open_api_spec_string).with_indifferent_access 
      end
  
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mobile_workflow-0.6.4 lib/mobile_workflow/open_api_spec/parser.rb
mobile_workflow-0.6.3 lib/mobile_workflow/open_api_spec/parser.rb
mobile_workflow-0.6.2 lib/mobile_workflow/open_api_spec/parser.rb
mobile_workflow-0.6.1 lib/mobile_workflow/open_api_spec/parser.rb
mobile_workflow-0.6.0 lib/mobile_workflow/open_api_spec/parser.rb