spine-actions 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b5a593ef11624c695a463658e22a0fb0504190ee
4
+ data.tar.gz: bda9eae42751da87a9474b12399d2d3e155d9228
5
+ SHA512:
6
+ metadata.gz: cf07c4502b41fe436f9136519bcce0b218c53e937222dec6f8b10344e4a53cedf1c49484742998f922277eb889718edeee23e10db2e94d37d95030b5ca5f1fcc
7
+ data.tar.gz: 4f66b317abd7bfdbcb3350a172374dee7297152e789b1940dac11be72a4bb12d109fd81982fb3d797bceb938fcf69ad149aa026b207937ac6bcf01c3594d8472
data/.gitignore ADDED
@@ -0,0 +1,36 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /bin/
25
+ /.bundle/
26
+ /vendor/bundle
27
+ /lib/bundler/man/
28
+
29
+ # for a library or gem, you might want to ignore these files since the code is
30
+ # intended to run in multiple environments; otherwise, check them in:
31
+ # Gemfile.lock
32
+ # .ruby-version
33
+ # .ruby-gemset
34
+
35
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
+ .rvmrc
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ Changelog
2
+ =========
3
+
4
+ 0.1.0
5
+ -----
6
+ - First public release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in spine-actions.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ spine-actions (0.1.0)
5
+ rack (~> 1.6)
6
+ spine-content_types (~> 0.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ multi_json (1.11.0)
12
+ rack (1.6.1)
13
+ rake (10.4.2)
14
+ spine-content_types (0.1.0)
15
+ multi_json (~> 1.0)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ bundler (~> 1.7)
22
+ rake (~> 10.0)
23
+ spine-actions!
data/LICENSE ADDED
@@ -0,0 +1,12 @@
1
+ Copyright (c) 2015, TOGGL LLC
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+
8
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
+
10
+ 3. Neither the name of the TOGGL LLC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # Spine::Actions
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/spine-actions.svg)](http://badge.fury.io/rb/spine-actions)
4
+ [![Dependency Status](https://gemnasium.com/rspine/actions.svg)](https://gemnasium.com/rspine/actions)
5
+ [![Code Climate](https://codeclimate.com/github/rspine/actions/badges/gpa.svg)](https://codeclimate.com/github/rspine/actions)
6
+
7
+ HTTP endpoint action with before and after callbacks, response builder.
8
+
9
+ ## Installation
10
+
11
+ To install it, add the gem to your Gemfile:
12
+
13
+ ```ruby
14
+ gem 'spine-actions'
15
+ ```
16
+
17
+ Then run `bundle`. If you're not using Bundler, just `gem install spine-actions`.
18
+
19
+ ## Usage
20
+
21
+ Each action can access Rack `env`, `request` and `response`.
22
+
23
+ Action provides following methods to respond: `respond(content, options)`,
24
+ `redirect(url, options)` and `send_data(binary, options)`. Options include
25
+ `:status`, `:content_type`, `:data` and `:filename`.
26
+
27
+ Response headers can be set using `response['header-name'] = 'value'`
28
+ before running response.
29
+
30
+ ```ruby
31
+ class Status < Spine::Actions::Action
32
+ before :authenticate
33
+ after :close_connections
34
+
35
+ # It will be used as response type. Default is Spine::ContentTypes::Html
36
+ def format
37
+ Spine::ContentTypes::Json
38
+ end
39
+
40
+ def action
41
+ # ...
42
+ respond({ status: 'OK' }, status: 200)
43
+ end
44
+
45
+ def authenticate
46
+ # ...
47
+ end
48
+
49
+ def close_connections
50
+ # ...
51
+ end
52
+ end
53
+
54
+
55
+ Status.call(env)
56
+ ```
57
+
58
+ ### Using with default Rack routing
59
+
60
+ ```ruby
61
+ # config.ru
62
+
63
+ map 'status' do
64
+ run Status
65
+ end
66
+ ```
67
+
68
+ ### Using with Spine::Routing
69
+
70
+ ```ruby
71
+ # config.ru
72
+
73
+ router = Spine::Routing::Router.new
74
+ router.configure do
75
+ get :status, to: Status
76
+ end
77
+
78
+ run router
79
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,31 @@
1
+ module Spine
2
+ module Actions
3
+ class Action
4
+ extend Filters
5
+ include Responder
6
+ include Formats
7
+ include Parameters
8
+
9
+ attr_reader :env, :request, :response
10
+
11
+ def initialize(env)
12
+ @env = env
13
+ @request = Rack::Request.new(env)
14
+ @response = Rack::Response.new
15
+ end
16
+
17
+ def self.call(env)
18
+ handler = new(env)
19
+
20
+ process_filters(handler, before_filters_with_parents)
21
+ return handler.response.finish if handler.responded?
22
+
23
+ handler.action
24
+ process_filters(handler, after_filters_with_parents)
25
+
26
+ raise StandardError.new('No response defined') unless handler.responded?
27
+ handler.response.finish
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,44 @@
1
+ module Spine
2
+ module Actions
3
+ module Filters
4
+ def before(method_name)
5
+ before_filters << method_name
6
+ end
7
+
8
+ def after(method_name)
9
+ after_filters << method_name
10
+ end
11
+
12
+ def process_filters(handler, filters)
13
+ filters.detect do |name|
14
+ handler.public_send(name) if handler.respond_to?(name)
15
+ handler.responded?
16
+ end
17
+ end
18
+
19
+ def before_filters
20
+ @before_filters ||= []
21
+ end
22
+
23
+ def before_filters_with_parents
24
+ if superclass.respond_to?(:before_filters_with_parents)
25
+ superclass.before_filters_with_parents + before_filters
26
+ else
27
+ before_filters
28
+ end
29
+ end
30
+
31
+ def after_filters
32
+ @after_filters ||= []
33
+ end
34
+
35
+ def after_filters_with_parents
36
+ if superclass.respond_to?(:after_filters_with_parents)
37
+ after_filters + superclass.after_filters_with_parents
38
+ else
39
+ after_filters
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+ require 'spine/content_types'
2
+
3
+ module Spine
4
+ module Actions
5
+ module Formats
6
+ def format
7
+ ContentTypes::Html
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Spine
2
+ module Actions
3
+ module Parameters
4
+ def params
5
+ request.params
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,56 @@
1
+ require 'rack'
2
+ require 'uri'
3
+
4
+ module Spine
5
+ module Actions
6
+ module Responder
7
+ def responded?
8
+ @responded
9
+ end
10
+
11
+ def respond(content, options = {})
12
+ response[Rack::CONTENT_TYPE] = options.fetch(:content_type) {
13
+ format.mime_type
14
+ }
15
+ response.status = options[:status] if options[:status]
16
+ response.write(format.dump(content))
17
+
18
+ finish_response
19
+ end
20
+
21
+ def send_data(binary, options = {})
22
+ response['Content-Transfer-Encoding'] = 'binary'
23
+ response[Rack::CONTENT_TYPE] = options.fetch(:content_type, 'application/octet-stream')
24
+
25
+ if options[:filename]
26
+ response['Content-Disposition'] = "inline; filename='#{ options[:filename] }'"
27
+ end
28
+ response.write(binary)
29
+
30
+ finish_response
31
+ end
32
+
33
+ def redirect(url, options = {})
34
+ response[Rack::CONTENT_TYPE] = 'application/x-www-form-urlencoded'
35
+ response.redirect(
36
+ build_url(url, options.fetch(:data, {})),
37
+ options.fetch(:status, 302)
38
+ )
39
+
40
+ finish_response
41
+ end
42
+
43
+ def finish_response
44
+ @responded = true
45
+ response
46
+ end
47
+
48
+ def build_url(url, data)
49
+ uri = URI(url)
50
+ query = URI.decode_www_form(uri.query || '') || []
51
+ uri.query = URI.encode_www_form(query + data.to_a)
52
+ uri.to_s
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,5 @@
1
+ module Spine
2
+ module Actions
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module Spine
2
+ module Actions
3
+ autoload :Action, 'spine/actions/action'
4
+ autoload :Parameters, 'spine/actions/parameters'
5
+ autoload :Filters, 'spine/actions/filters'
6
+ autoload :Formats, 'spine/actions/formats'
7
+ autoload :Responder, 'spine/actions/responder'
8
+ end
9
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'spine/actions/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "spine-actions"
8
+ spec.version = Spine::Actions::VERSION
9
+ spec.authors = ["TOGGL LLC"]
10
+ spec.email = ["support@toggl.com"]
11
+ spec.summary = 'Web application request handlers.'
12
+ spec.description = ''
13
+ spec.homepage = 'https://github.com/rspine/actions'
14
+ spec.license = 'BSD-3-Clause'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'rack', "~> 1.6"
22
+ spec.add_dependency 'spine-content_types', "~> 0.1"
23
+
24
+ spec.add_development_dependency 'bundler', "~> 1.7"
25
+ spec.add_development_dependency 'rake', "~> 10.0"
26
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spine-actions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - TOGGL LLC
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: spine-content_types
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: ''
70
+ email:
71
+ - support@toggl.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - CHANGELOG.md
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - lib/spine/actions.rb
84
+ - lib/spine/actions/action.rb
85
+ - lib/spine/actions/filters.rb
86
+ - lib/spine/actions/formats.rb
87
+ - lib/spine/actions/parameters.rb
88
+ - lib/spine/actions/responder.rb
89
+ - lib/spine/actions/version.rb
90
+ - spine-actions.gemspec
91
+ homepage: https://github.com/rspine/actions
92
+ licenses:
93
+ - BSD-3-Clause
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.5
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Web application request handlers.
115
+ test_files: []