daredevil 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +18 -0
  4. data/config/locales/daredevil.yml +14 -0
  5. data/lib/daredevil.rb +60 -0
  6. data/lib/daredevil/configuration.rb +22 -0
  7. data/lib/daredevil/engine.rb +6 -0
  8. data/lib/daredevil/errors.rb +41 -0
  9. data/lib/daredevil/responder.rb +126 -0
  10. data/lib/daredevil/responder/actions.rb +92 -0
  11. data/lib/daredevil/responder/responses.rb +30 -0
  12. data/lib/daredevil/responder/sanitizers.rb +18 -0
  13. data/lib/daredevil/utils/rack_helper.rb +13 -0
  14. data/lib/daredevil/version.rb +3 -0
  15. data/test/daredevil_test.rb +15 -0
  16. data/test/dummy/README.rdoc +28 -0
  17. data/test/dummy/Rakefile +6 -0
  18. data/test/dummy/app/controllers/application_controller.rb +4 -0
  19. data/test/dummy/app/controllers/posts_controller.rb +44 -0
  20. data/test/dummy/app/models/application_record.rb +3 -0
  21. data/test/dummy/app/models/post.rb +3 -0
  22. data/test/dummy/app/serializers/kustom_post_serializer.rb +3 -0
  23. data/test/dummy/app/serializers/post_serializer.rb +3 -0
  24. data/test/dummy/app/views/posts/_post.json.jbuilder +1 -0
  25. data/test/dummy/app/views/posts/index.json.jbuilder +5 -0
  26. data/test/dummy/app/views/posts/show.json.jbuilder +4 -0
  27. data/test/dummy/bin/bundle +3 -0
  28. data/test/dummy/bin/rails +4 -0
  29. data/test/dummy/bin/rake +4 -0
  30. data/test/dummy/bin/setup +29 -0
  31. data/test/dummy/config.ru +4 -0
  32. data/test/dummy/config/application.rb +14 -0
  33. data/test/dummy/config/boot.rb +5 -0
  34. data/test/dummy/config/database.yml +25 -0
  35. data/test/dummy/config/environment.rb +5 -0
  36. data/test/dummy/config/environments/development.rb +41 -0
  37. data/test/dummy/config/environments/test.rb +42 -0
  38. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  39. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  40. data/test/dummy/config/initializers/session_store.rb +3 -0
  41. data/test/dummy/config/initializers/to_time_preserves_timezone.rb +10 -0
  42. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  43. data/test/dummy/config/locales/en.yml +23 -0
  44. data/test/dummy/config/routes.rb +3 -0
  45. data/test/dummy/config/secrets.yml +22 -0
  46. data/test/dummy/db/development.sqlite3 +0 -0
  47. data/test/dummy/db/migrate/20170713181802_create_posts.rb +8 -0
  48. data/test/dummy/db/schema.rb +20 -0
  49. data/test/dummy/db/test.sqlite3 +0 -0
  50. data/test/dummy/log/development.log +12878 -0
  51. data/test/dummy/log/test.log +5 -0
  52. data/test/factories/posts.rb +6 -0
  53. data/test/post_jbuilder_test.rb +27 -0
  54. data/test/post_test.rb +77 -0
  55. data/test/reports/TEST-DaredevilTest.xml +9 -0
  56. data/test/reports/TEST-PostJbuilderTest.xml +7 -0
  57. data/test/reports/TEST-PostTest.xml +17 -0
  58. data/test/test_helper.rb +20 -0
  59. metadata +174 -0
