roy 0.3.4 → 0.4.0

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.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- roy (0.3.4)
4
+ roy (0.4.0)
5
5
  rack
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -30,7 +30,7 @@ class MessageQueue
30
30
  end
31
31
 
32
32
  def post(_)
33
- halt 403 unless roy.params[:item]
33
+ roy.halt 403 unless roy.params[:item]
34
34
  @stack << roy.params[:item].strip
35
35
  get
36
36
  end
@@ -62,12 +62,12 @@ end
62
62
  ### Environment
63
63
 
64
64
  Inside your handler methods, you have access to a `roy` readable attribute which
65
- is a Struct containing the following fields:
65
+ is an OpenStruct containing at least the following fields:
66
66
 
67
67
  * `env`: the Rack environment
68
68
  * `response`: a `Rack::Response` object that will be returned by `call`
69
69
  * `request`: a `Rack::Request` build from the environment
70
- * `header`: a hash of headers that is part of `response`
70
+ * `headers`: a hash of headers that is part of `response`
71
71
  * `params`: parameters extracted from the query string and the request body
72
72
  * `conf`: the configuration set via `::roy`
73
73
 
@@ -79,7 +79,7 @@ Your handler methods are run inside a `catch` block which will catch the `:halt`
79
79
  symbol. You can then use `throw` to abort a method but you must return an array
80
80
  composed of a status code and a message.
81
81
 
82
- Roy provides a `halt` method that takes a status code and an optional message.
82
+ Roy provides a `roy.halt` method that takes a status code and an optional message.
83
83
  If there is no message it uses the default message from
84
84
  `Rack::Utils::HTTP_STATUS_CODES`
85
85
 
