vayacondios-server 0.2.6 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
data/app/http_shim.rb CHANGED
@@ -4,22 +4,41 @@ require 'gorillib/logger/log'
4
4
  require 'vayacondios-server'
5
5
 
6
6
  class HttpShim < Goliath::API
7
+ use Goliath::Rack::Heartbeat # respond to /status with 200, OK (monitoring, etc)
8
+ use Vayacondios::Rack::JSONize
7
9
  use Goliath::Rack::Tracer, 'X-Tracer' # log trace statistics
8
- use Goliath::Rack::Params # parse query string and message body into params hash
10
+ use Vayacondios::Rack::Params # parse query string and message body into params hash
11
+ # use Goliath::Rack::Params # parse query string and message body into params hash
9
12
  use Goliath::Rack::Validation::RequestMethod, %w[GET PUT PATCH DELETE] # only allow these methods
10
13
  use Vayacondios::Rack::ExtractMethods # interpolate GET, PUT into :create, :update, etc
11
14
  use Vayacondios::Rack::Path # parse path into parameterized pieces
12
15
  use Vayacondios::Rack::PathValidation # validate the existence of env[:vayacondios_path]
13
16
  use Goliath::Rack::Formatters::JSON # JSON output formatter
14
17
  use Goliath::Rack::Render # auto-negotiate response format
15
- use Goliath::Rack::Heartbeat # respond to /status with 200, OK (monitoring, etc)
18
+
19
+ # The document part of the request, e.g. - params that came
20
+ # directly from its body.
21
+ #
22
+ # Something somewhere in Rack is unhappy when receiving
23
+ # non-Hash-like records via a JSON-formatted request body. So that
24
+ # Vayacondios::Rack::Params takes a non-Hash-like request body and
25
+ # turns it into a Hash with a single key: _document.
26
+ #
27
+ # This hack does **not** affect the client-side: clients can still
28
+ # send non-Hash-like JSON documents and they will be interpreted
29
+ # as intended.
30
+ #
31
+ # @return [Hash,Array,String,Fixnum,nil] any native JSON datatype
32
+ def document
33
+ params['_document'] || params
34
+ end
16
35
 
17
36
  def response(env)
18
37
  path_params = env[:vayacondios_path]
19
38
  klass = ('vayacondios/' + path_params[:type] + '_handler').camelize.constantize
20
39
 
21
40
  Log.info("received request #{env['REQUEST_METHOD']} #{env['REQUEST_URI']}")
22
- Log.info("params: #{env['params']}")
41
+ Log.info("params: #{document}")
23
42
 
24
43
  begin
25
44
  case env[:vayacondios_method]
@@ -29,15 +48,15 @@ class HttpShim < Goliath::API
29
48
  [200, {}, MultiJson.dump(record.body)]
30
49
 
31
50
  when :update
32
- record = klass.new(mongo).update(env['params'], path_params)
51
+ record = klass.new(mongo).update(document, path_params)
33
52
  [200, {}, nil]
34
53
 
35
54
  when :patch
36
- record = klass.new(mongo).patch(env['params'], path_params)
55
+ record = klass.new(mongo).patch(document, path_params)
37
56
  [200, {}, nil]
38
57
 
39
58
  when :delete
40
- record = klass.find(mongo, path_params).destroy(env['params'])
59
+ record = klass.find(mongo, path_params).destroy(document)
41
60
  [200, {}, MultiJson.dump(record.body)]
42
61
  end
43
62
  rescue Vayacondios::Error::NotFound => ex
@@ -4,4 +4,4 @@ mongo:
4
4
  database: vayacondios_dev
5
5
 
6
6
  vayacondios:
7
- legacy: true
7
+ legacy: false
@@ -4,4 +4,4 @@ mongo:
4
4
  database: vayacondios_dev
5
5
 
6
6
  vayacondios:
7
- legacy: true
7
+ legacy: false
@@ -22,7 +22,8 @@ require 'vayacondios/server/handlers/config_handler'
22
22
  require 'vayacondios/server/handlers/event_handler'
23
23
  require 'vayacondios/server/handlers/itemset_handler'
24
24
 
25
- require 'vayacondios/server/rack/assume_json'
26
25
  require 'vayacondios/server/rack/extract_methods'
26
+ require 'vayacondios/server/rack/params'
27
+ require 'vayacondios/server/rack/jsonize'
27
28
  require 'vayacondios/server/rack/path'
28
29
  require 'vayacondios/server/rack/path_validation'
