tasks/thor/example.rb in praxis-0.15.0 vs tasks/thor/example.rb in praxis-0.16.0

- old
+ new

@@ -2,14 +2,14 @@ module PraxisGen class Example < Thor include Thor::Actions namespace "praxis:example" - + argument :app_name, required: true desc "new", "Generates a new 'hello world' example application under an <app_name> directory" - + def new puts "GENERATION COMMENCED!! (for #{app_name})" # Fix weird symbols in the app name (if they are) @app_name = app_name.downcase.gsub(/[^a-z0-9_\/.]/, '') # Generate a new app @@ -31,16 +31,22 @@ puts " cd #{app_name}" puts " bundle" puts " bundle exec rackup" puts puts " # terminal 2:" - puts " curl -i http://localhost:8888/api/hello -H 'X-Api-Version: 1.0' -X GET # Index" - puts " curl -i http://localhost:8888/api/hello/2 -H 'X-Api-Version: 1.0' -X GET # Show" - puts " curl -i http://localhost:8888/api/hello/2 -H 'X-Api-Version: 2.0' -X GET # NotFound Error" + puts " # Index: list the hello words (especifying api version through the query string) " + puts " curl -i 'http://localhost:8888/api/hello?api_version=1.0' -H 'Authorization: Bearer XYZ' " + puts "" + puts " # Show: list one of the hello words (especifying api version through a header) " + puts " curl -i 'http://localhost:8888/api/hello/1' -H 'X-Api-Version: 1.0' -H 'Authorization: Bearer XYZ'" + puts "" + puts " # NotFound: Hello word will not be found under API 2.0" + puts " curl -i 'http://localhost:8888/api/hello/1' -H 'X-Api-Version: 2.0' -H 'Authorization: Bearer XYZ'" + puts " #Note: The Authorization header is made required in the application to emulate OAuth2 (but not used)" nil end -private +private # Returns relative path for the new application # # @return [String] # # @example @@ -49,12 +55,12 @@ # # def app_dir_pathname @app_dir_pathname ||= Pathname.new(app_name) end - - + + # Returns path string built from the set of the given strings # # @param [String,Array] strings # # @return [String] @@ -63,12 +69,12 @@ # path('a', 'b', 'c') #=> 'my_test_app/a/b/c' # def path(*strings) app_dir_pathname.join(*strings).to_s end - - + + # Creates './config/environment.rb' file # # @return [void] # def generate_config_environment_rb @@ -98,12 +104,12 @@ end RUBY end nil end - - + + # Creates './Gemfile' file # # @return [void] # def generate_gemfile @@ -120,12 +126,12 @@ end RUBY end nil end - - + + # Creates './Rakefile' file # # @return [void] # def generate_rakefile @@ -135,12 +141,12 @@ require 'praxis/tasks' RUBY end nil end - - + + # Creates './config.ru' file # # @return [void] # def generate_config_ru @@ -158,12 +164,12 @@ run application RUBY end nil end - - + + def generate_app_definitions_hello_world create_file path('design/api.rb') do <<-RUBY # Use this file to define your response templates and traits. # @@ -171,83 +177,85 @@ # response_template :custom do |media_type:| # status 200 # media_type media_type # end Praxis::ApiDefinition.define do - trait :versionable do + # Trait that when included will require a Bearer authorization header to be passed in. + trait :authorized do headers do - key "X-Api-Version", String, values: ['1.0'], required: true + key "Authorization", String, regexp: /^.*Bearer\s/, required: true end end end RUBY end - + create_file path('design/resources/hello.rb') do <<-RUBY module V1 module ApiResources class Hello include Praxis::ResourceDefinition media_type V1::MediaTypes::Hello version '1.0' - routing do - prefix '/api/hello' + prefix '/api/hello' + + # Will apply to all actions of this resource + trait :authorized + + action_defaults do + response :ok end action :index do - use :versionable routing do get '' end - response :ok end action :show do - use :versionable routing do get '/:id' end params do attribute :id, Integer, required: true, min: 0 end - response :ok response :not_found end end end end RUBY end - + create_file path('design/media_types/hello.rb') do <<-RUBY module V1 module MediaTypes class Hello < Praxis::MediaType - + identifier 'application/json' - + attributes do attribute :string, String end - + view :default do attribute :string end end end end RUBY end end - - + + def generate_app_controllers_hello_world create_file path('app/controllers/hello.rb') do <<-RUBY module V1 class Hello @@ -266,18 +274,18 @@ def show(id:, **other_params) hello = HELLO_WORLD[id] if hello response.body = { id: id, data: hello } else - self.response = Praxis::Responses::NotFound.new + self.response = Praxis::Responses::NotFound.new(body: "Hello word with index \#{id} not found in our DB") end response.headers['Content-Type'] = 'application/json' response end end end RUBY end end - + end end \ No newline at end of file