@@ -0,0 +1,18 @@
1
+ module Daredevil
2
+ class Responder
3
+ module Sanitizers
4
+ def self.status_symbol(status)
5
+ if status.is_a?(Integer)
6
+ status = Rack::Utils::SYMBOL_TO_STATUS_CODE.invert[status]
7
+ end
8
+
9
+ if status.nil? || !status.is_a?(Symbol) ||
10
+ Rack::Utils::SYMBOL_TO_STATUS_CODE[status].nil?
11
+ raise(Daredevil::Errors::UnknownHTTPStatus, status)
12
+ end
13
+
14
+ status
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,13 @@
1
+ module Utils
2
+ module RackHelper
3
+ private
4
+
5
+ def status_symbols
6
+ Rack::Utils::SYMBOL_TO_STATUS_CODE.keys.map(&:to_s)
7
+ end
8
+
9
+ def status_codes
10
+ Rack::Utils::SYMBOL_TO_STATUS_CODE.invert.keys.map(&:to_s)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Daredevil
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class DaredevilTest < ActionDispatch::IntegrationTest
4
+ test 'Daredevil is actually a thing' do
5
+ assert_kind_of Module, Daredevil
6
+ end
7
+
8
+ test 'configuration defaults to jbuilder' do
9
+ assert_equal Daredevil.config.responder_type, :jbuilder
10
+ end
11
+
12
+ test 'it has a gem version' do
13
+ assert_not_nil Daredevil::VERSION
14
+ end
15
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,4 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery with: :null_session
3
+ include Daredevil
4
+ end
@@ -0,0 +1,44 @@
1
+ class PostsController < ApplicationController
2
+ before_action :set_post, only: %w(show)
3
+
4
+ def index
5
+ @posts = Post.all
6
+ respond_with @posts
7
+ end
8
+
9
+ def show
10
+ respond_with @post
11
+ end
12
+
13
+ def create
14
+ @post = Post.new(post_params)
15
+
16
+ if @post.save
17
+ respond_with(@post, status: 201)
18
+ else
19
+ respond_with(@post, status: 422)
20
+ end
21
+ end
22
+
23
+ def update
24
+ if @post.update(post_params)
25
+ respond_with(@post, status: 200)
26
+ else
27
+ respond_with(@post, status: 422)
28
+ end
29
+ end
30
+
31
+ def destroy
32
+ respond_with(@post, status: 204) if @post.destroy
33
+ end
34
+
35
+ private
36
+
37
+ def set_post
38
+ @post = Post.find(params[:id])
39
+ end
40
+
41
+ def post_params
42
+ params.require(:post).permit(:title, :body)
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end
@@ -0,0 +1,3 @@
1
+ class Post < ApplicationRecord
2
+ validates :title, presence: true
3
+ end
@@ -0,0 +1,3 @@
1
+ class KustomPostSerializer < ActiveModel::Serializer
2
+ attributes :title
3
+ end
@@ -0,0 +1,3 @@
1
+ class PostSerializer < ActiveModel::Serializer
2
+ attributes :id, :title
3
+ end
@@ -0,0 +1 @@
1
+ json.extract!(post, :id, :title)
@@ -0,0 +1,5 @@
1
+ json.array!(
2
+ @posts,
3
+ partial: 'posts/post',
4
+ as: :post
5
+ )
@@ -0,0 +1,4 @@
1
+ json.partial!(
2
+ 'posts/post',
3
+ post: @post
4
+ )
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pathname'
3
+
4
+ # path to your application root.
5
+ APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
6
+
7
+ Dir.chdir APP_ROOT do
8
+ # This script is a starting point to setup your application.
9
+ # Add necessary setup steps to this file:
10
+
11
+ puts "== Installing dependencies =="
12
+ system "gem install bundler --conservative"
13
+ system "bundle check || bundle install"
14
+
15
+ # puts "\n== Copying sample files =="
16
+ # unless File.exist?("config/database.yml")
17
+ # system "cp config/database.yml.sample config/database.yml"
18
+ # end
19
+
20
+ puts "\n== Preparing database =="
21
+ system "bin/rake db:setup"
22
+
23
+ puts "\n== Removing old logs and tempfiles =="
24
+ system "rm -f log/*"
25
+ system "rm -rf tmp/cache"
26
+
27
+ puts "\n== Restarting application server =="
28
+ system "touch tmp/restart.txt"
29
+ end
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,14 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require(*Rails.groups)
6
+ require "daredevil"
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ FactoryGirl.definition_file_paths << Pathname.new("../factories")
11
+ FactoryGirl.definition_file_paths.uniq!
12
+ FactoryGirl.find_definitions
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,25 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ #
4
+ # Ensure the SQLite 3 gem is defined in your Gemfile
5
+ # gem 'sqlite3'
6
+ #
7
+ default: &default
8
+ adapter: sqlite3
9
+ pool: 5
10
+ timeout: 5000
11
+
12
+ development:
13
+ <<: *default
14
+ database: db/development.sqlite3
15
+
16
+ # Warning: The database defined as "test" will be erased and
17
+ # re-generated from your development database when you run "rake".
18
+ # Do not set this db to the same as development or production.
19
+ test:
20
+ <<: *default
21
+ database: db/test.sqlite3
22
+
23
+ production:
24
+ <<: *default
25
+ database: db/production.sqlite3
@@ -0,0 +1,5 @@
1
+ # Load the Rails application.
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the Rails application.
5
+ Rails.application.initialize!
@@ -0,0 +1,41 @@
1
+ Rails.application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the web server when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Do not eager load code on boot.
10
+ config.eager_load = false
11
+
12
+ # Show full error reports and disable caching.
13
+ config.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Don't care if the mailer can't send.
17
+ config.action_mailer.raise_delivery_errors = false
18
+
19
+ # Print deprecation notices to the Rails logger.
20
+ config.active_support.deprecation = :log
21
+
22
+ # Raise an error on page load if there are pending migrations.
23
+ config.active_record.migration_error = :page_load
24
+
25
+ # Debug mode disables concatenation and preprocessing of assets.
26
+ # This option may cause significant delays in view rendering with a large
27
+ # number of complex assets.
28
+ config.assets.debug = true
29
+
30
+ # Asset digests allow you to set far-future HTTP expiration dates on all assets,
31
+ # yet still be able to expire them through the digest params.
32
+ config.assets.digest = true
33
+
34
+ # Adds additional error checking when serving assets at runtime.
35
+ # Checks for improperly declared sprockets dependencies.
36
+ # Raises helpful error messages.
37
+ config.assets.raise_runtime_errors = true
38
+
39
+ # Raises error for missing translations
40
+ # config.action_view.raise_on_missing_translations = true
41
+ end
@@ -0,0 +1,42 @@
1
+ Rails.application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Do not eager load code on boot. This avoids loading your whole application
11
+ # just for the purpose of running a single test. If you are using a tool that
12
+ # preloads Rails for running tests, you may have to set it to true.
13
+ config.eager_load = false
14
+
15
+ # Configure static file server for tests with Cache-Control for performance.
16
+ config.serve_static_files = true
17
+ config.static_cache_control = 'public, max-age=3600'
18
+
19
+ # Show full error reports and disable caching.
20
+ config.consider_all_requests_local = true
21
+ config.action_controller.perform_caching = false
22
+
23
+ # Raise exceptions instead of rendering exception templates.
24
+ config.action_dispatch.show_exceptions = false
25
+
26
+ # Disable request forgery protection in test environment.
27
+ config.action_controller.allow_forgery_protection = false
28
+
29
+ # Tell Action Mailer not to deliver emails to the real world.
30
+ # The :test delivery method accumulates sent emails in the
31
+ # ActionMailer::Base.deliveries array.
32
+ config.action_mailer.delivery_method = :test
33
+
34
+ # Randomize the order test cases are executed.
35
+ config.active_support.test_order = :random
36
+
37
+ # Print deprecation notices to the stderr.
38
+ config.active_support.deprecation = :stderr
39
+
40
+ # Raises error for missing translations
41
+ # config.action_view.raise_on_missing_translations = true
42
+ end
@@ -0,0 +1,3 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Rails.application.config.action_dispatch.cookies_serializer = :json
@@ -0,0 +1,4 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Configure sensitive parameters which will be filtered from the log file.
4
+ Rails.application.config.filter_parameters += [:password]
@@ -0,0 +1,3 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Rails.application.config.session_store :cookie_store, key: '_dummy_session'
@@ -0,0 +1,10 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Preserve the timezone of the receiver when calling to `to_time`.
4
+ # Ruby 2.4 will change the behavior of `to_time` to preserve the timezone
5
+ # when converting to an instance of `Time` instead of the previous behavior
6
+ # of converting to the local system timezone.
7
+ #
8
+ # Rails 5.0 introduced this config option so that apps made with earlier
9
+ # versions of Rails are not affected when upgrading.
10
+ ActiveSupport.to_time_preserves_timezone = true
@@ -0,0 +1,14 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # This file contains settings for ActionController::ParamsWrapper which
4
+ # is enabled by default.
5
+
6
+ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7
+ ActiveSupport.on_load(:action_controller) do
8
+ wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
9
+ end
10
+
11
+ # To enable root element in JSON for ActiveRecord objects.
12
+ # ActiveSupport.on_load(:active_record) do
13
+ # self.include_root_in_json = true
14
+ # end
@@ -0,0 +1,23 @@
1
+ # Files in the config/locales directory are used for internationalization
2
+ # and are automatically loaded by Rails. If you want to use locales other
3
+ # than English, add the necessary files in this directory.
4
+ #
5
+ # To use the locales, use `I18n.t`:
6
+ #
7
+ # I18n.t 'hello'
8
+ #
9
+ # In views, this is aliased to just `t`:
10
+ #
11
+ # <%= t('hello') %>
12
+ #
13
+ # To use a different locale, set it with `I18n.locale`:
14
+ #
15
+ # I18n.locale = :es
16
+ #
17
+ # This would use the information in config/locales/es.yml.
18
+ #
19
+ # To learn more, please read the Rails Internationalization guide
20
+ # available at http://guides.rubyonrails.org/i18n.html.
21
+
22
+ en:
23
+ hello: "Hello world"
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ resources :posts
3
+ end