data/lib/roy.rb CHANGED
@@ -1,71 +1 @@
1
- require 'set'
2
- require 'rack'
3
- require 'ostruct'
4
- require 'roy/version'
5
- require 'roy/plugins'
6
-
7
- module Roy
8
- Env = Struct.new(:env, :request, :response, :headers, :params, :conf)
9
- Defaults = OpenStruct.new(allow: Set.new, prefix: :'')
10
-
11
- def self.included(base)
12
- base.send(:extend, ClassMethods)
13
- end
14
-
15
- def roy
16
- @roy ||= Env.new.tap {|e|
17
- e.conf = self.class.conf
18
- }
19
- end
20
-
21
- def call(env)
22
- roy.tap { |e|
23
- e.env = env
24
- e.request = Rack::Request.new(env)
25
- e.response = Rack::Response.new
26
- e.headers = e.response.header
27
- e.params = e.request.GET.merge(e.request.POST)
28
- e.params.default_proc = proc do |hash, key|
29
- hash[key.to_s] if Symbol === key
30
- end
31
- }
32
-
33
- method = roy.env['REQUEST_METHOD'].downcase.to_sym
34
- roy.env['PATH_INFO'].sub!(/^([^\/])/, '/\1')
35
-
36
- method, was_head = :get, true if method == :head
37
-
38
- roy.response.status, body = catch(:halt) do
39
- halt(405) unless roy.conf.allow.include?(method)
40
- prefixed_method = :"#{roy.conf.prefix}#{method}"
41
- [roy.response.status, send(prefixed_method, roy.env['PATH_INFO'])]
42
- end
43
-
44
- roy.response.write(body) unless was_head
45
- roy.response.finish
46
- end
47
-
48
- def halt(code, message=nil)
49
- throw :halt, [code, message || Rack::Utils::HTTP_STATUS_CODES[code]]
50
- end
51
-
52
- module ClassMethods
53
- attr_reader :conf
54
-
55
- def self.extended(base)
56
- base.instance_eval { @conf ||= Defaults.dup }
57
- end
58
-
59
- def roy(options={})
60
- options.each do |k,v|
61
- case k
62
- when :allow
63
- conf.allow.merge(v)
64
- conf.allow.add(:head) if v.member?(:get)
65
- else
66
- conf.send(:"#{k}=", v)
67
- end
68
- end
69
- end
70
- end
71
- end
1
+ require 'roy/base'
data/lib/roy/after.rb ADDED
@@ -0,0 +1,10 @@
1
+ module Roy
2
+ module After
3
+ def call(env)
4
+ status, header, body = super
5
+ resp = Rack::Response.new(body, status, header)
6
+ (roy.conf.after || lambda {|x| x }).(resp)
7
+ resp.finish
8
+ end
9
+ end
10
+ end
data/lib/roy/base.rb ADDED
@@ -0,0 +1,84 @@
1
+ # external dependencies
2
+ require 'rack'
3
+
4
+ # stdlib dependencies
5
+ require 'set'
6
+ require 'ostruct'
7
+
8
+ # local dependencies
9
+ require 'roy/version'
10
+
11
+ module Roy
12
+ Defaults = OpenStruct.new(allow: Set.new, prefix: :'')
13
+
14
+ def self.included(base)
15
+ base.send(:extend, ClassMethods)
16
+ end
17
+
18
+ def roy
19
+ @roy ||= OpenStruct.new.tap {|r|
20
+ r.app = self
21
+ r.conf = self.class.conf
22
+
23
+ def r.halt(code, message=nil)
24
+ throw :halt, [code, message || Rack::Utils::HTTP_STATUS_CODES[code]]
25
+ end
26
+ }
27
+ end
28
+
29
+ def call(env)
30
+ roy.tap { |r|
31
+ r.env = env
32
+ r.request = Rack::Request.new(env)
33
+ r.response = Rack::Response.new
34
+ r.headers = r.response.header
35
+ r.params = r.request.GET.merge(r.request.POST)
36
+ r.params.default_proc = proc do |hash, key|
37
+ hash[key.to_s] if Symbol === key
38
+ end
39
+ }
40
+
41
+ method = roy.env['REQUEST_METHOD'].downcase.to_sym
42
+ roy.env['PATH_INFO'].sub!(/^([^\/])/, '/\1')
43
+
44
+ method, was_head = :get, true if method == :head
45
+
46
+ roy.response.status, body = catch(:halt) do
47
+ roy.halt(405) unless roy.conf.allow.include?(method)
48
+ prefixed_method = :"#{roy.conf.prefix}#{method}"
49
+ [roy.response.status, send(prefixed_method, roy.env['PATH_INFO'])]
50
+ end
51
+
52
+ roy.response.write(body) unless was_head
53
+ roy.response.finish
54
+ end
55
+
56
+ module ClassMethods
57
+ attr_reader :conf
58
+
59
+ def self.extended(base)
60
+ base.instance_eval { @conf ||= Defaults.dup }
61
+ end
62
+
63
+ def roy(options={})
64
+ options.each do |key,value|
65
+ case key
66
+ when :allow
67
+ conf.allow.merge(value)
68
+ conf.allow.add(:head) if value.member?(:get)
69
+ when :use
70
+ value.each do |name|
71
+ if name.is_a?(Symbol)
72
+ require "roy/#{name}"
73
+ const = "#{name}".capitalize.gsub(/_(\w)/) {|m| m[1].upcase }.to_sym
74
+ name = Roy.const_get(const)
75
+ end
76
+ include name
77
+ end
78
+ else
79
+ conf.send(:"#{key}=", value)
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,20 @@
1
+ module Roy
2
+ module BasicAuth
3
+ def protected!(data=nil)
4
+ unless authorized?(data)
5
+ realm = roy.conf.auth && roy.conf.auth[:realm] || 'Realm'
6
+ roy.response['WWW-Authenticate'] = %(Basic realm="#{realm}")
7
+ roy.halt 401
8
+ end
9
+ end
10
+
11
+ def authorized?(data=nil)
12
+ auth = Rack::Auth::Basic::Request.new(roy.request.env)
13
+
14
+ auth.provided? && auth.basic? && auth.credentials &&
15
+ (roy.conf.auth[:logic] || ->(data, u, p) {
16
+ %w(admin password) == [u, p]
17
+ }).(data, *auth.credentials)
18
+ end
19
+ end
20
+ end
data/lib/roy/before.rb ADDED
@@ -0,0 +1,8 @@
1
+ module Roy
2
+ module Before
3
+ def call(env)
4
+ (roy.conf.before || lambda {|x| x }).(env)
5
+ super
6
+ end
7
+ end
8
+ end
data/lib/roy/render.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'tilt'
2
+
3
+ module Roy
4
+ module Render
5
+
6
+ def initialize
7
+ super
8
+ class << roy
9
+ def render(engine, view_or_string, params={}, &block)
10
+ options = conf.render || {}
11
+ template = case view_or_string
12
+ when Symbol
13
+ file = [view_or_string.to_s, engine].map(&:to_s).join('.')
14
+ dir = conf.views || 'views'
15
+ Tilt.new(File.join(dir, file), nil, options)
16
+ else
17
+ Tilt[engine].new(nil, nil, options) { view_or_string.to_s }
18
+ end
19
+
20
+ template.render(app, params, &block)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
data/lib/roy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Roy
2
- VERSION = "0.3.4"
2
+ VERSION = "0.4.0"
3
3
  end