@@ -8,18 +8,14 @@ class Vayacondios
8
8
  def wrap_contents(contents) {contents: contents} end
9
9
  def extract_contents(document) document['contents'] end
10
10
  def extract_response(document) document['contents'] end
11
- def proper_request(document)
12
- document.is_a? Hash and document.fetch('_json', {}).is_a? Hash
13
- end
11
+ def proper_request(document) true end
14
12
  end
15
13
 
16
14
  class LegacyContentsHandler
17
15
  def wrap_contents(contents) contents end
18
- def extract_contents(document) document.fetch('_json', {}) end
16
+ def extract_contents(document) document end
19
17
  def extract_response(document) document end
20
- def proper_request(document)
21
- document.is_a? Array or document.fetch('_json', {}).is_a? Array
22
- end
18
+ def proper_request(document) true end
23
19
  end
24
20
 
25
21
  @@legacy_switch = nil
@@ -8,18 +8,14 @@ class Vayacondios
8
8
  def wrap_contents(contents) {contents: contents} end
9
9
  def extract_contents(document) document['contents'] end
10
10
  def extract_response(document) document['contents'] end
11
- def proper_request(document)
12
- document.is_a? Hash and document.fetch('_json', {}).is_a? Hash
13
- end
11
+ def proper_request(document) true end
14
12
  end
15
13
 
16
14
  class LegacyContentsHandler
17
15
  def wrap_contents(contents) contents end
18
- def extract_contents(document) document.fetch('_json', {}) end
16
+ def extract_contents(document) document end
19
17
  def extract_response(document) document end
20
- def proper_request(document)
21
- document.is_a? Array or document.fetch('_json', {}).is_a? Array
22
- end
18
+ def proper_request(document) true end
23
19
  end
24
20
 
25
21
  @@legacy_switch = nil
@@ -8,18 +8,14 @@ class Vayacondios
8
8
  def wrap_contents(contents) {contents: contents} end
9
9
  def extract_contents(document) document['contents'] end
10
10
  def extract_response(document) document['contents'] end
11
- def proper_request(document)
12
- document.is_a? Hash and document.fetch('_json', {}).is_a? Hash
13
- end
11
+ def proper_request(document) true end
14
12
  end
15
13
 
16
14
  class LegacyContentsHandler
17
15
  def wrap_contents(contents) contents end
18
- def extract_contents(document) document.fetch('_json', {}) end
16
+ def extract_contents(document) document end
19
17
  def extract_response(document) document end
20
- def proper_request(document)
21
- document.is_a? Array or document.fetch('_json', {}).is_a? Array
22
- end
18
+ def proper_request(document) true end
23
19
  end
24
20
 
25
21
  @@legacy_switch = nil
@@ -0,0 +1,39 @@
1
+ class Vayacondios
2
+ module Rack
3
+
4
+ # Sets the `Content-Type` of the request and response to
5
+ # `application/json`.
6
+ class JSONize
7
+ include Goliath::Rack::AsyncMiddleware
8
+
9
+ # Sets the `Content-Type` header to `application/json` so
10
+ # downstream parameter parsing will work correctly.
11
+ #
12
+ # @param [Hash] env the request environment
13
+ def call(env)
14
+ env.logger.debug("Started #{env[Goliath::Request::REQUEST_METHOD]} \"#{env[Goliath::Request::REQUEST_PATH]}\" for #{env[Goliath::Request::REMOTE_ADDR]} as JSON")
15
+ env['CONTENT_TYPE'] = 'application/json'
16
+ super(env)
17
+ end
18
+
19
+ # Sets the `Content-Type` header to `application/json` and
20
+ # serializes the response body to a JSON string.
21
+ #
22
+ # Will *not* do this if the request was sent to `/status` which
23
+ # is expected to just return the string `OK`.
24
+ #
25
+ # @param [Hash] env the request environment
26
+ # @param [Integer] status the HTTP status code of the response
27
+ # @param [Hash] headers the HTTP headers of the response
28
+ # @param [Object] body the upstream response body
29
+ # @return [Array] the response
30
+ def post_process(env, status, headers, body)
31
+ return [status, headers, body] if env["REQUEST_PATH"] == '/status'
32
+ headers['Content-Type'] = 'application/json'
33
+ body = [MultiJson.encode(body) + "\n"]
34
+ [status, headers, body]
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,50 @@
1
+ class Vayacondios
2
+ module Rack
3
+
4
+ # Parses parameters in the request.
5
+ #
6
+ # Will parse parameters from the query string as well as from the
7
+ # request body.
8
+ #
9
+ # Rack gets unhappy when the parameters are anything other than a
10
+ # Hash but we want clients to be able to send non-Hash-like
11
+ # request bodies. To support this case, the request's params are
12
+ # stored into the `_document` key of the params, to be fetched
13
+ # later by Vayacondios::HttpServer#document.
14
+ class Params < Goliath::Rack::Params
15
+
16
+ # Parses the parameters of the request.
17
+ #
18
+ # @param [Hash] env the request environment
19
+ def retrieve_params(env)
20
+ params = {}
21
+ params.merge!(::Rack::Utils.parse_nested_query(env['QUERY_STRING']))
22
+
23
+ if env['rack.input']
24
+ post_params = ::Rack::Utils::Multipart.parse_multipart(env)
25
+ unless post_params
26
+ body = env['rack.input'].read
27
+ return params if body.empty?
28
+ env['rack.input'].rewind
29
+
30
+ post_params = case(env['CONTENT_TYPE'])
31
+ when Goliath::Rack::URL_ENCODED then
32
+ ::Rack::Utils.parse_nested_query(body)
33
+ when Goliath::Rack::JSON_ENCODED then
34
+ MultiJson.decode(body)
35
+ else
36
+ {}
37
+ end
38
+ if post_params.is_a?(Hash)
39
+ params.merge!(post_params)
40
+ else
41
+ params['_document'] = post_params
42
+ end
43
+ end
44
+ end
45
+ params
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -1,3 +1,3 @@
1
1
  class Vayacondios
