|
1 | 1 | module API
|
2 |
| - module V3 |
3 |
| - module Public |
4 |
| - class Api::V3::Public::ValidateDeckController < ::ApplicationController |
5 |
| - def index |
6 |
| - out = params[:data] |
| 2 | + module V3 |
| 3 | + module Public |
| 4 | + class Api::V3::Public::ValidateDeckController < ::ApplicationController |
| 5 | + def index |
| 6 | + out = params[:data] |
7 | 7 |
|
8 |
| - # Check for presence of everything needed to perform deck validation and error if not present. |
9 |
| - if out.nil? or not (out.has_key?(:attributes) and out[:attributes].has_key?(:identity_card_id) and out[:attributes].has_key?(:side_id) and out[:attributes].has_key?(:cards) and out[:attributes].has_key?(:validations)) |
10 |
| - return render json: { |
11 |
| - :errors => [{ |
12 |
| - :title => "Invalid request", |
13 |
| - :detail => "Valid requests must be of the form `{'data': { 'attributes': { 'identity_card_id': 'foo', 'side_id': 'bar', 'cards': { }, 'validations': [] } } |
14 |
| -}`. Extra fields are allowed.", |
15 |
| - :code => "400", |
16 |
| - :status => "400" |
17 |
| - }]}, :status => :bad_request |
18 |
| - end |
| 8 | + # Check for presence of everything needed to perform deck validation and error if not present. |
| 9 | + if out.nil? or not ( |
| 10 | + out.has_key?(:attributes) and |
| 11 | + out[:attributes].has_key?(:identity_card_id) and |
| 12 | + out[:attributes].has_key?(:side_id) and |
| 13 | + out[:attributes].has_key?(:cards) and |
| 14 | + out[:attributes].has_key?(:validations)) |
| 15 | + return render json: { |
| 16 | + :errors => [{ |
| 17 | + :title => "Invalid request", |
| 18 | + :detail => "Valid requests must be of the form `{'data': { 'attributes': { 'identity_card_id': 'foo', 'side_id': 'bar', 'cards': { }, 'validations': [] } }}`. Extra fields are allowed.", |
| 19 | + :code => "400", |
| 20 | + :status => "400" |
| 21 | + }]}, :status => :bad_request |
| 22 | + end |
19 | 23 |
|
20 |
| - # Deck validation takes in a simple datastructure, so construct it instead of passing around ActionController::Parameters |
21 |
| - deck = { |
22 |
| - 'identity_card_id' => params[:data][:attributes][:identity_card_id], |
23 |
| - 'side_id' => params[:data][:attributes][:side_id], |
24 |
| - 'cards' => {}, |
25 |
| - 'validations' => [], |
26 |
| - } |
27 |
| - params[:data][:attributes][:cards].each {|c,q| deck['cards'][c] = q} |
28 |
| - params[:data][:attributes][:validations].each do |v| |
29 |
| - deck['validations'] << v |
30 |
| - end |
| 24 | + # Deck validation takes in a simple datastructure, so construct it instead of passing around ActionController::Parameters |
| 25 | + deck = { |
| 26 | + 'identity_card_id' => params[:data][:attributes][:identity_card_id], |
| 27 | + 'side_id' => params[:data][:attributes][:side_id], |
| 28 | + 'cards' => {}, |
| 29 | + 'validations' => [], |
| 30 | + } |
| 31 | + params[:data][:attributes][:cards].each {|c,q| deck['cards'][c] = q} |
| 32 | + params[:data][:attributes][:validations].each do |v| |
| 33 | + deck['validations'] << v |
| 34 | + end |
31 | 35 |
|
32 |
| - Rails.logger.info 'Deck is %s' % deck.inspect |
33 |
| - v = DeckValidator.new(deck) |
| 36 | + Rails.logger.info 'Deck is %s' % deck.inspect |
| 37 | + v = DeckValidator.new(deck) |
34 | 38 |
|
35 |
| - out[:attributes][:is_valid] = v.is_valid? |
36 |
| - out[:attributes][:validation_errors] = v.errors |
37 |
| - out[:attributes][:validations] = v.validations |
| 39 | + out[:attributes][:is_valid] = v.is_valid? |
| 40 | + out[:attributes][:validation_errors] = v.errors |
| 41 | + out[:attributes][:validations] = v.validations |
38 | 42 |
|
39 |
| - render json: { data: out }, :status => :ok |
40 |
| - end |
| 43 | + render json: { data: out }, :status => :ok |
41 | 44 | end
|
42 | 45 | end
|
43 | 46 | end
|
44 | 47 | end
|
| 48 | +end |
0 commit comments