data/test/base_test.rb CHANGED
@@ -16,7 +16,7 @@ class BaseTestObject
16
16
  end
17
17
 
18
18
  def put(path)
19
- Roy.halt 400 unless roy.params[:body]
19
+ roy.halt 400 unless roy.params[:body]
20
20
  history << roy.params[:body]
21
21
  history << roy.params[:foo] if roy.params[:foo]
22
22
  get(path)
@@ -74,7 +74,7 @@ class BaseTest < MiniTest::Unit::TestCase
74
74
 
75
75
  def test_roy_halt
76
76
  assert_throws :halt do
77
- app.halt 200
77
+ app.roy.halt 200
78
78
  end
79
79
  end
80
80
 
@@ -1,4 +1,3 @@
1
-
2
1
  require_relative 'helper'
3
2
 
4
3
  class CustomOptionsTestObject
@@ -1,11 +1,9 @@
1
1
  require_relative '../helper'
2
- require 'roy/plugin/after'
3
2
 
4
3
  class AfterTestObject
5
4
  include Roy
6
- include Roy::Plugin::After
7
5
 
8
- roy allow: [:get],
6
+ roy use: [:after], allow: [:get],
9
7
  after: lambda { |response|
10
8
  response.header['Content-Type'] = 'text/plain'
11
9
  }
@@ -1,11 +1,9 @@
1
1
  require_relative '../helper'
2
- require 'roy/plugin/basic_auth'
3
2
 
4
3
  class BasicAuthTestObject
5
4
  include Roy
6
- include Roy::Plugin::BasicAuth
7
5
 