2
- VERSION = '0.2.6'
2
+ VERSION = '0.2.7'
3
3
  end
data/pom.xml CHANGED
@@ -4,14 +4,14 @@
4
4
  <groupId>com.infochimps</groupId>
5
5
  <artifactId>vayacondios</artifactId>
6
6
  <packaging>jar</packaging>
7
- <!-- ICSVERS --><version>1.6.2-SNAPSHOT</version>
7
+ <!-- ICSVERS --><version>1.7.0-SNAPSHOT</version>
8
8
  <name>java-common</name>
9
9
  <url>http://maven.apache.org</url>
10
10
 
11
11
  <parent>
12
12
  <groupId>com.infochimps</groupId>
13
13
  <artifactId>parent-pom</artifactId>
14
- <!-- ICSVERS --><version>1.6.2-SNAPSHOT</version>
14
+ <!-- ICSVERS --><version>1.7.0-SNAPSHOT</version>
15
15
  <relativePath></relativePath>
16
16
  </parent>
17
17
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vayacondios-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2013-07-19 00:00:00.000000000 Z
15
+ date: 2013-08-08 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: configliere
@@ -222,8 +222,9 @@ files:
222
222
  - lib/vayacondios/server/model/document.rb
223
223
  - lib/vayacondios/server/model/event_document.rb
224
224
  - lib/vayacondios/server/model/itemset_document.rb
225
- - lib/vayacondios/server/rack/assume_json.rb
226
225
  - lib/vayacondios/server/rack/extract_methods.rb
226
+ - lib/vayacondios/server/rack/jsonize.rb
227
+ - lib/vayacondios/server/rack/params.rb
227
228
  - lib/vayacondios/server/rack/path.rb
228
229
  - lib/vayacondios/server/rack/path_validation.rb
229
230
  - lib/vayacondios/version.rb
@@ -279,7 +280,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
279
280
  version: '0'
280
281
  segments:
281
282
  - 0
282
- hash: 2406222611066502190
283
+ hash: 1685170955492084086
283
284
  required_rubygems_version: !ruby/object:Gem::Requirement
284
285
  none: false
285
286
  requirements:
@@ -288,7 +289,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
288
289
  version: '0'
289
290
  segments:
290
291
  - 0
291
- hash: 2406222611066502190
292
+ hash: 1685170955492084086
292
293
  requirements: []
293
294
  rubyforge_project:
294
295
  rubygems_version: 1.8.25
@@ -1,13 +0,0 @@
1
- class Vayacondios
2
- module Rack
3
- class AssumeJSON
4
- include Goliath::Rack::AsyncMiddleware
5
-
6
- def call(env)
7
- env['CONTENT_TYPE'] =
8
- 'application/json' unless env.has_key? 'CONTENT_TYPE'
9
- super env
10
- end
11
- end
12
- end
13
- end