-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.rb
100 lines (84 loc) · 3.19 KB
/
create.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
module Web::Controllers::User
class Create
include Web::Action
# Action is called with the following params - see spec/web/controllers/user/create_spec.rb:
#
# {
# data: {
# attributes: {
# email: 'th@skalar.no',
# password: 'secret',
# alternative_emails: [
# {email: "th@skalar.no", description: "work"},
# {email: "th@home.no", description: "home"}
# ]
# }
# }
# }
params do
required(:data).schema do
required(:attributes).schema do
required(:email).filled(:str?, format?: /@/)
required(:password).filled(:str?)
# The following blocks of commented code is me trying different ways to
# get the alternative_emails read correclty.
#
# Please read the #call and then each ATTEMPT below as each comment includes the
# printed lines from #call.
# ATTEMPT 1:
#
# required(:alternative_emails).each(:hash?)
#
# RESULT, printed out:
#
# ERRORS: {:data=>{:attributes=>{:alternative_emails=>{0=>["must be a hash"], 1=>["must be a hash"]}}}}
#
#
# Looping over each alternative_emails; each expected to be a hash
# CLASS: String: "{:email=>\"th@skalar.no\", :description=>\"work\"}"
# CLASS: String: "{:email=>\"th@home.no\", :description=>\"home\"}"
# ATTEMPT 2:
#
# required(:alternative_emails).each do
# schema do
# required(:email).filled(:str?, format?: /@/)
# required(:description).filled(:str?)
# end
# end
#
# RESULT, printed out:
#
# # ERRORS: {:data=>{:attributes=>{:alternative_emails=>{0=>["must be a hash"], 1=>["must be a hash"]}}}}
#
#
# Looping over each alternative_emails; each expected to be a hash
# CLASS: String: "{:email=>\"th@skalar.no\", :description=>\"work\"}"
# CLASS: String: "{:email=>\"th@home.no\", :description=>\"home\"}"
# ATTEMPT 3:
#
# Comment out the *complete params bock* and run test. This actually returns expected
# hashes.
#
# RESULT, printed out:
#
# Looping over each alternative_emails; each expected to be a hash
# CLASS: Hash: {:email=>"th@skalar.no", :description=>"work"}
# CLASS: Hash: {:email=>"th@home.no", :description=>"home"}
end
end
end
def call(params)
puts "\nERRORS: #{params.errors.inspect}" if params.respond_to? :errors
puts "\n\nLooping over each alternative_emails; each expected to be a hash"
params.get('data.attributes.alternative_emails').each do |alternative|
puts "CLASS: #{alternative.class}: #{alternative.inspect}"
end
puts "\n... Setting a response with values from params\n\n"
self.body = {
email: params.get('data.attributes.email'),
password: params.get('data.attributes.password'),
alternative_emails: params.get('data.attributes.alternative_emails')
}.to_json
end
end
end