8
- roy allow: [:get],
6
+ roy use: [:basic_auth], allow: [:get],
9
7
  auth: {
10
8
  realm: "Custom",
11
9
  logic: ->(_, u, p) { %w(user password) == [u, p] }
@@ -1,11 +1,9 @@
1
1
  require_relative '../helper'
2
- require 'roy/plugin/before'
3
2
 
4
3
  class BeforeTestObject
5
4
  include Roy
6
- include Roy::Plugin::Before
7
5
 
8
- roy allow: [:get],
6
+ roy use: [:before], allow: [:get],
9
7
  before: lambda { |env|
10
8
  env['REQUEST_METHOD'] = 'GET'
11
9
  }
@@ -1,12 +1,10 @@
1
1
  require_relative '../helper'
2
- require 'roy/plugins'
3
- require 'roy/plugin/before'
2
+ require 'roy/before'
4
3
 
5
4
  class PluginsTestObject
6
5
  include Roy
7
- include Roy::Plugins(Plugin::Before, :after)
8
6
 
9
- roy allow: [:get],
7
+ roy use: [Before, :after], allow: [:get],
10
8
  before: lambda { |env|
11
9
  env['REQUEST_METHOD'] = 'GET'
12
10
  },
@@ -1,5 +1,4 @@
1
1
  require_relative '../helper'
2
- require 'roy/plugin/render'
3
2
 
4
3
  Templates = {
5
4
  simple: %q(<%= "Hello world!" %>),
@@ -10,28 +9,28 @@ Templates = {
10
9
 
11
10
  class RenderTestObject
12
11
  include Roy
13
- include Roy::Plugin::Render
14
12
 
15
13
  roy allow: [:get],
16
- views: 'test/views'
14
+ views: 'test/views',
15
+ use: [:render]
17
16
 
18
17
  def get(path)
19
18
  case path
20
19
  when '/template'
21
- render :erb, :test
20
+ roy.render :erb, :test
22
21
  when '/template_layout'
23
- render :erb, :layout do
24
- render :erb, :test
22
+ roy.render :erb, :layout do
23
+ roy.render :erb, :test
25
24
  end
26
25
  when '/inline'
27
- render :erb, Templates[:simple]
26
+ roy.render :erb, Templates[:simple]
28
27
  when '/locals'
29
- render :erb, Templates[:locals], person: 'Bob'
28
+ roy.render :erb, Templates[:locals], person: 'Bob'
30
29
  when '/scope'
31
- render :erb, Templates[:scope]
30
+ roy.render :erb, Templates[:scope]
32
31
  when '/yield'
33
- render :erb, Templates[:yield] do
34
- render :erb, Templates[:simple]
32
+ roy.render :erb, Templates[:yield] do
33
+ roy.render :erb, Templates[:simple]
35
34
  end
36
35
  end
37
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-21 00:00:00.000000000 Z
12
+ date: 2012-01-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
16
- requirement: &22715480 !ruby/object:Gem::Requirement
16
+ requirement: &6178220 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *22715480
24
+ version_requirements: *6178220
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: minitest
27
- requirement: &22714940 !ruby/object:Gem::Requirement
27
+ requirement: &6177760 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *22714940
35
+ version_requirements: *6177760
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rack-test
38
- requirement: &22714460 !ruby/object:Gem::Requirement
38
+ requirement: &6177200 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *22714460
46
+ version_requirements: *6177200
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: tilt
49
- requirement: &22713940 !ruby/object:Gem::Requirement
49
+ requirement: &6176320 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *22713940
57
+ version_requirements: *6176320
58
58
  description: ! 'roy is a small library which allows every Ruby object to be used
59
59
 
60
60
  as a Rack application.'
@@ -70,11 +70,11 @@ files:
70
70
  - README.md
71
71
  - Rakefile
72
72
  - lib/roy.rb
73
- - lib/roy/plugin/after.rb
74
- - lib/roy/plugin/basic_auth.rb
75
- - lib/roy/plugin/before.rb
76
- - lib/roy/plugin/render.rb
77
- - lib/roy/plugins.rb
73
+ - lib/roy/after.rb
74
+ - lib/roy/base.rb
75
+ - lib/roy/basic_auth.rb
76
+ - lib/roy/before.rb
77
+ - lib/roy/render.rb
78
78
  - lib/roy/version.rb
79
79
  - roy.gemspec
80
80
  - test/base_test.rb
@@ -126,3 +126,4 @@ test_files:
126
126
  - test/prefix_test.rb
127
127
  - test/views/layout.erb
128
128
  - test/views/test.erb
129
+ has_rdoc:
@@ -1,12 +0,0 @@
1
- module Roy
2
- module Plugin
3
- module After
4
- def call(env)
5
- status, header, body = super
6
- resp = Rack::Response.new(body, status, header)
7
- (roy.conf.after || lambda {|x| x }).(resp)
8
- resp.finish
9
- end
10
- end
11
- end
12
- end
@@ -1,22 +0,0 @@
1
- module Roy
2
- module Plugin
3
- module BasicAuth
4
- def protected!(data=nil)
5
- unless authorized?(data)
6
- realm = roy.conf.auth && roy.conf.auth[:realm] || 'Realm'
7
- roy.response['WWW-Authenticate'] = %(Basic realm="#{realm}")
8
- halt 401
9
- end
10
- end
11
-
12
- def authorized?(data=nil)
13
- auth = Rack::Auth::Basic::Request.new(roy.request.env)
14
-
15
- auth.provided? && auth.basic? && auth.credentials &&
16
- (roy.conf.auth[:logic] || ->(data, u, p) {
17
- %w(admin password) == [u, p]
18
- }).(data, *auth.credentials)
19
- end
20
- end
21
- end
22
- end
@@ -1,10 +0,0 @@
1
- module Roy
2
- module Plugin
3
- module Before
4
- def call(env)
5
- (roy.conf.before || lambda {|x| x }).(env)
6
- super
7
- end
8
- end
9
- end
10
- end
@@ -1,23 +0,0 @@
1
- require 'tilt'
2
-
3
- module Roy
4
- module Plugin
5
- module Render
6
-
7
- def render(engine, view_or_string, params={}, &block)
8
- options = roy.conf.render || {}
9
- template = case view_or_string
10
- when Symbol
11
- file = [view_or_string.to_s, engine].map(&:to_s).join('.')
12
- dir = roy.conf.views || 'views'
13
- Tilt.new(File.join(dir, file), nil, options)
14
- else
15
- Tilt[engine].new(nil, nil, options) { view_or_string.to_s }
16
- end
17
-
18
- template.render(self, params, &block)
19
- end
20
-
21
- end
22
- end
23
- end
data/lib/roy/plugins.rb DELETED
@@ -1,17 +0,0 @@
1
- module Roy
2
- module Plugin; end
3
-
4
- def self.Plugins(*names)
5
- return Module.new.tap do |mod|
6
- names.each do |name|
7
- if name.is_a?(Symbol)
8
- require "roy/plugin/#{name}"
9
- const = "#{name}".capitalize.gsub(/_(\w)/) {|m| m[1].upcase }.to_sym
10
- name = Roy::Plugin.const_get(const)
11
- end
12
- mod.send(:include, name)
13
- end
14
- end
15
- end
16
- end
17
-