lotus-controller 0.3.0 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f0f02211060856c6f853b05907e1993a23684a2
4
- data.tar.gz: 98db51b586aa274b812def351f2960219d26a254
3
+ metadata.gz: 394aa431f6edb629284fc999c3a317ce46a34b91
4
+ data.tar.gz: 97020ea1106fa6b4abe1a878143c9a2494603933
5
5
  SHA512:
6
- metadata.gz: 8a9988d2c322dba57028f60615652aa4307ef2808a1a742e7a12c15c70609defb83abec4cd8d0c31eddaf5de49e618f74a4010c97c2c03d95a4544fe459b388a
7
- data.tar.gz: 688a5dba74eecf6c8d6a14891c9b4d0d2c8b853864e66fa0beb6f09c0abd3918b18ab2daf957e64ece73a1e72ea86f803b67ffbf6bd3a8bc1e0e433a66767f05
6
+ metadata.gz: 02f845f2f20d2f91c4a5cf3247a70e231f9f776bea1b550b85e971d4abdb06027951c75096b7b9dd246421a00fd14bd6b8063cca5fd097ba51ce6fbfef38ae98
7
+ data.tar.gz: df96a33e1899c417311907ea6313c190000355076dcac07502096768369ef1ddc824dfd9e5214551b0e07ba89c0312c6dc21b635b66a797d4f9fb3c63ed184d9
data/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Lotus::Controller
2
2
  Complete, fast and testable actions for Rack
3
3
 
4
+ ## v0.3.1 - 2015-01-08
5
+ ### Added
6
+ - [Lasse Skindstad Ebert] Introduced `Action#request` which returns an instance a `Rack::Request` compliant object: `Lotus::Action::Request`.
7
+
8
+ ### Fixed
9
+ - [Steve Hodgkiss] Ensure params to return coerced values
10
+
4
11
  ## v0.3.0 - 2014-12-23
5
12
  ### Added
6
13
  - [Luca Guidi] Introduced `Action#request_id` as unique identifier for an incoming HTTP request
data/README.md CHANGED
@@ -1023,33 +1023,6 @@ Articles::Create.new.call({})
1023
1023
  # => raises ArgumentError because we set handle_exceptions to false
