sinatra-ajax_only 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sinatra-ajax_only.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 ymmtmsys
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # Sinatra::AjaxOnly
2
+
3
+ Sinatra extensions for blocking non-AJAX requests.
4
+ Provide **ajax_only** condition. Return 403 when request is not ajax.
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'sinatra-ajax_only'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sinatra-ajax_only
20
+
21
+ ## Usage
22
+
23
+ ### for Classic Style Application
24
+
25
+ ``````Ruby
26
+ # Only allow access via Ajax.
27
+ get "/should/ajax", ajax_only do
28
+ "Ok!"
29
+ end
30
+ ``````
31
+
32
+ ### for Modular Application
33
+
34
+ ``````Ruby
35
+ class MyApp < Sinatra::Base
36
+
37
+ register Sinatra::AjaxOnly
38
+
39
+ # Only allow access via Ajax.
40
+ get "/should/ajax", ajax_only do
41
+ "Hey!"
42
+ end
43
+ end
44
+ ``````
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = 'spec/**/*_spec.rb'
7
+ spec.rspec_opts = ['-cfs']
8
+ end
9
+ rescue LoadError => e
10
+ $stderr.puts "rspec/core is missing"
11
+ end
12
+
13
+ task :default => [:spec]
@@ -0,0 +1 @@
1
+ require "sinatra/ajax_only"
@@ -0,0 +1,17 @@
1
+ require "sinatra/base"
2
+
3
+ module Sinatra
4
+ module AjaxOnly
5
+ def ajax_only
6
+ {:filter_xhr => true}
7
+ end
8
+
9
+ def filter_xhr(_)
10
+ condition do
11
+ halt 403 unless request.xhr?
12
+ end
13
+ end
14
+ end
15
+
16
+ register AjaxOnly
17
+ end
@@ -0,0 +1,5 @@
1
+ module Sinatra
2
+ module AjaxOnly
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sinatra/ajax_only/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "sinatra-ajax_only"
8
+ gem.version = Sinatra::AjaxOnly::VERSION
9
+ gem.authors = ["ymmtmsys"]
10
+ gem.email = ["m2yamamoto@gmail.com"]
11
+ gem.description = %q{Sinatra extensions for blocking non-AJAX requests.}
12
+ gem.summary = gem.description
13
+ gem.homepage = "https://github.com/ymmtmsys/sinatra-ajax_only"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'sinatra', '>= 1.3.0'
21
+ gem.add_development_dependency 'rspec', '>= 0'
22
+ gem.add_development_dependency 'rack-test', '>= 0'
23
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sinatra::AjaxOnly do
4
+
5
+ class TestApp < Sinatra::Base
6
+ register Sinatra::AjaxOnly
7
+
8
+ get '/require/ajax', ajax_only do
9
+ "ok"
10
+ end
11
+
12
+ get '/' do
13
+ "root"
14
+ end
15
+
16
+ end
17
+
18
+ def app
19
+ TestApp
20
+ end
21
+
22
+ describe "not require ajax request" do
23
+ describe 'non-ajax' do
24
+ before { get '/' }
25
+ subject { last_response.status }
26
+ it { should == 200 }
27
+ end
28
+
29
+ describe 'ajax' do
30
+ before do
31
+ get '/', {}, {"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"}
32
+ end
33
+ subject { last_response.status }
34
+ it { should == 200 }
35
+ end
36
+ end
37
+
38
+ describe "require ajax ajax" do
39
+ describe 'non-ajax' do
40
+ before { get '/require/ajax' }
41
+ subject { last_response.status }
42
+ it { should == 403 }
43
+ end
44
+
45
+ describe 'ajax' do
46
+ before do
47
+ get '/require/ajax', {}, {"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"}
48
+ end
49
+ subject { last_response.status }
50
+ it { should == 200 }
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,26 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ lib = File.expand_path('../lib', __FILE__)
9
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
10
+ require 'sinatra-ajax_only'
11
+ require 'rspec'
12
+ require 'rack/test'
13
+
14
+ RSpec.configure do |config|
15
+ config.treat_symbols_as_metadata_keys_with_true_values = true
16
+ config.run_all_when_everything_filtered = true
17
+ config.filter_run :focus
18
+
19
+ config.include Rack::Test::Methods
20
+
21
+ # Run specs in random order to surface order dependencies. If you find an
22
+ # order dependency and want to debug it, you can fix the order by providing
23
+ # the seed, which is printed after each run.
24
+ # --seed 1234
25
+ config.order = 'random'
26
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-ajax_only
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ymmtmsys
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.3.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.3.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rack-test
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Sinatra extensions for blocking non-AJAX requests.
63
+ email:
64
+ - m2yamamoto@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/sinatra-ajax_only.rb
75
+ - lib/sinatra/ajax_only.rb
76
+ - lib/sinatra/ajax_only/version.rb
77
+ - sinatra-ajax_only.gemspec
78
+ - spec/sinatra/ajax_only_spec.rb
79
+ - spec/spec_helper.rb
80
+ homepage: https://github.com/ymmtmsys/sinatra-ajax_only
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.24
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Sinatra extensions for blocking non-AJAX requests.
104
+ test_files:
105
+ - spec/sinatra/ajax_only_spec.rb
106
+ - spec/spec_helper.rb