1024
1024
  ```
1025
1025
 
1026
- ### Reusability
1027
-
1028
- Lotus::Controller can be used as a singleton framework as seen in this README.
1029
- The application code includes `Lotus::Controller` or `Lotus::Action` directly
1030
- and the configuration is unique per Ruby process.
1031
-
1032
- While this is convenient for tiny applications, it doesn't fit well for more
1033
- complex scenarios, where we want micro applications to coexist together.
1034
-
1035
- ```ruby
1036
- require 'lotus/controller'
1037
-
1038
- module WebApp
1039
- Controller = Lotus::Controller.duplicate
1040
- end
1041
-
1042
- module ApiApp
1043
- Controller = Lotus::Controller.duplicate(self) do
1044
- handle_exception ArgumentError => 400
1045
- end
1046
- end
1047
- ```
1048
-
1049
- The code above defines `WebApp::Controller` and `WebApp::Action`, to be used for
1050
- the `WebApp` endpoints, while `ApiApp::Controller` and `ApiApp::Action` have
1051
- a different configuration.
1052
-
1053
1026
  ### Thread safety
1054
1027
 
1055
1028
  An Action is **mutable**. When used without Lotus::Router, be sure to instantiate an
@@ -90,7 +90,7 @@ module Lotus
90
90
  # @since 0.3.0
91
91
  # @api private
92
92
  def flash
93
- @session[SESSION_KEY]
93
+ @session[SESSION_KEY] || {}
94
94
  end
95
95
 
96
96
  # The flash registry that holds the data **only for** the current request
@@ -100,7 +100,7 @@ module Lotus
100
100
  # @since 0.3.0
101
101
  # @api private
102
102
  def data
103
- flash[@request_id]
103
+ flash[@request_id] || {}
104
104
  end
105
105
 
106
106
  # Expire the stale data from the previous request.
@@ -1,5 +1,5 @@
1
- require 'lotus/utils/hash'
2
1
  require 'lotus/validations'
2
+ require 'lotus/utils/attributes'
3
3
  require 'set'
4
4
 
5
5
  module Lotus
@@ -87,6 +87,10 @@ module Lotus
87
87
 
88
88
  include Lotus::Validations
89
89
 
90
+ def self.whitelisting?
91
+ defined_attributes.any?
92
+ end
93
+
90
94
  # @attr_reader env [Hash] the Rack env
91
95
  #
92
96
  # @since 0.2.0
@@ -103,12 +107,7 @@ module Lotus
103
107
  def initialize(env)
104
108
  @env = env
105
109
  super(_compute_params)
106
- freeze
107
- end
108
-
109
- def self.defined_attributes
110
- result = super
111
- return result if result.to_ary.any?
110
+ # freeze
112
111
  end
113
112
 
114
113
  # Returns the object associated with the given key
@@ -133,13 +132,19 @@ module Lotus
133
132
  alias_method :to_hash, :to_h
134
133
 
135
134
  private
135
+ # @since 0.3.1
136
+ # @api private
136
137
  def _compute_params
137
- Utils::Hash.new(
138
- _extract
139
- ).symbolize!
138
+ if self.class.whitelisting?
139
+ _whitelisted_params
140
+ else
141
+ @attributes = Utils::Attributes.new(_params)
142
+ end
140
143
  end
141
144
 
142
- def _extract
145
+ # @since 0.3.1
146
+ # @api private
147
+ def _params
143
148
  {}.tap do |result|
144
149
  if env.has_key?(RACK_INPUT)
145
150
  result.merge! ::Rack::Request.new(env).params
@@ -149,6 +154,18 @@ module Lotus
149
154
  end
150
155
  end
151
156
  end
157
+
158
+ # @since 0.3.1
159
+ # @api private
160
+ def _whitelisted_params
161
+ {}.tap do |result|
162
+ _params.each do |k, v|
163
+ next unless self.class.defined_attributes.include?(k.to_s)
164
+
165
+ result[k] = v
166
+ end
167
+ end
168
+ end
152
169
  end
153
170
  end
154
171
  end
@@ -1,4 +1,5 @@
1
1
  require 'securerandom'
2
+ require 'lotus/action/request'
2
3
 
3
4
  module Lotus
4
5
  module Action
@@ -142,6 +143,27 @@ module Lotus
142
143
  @request_id ||= SecureRandom.hex(DEFAULT_REQUEST_ID_LENGTH)
143
144
  end
144
145
 
146
+ # Returns a Lotus specialized rack request
147
+ #
148
+ # @return [Lotus::Action::Request] The request
149
+ #
150
+ # @since 0.3.1
151
+ #
152
+ # @example
153
+ # require 'lotus/controller'
154
+ #
155
+ # class Create
156
+ # include Lotus::Action
157
+ #
158
+ # def call(params)
159
+ # ip = request.ip
160
+ # secure = request.ssl?
161
+ # end
162
+ # end
163
+ def request
164
+ @request ||= ::Lotus::Action::Request.new(@_env)
165
+ end
166
+
145
167
  private
146
168
 
147
169
  # Sets the HTTP status code for the response
@@ -0,0 +1,86 @@
1
+ require 'rack/request'
2
+
3
+ module Lotus
4
+ module Action
5
+ # An HTTP request based on top of Rack::Request.
6
+ # This guarantees backwards compatibility with with Rack.
7
+ #
8
+ # @since 0.3.1
9
+ #
10
+ # @see http://www.rubydoc.info/gems/rack/Rack/Request
11
+ class Request < ::Rack::Request
12
+
13
+ # @raise [NotImplementedError]
14
+ #
15
+ # @since 0.3.1
16
+ # @api private
17
+ def content_type
18
+ raise NotImplementedError, 'Please use Action#content_type'
19
+ end
20
+
21
+ # @raise [NotImplementedError]
22
+ #
23
+ # @since 0.3.1
24
+ # @api private
25
+ def session
26
+ raise NotImplementedError, 'Please include Action::Session and use Action#session'
27
+ end
28
+
29
+ # @raise [NotImplementedError]
30
+ #
31
+ # @since 0.3.1
32
+ # @api private
33
+ def cookies
34
+ raise NotImplementedError, 'Please include Action::Cookies and use Action#cookies'
35
+ end
36
+
37
+ # @raise [NotImplementedError]
38
+ #
39
+ # @since 0.3.1
40
+ # @api private
41
+ def params
42
+ raise NotImplementedError, 'Please use params passed to Action#call'
43
+ end
44
+
45
+ # @raise [NotImplementedError]
46
+ #
47
+ # @since 0.3.1
48
+ # @api private
49
+ def update_param(*)
50
+ raise NotImplementedError, 'Please use params passed to Action#call'
51
+ end
52
+
53
+ # @raise [NotImplementedError]
54
+ #
55
+ # @since 0.3.1
56
+ # @api private
57
+ def delete_param(*)
58
+ raise NotImplementedError, 'Please use params passed to Action#call'
59
+ end
60
+
61
+ # @raise [NotImplementedError]
62
+ #
63
+ # @since 0.3.1
64
+ # @api private
65
+ def [](*)
66
+ raise NotImplementedError, 'Please use params passed to Action#call'
67
+ end
68
+
69
+ # @raise [NotImplementedError]
70
+ #
71
+ # @since 0.3.1
72
+ # @api private
73
+ def []=(*)
74
+ raise NotImplementedError, 'Please use params passed to Action#call'
75
+ end
76
+
77
+ # @raise [NotImplementedError]
78
+ #
79
+ # @since 0.3.1
80
+ # @api private
81
+ def values_at(*)
82
+ raise NotImplementedError, 'Please use params passed to Action#call'
83
+ end
84
+ end
85
+ end
86
+ end
@@ -3,6 +3,6 @@ module Lotus
3
3
  # Defines the version
4
4
  #
5
5
  # @since 0.1.0
6
- VERSION = '0.3.0'.freeze
6
+ VERSION = '0.3.1'.freeze
7
7
  end
8
8
  end
@@ -6,8 +6,8 @@ require 'lotus/controller/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'lotus-controller'
8
8
  spec.version = Lotus::Controller::VERSION
9
- spec.authors = ['Luca Guidi']
10
- spec.email = ['me@lucaguidi.com']
9
+ spec.authors = ['Luca Guidi', 'Trung Lê']
10
+ spec.email = ['me@lucaguidi.com', 'trung.le@ruby-journal.com']
11
11
  spec.description = %q{Complete, fast and testable actions for Rack}
12
12
  spec.summary = %q{Complete, fast and testable actions for Rack and Lotus}
13
13
  spec.homepage = 'http://lotusrb.org'
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_dependency 'rack', '~> 1.5'
23
23
  spec.add_dependency 'lotus-utils', '~> 0.3', '>= 0.3.2'
24
- spec.add_dependency 'lotus-validations', '~> 0.2', '>= 0.2.1'
24
+ spec.add_dependency 'lotus-validations', '~> 0.2', '>= 0.2.2'
25
25
 
26
26
  spec.add_development_dependency 'bundler', '~> 1.6'
27
27
  spec.add_development_dependency 'minitest', '~> 5'
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lotus-controller
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
8
+ - Trung Lê
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-12-23 00:00:00.000000000 Z
12
+ date: 2015-01-08 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rack
@@ -53,7 +54,7 @@ dependencies:
53
54
  version: '0.2'
54
55
  - - ">="
55
56
  - !ruby/object:Gem::Version
56
- version: 0.2.1
57
+ version: 0.2.2
57
58
  type: :runtime
58
59
  prerelease: false
59
60
  version_requirements: !ruby/object:Gem::Requirement
@@ -63,7 +64,7 @@ dependencies:
63
64
  version: '0.2'
64
65
  - - ">="
65
66
  - !ruby/object:Gem::Version
66
- version: 0.2.1
67
+ version: 0.2.2
67
68
  - !ruby/object:Gem::Dependency
68
69
  name: bundler
69
70
  requirement: !ruby/object:Gem::Requirement
@@ -123,6 +124,7 @@ dependencies:
123
124
  description: Complete, fast and testable actions for Rack
124
125
  email:
125
126
  - me@lucaguidi.com
127
+ - trung.le@ruby-journal.com
126
128
  executables: []
127
129
  extensions: []
128
130
  extra_rdoc_files: []
@@ -149,6 +151,7 @@ files:
149
151
  - lib/lotus/action/params.rb
150
152
  - lib/lotus/action/rack.rb
151
153
  - lib/lotus/action/redirect.rb
154
+ - lib/lotus/action/request.rb
152
155
  - lib/lotus/action/session.rb
153
156
  - lib/lotus/action/throwable.rb
154
157
  - lib/lotus/action/validatable.rb
@@ -178,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
178
181
  version: '0'
179
182
  requirements: []
180
183
  rubyforge_project:
181
- rubygems_version: 2.2.2
184
+ rubygems_version: 2.4.5
182
185
  signing_key:
183
186
  specification_version: 4
184
187
  summary: Complete, fast and testable actions for Rack and Lotus