acts_as_api 0.3.2 → 0.3.3

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.
@@ -0,0 +1,62 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe ActsAsApi::Base do
4
+
5
+ describe "calling a closure in the api template", :orm => :active_record do
6
+
7
+ before(:each) do
8
+ setup_models
9
+ end
10
+
11
+ after(:each) do
12
+ clean_up
13
+ end
14
+
15
+ describe "i.e. a proc (the record is passed as only parameter)" do
16
+
17
+ before(:each) do
18
+ @response = @luke.as_api_response(:calling_a_proc)
19
+ end
20
+
21
+ it "returns a hash" do
22
+ @response.should be_kind_of(Hash)
23
+ end
24
+
25
+ it "returns the correct number of fields" do
26
+ @response.should have(2).keys
27
+ end
28
+
29
+ it "returns all specified fields" do
30
+ @response.keys.sort_by(&:to_s).should eql([:all_caps_name, :without_param])
31
+ end
32
+
33
+ it "returns the correct values for the specified fields" do
34
+ @response.values.sort.should eql(["LUKE SKYWALKER", "Time"])
35
+ end
36
+ end
37
+
38
+ describe "i.e. a lambda (the record is passed as only parameter)" do
39
+
40
+ before(:each) do
41
+ @response = @luke.as_api_response(:calling_a_lambda)
42
+ end
43
+
44
+ it "returns a hash" do
45
+ @response.should be_kind_of(Hash)
46
+ end
47
+
48
+ it "returns the correct number of fields" do
49
+ @response.should have(2).keys
50
+ end
51
+
52
+ it "returns all specified fields" do
53
+ @response.keys.sort_by(&:to_s).should eql([:all_caps_name, :without_param])
54
+ end
55
+
56
+ it "returns the correct values for the specified fields" do
57
+ @response.values.sort.should eql(["LUKE SKYWALKER", "Time"])
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,178 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe ActsAsApi::Base do
4
+
5
+ describe "conditional statements", :orm => :active_record do
6
+
7
+ before(:each) do
8
+ setup_models
9
+ end
10
+
11
+ after(:each) do
12
+ clean_up
13
+ end
14
+
15
+ describe "using the :if option" do
16
+
17
+ describe "passing a symbol" do
18
+
19
+ describe "that returns false" do
20
+
21
+ before(:each) do
22
+ @response = @luke.as_api_response(:if_over_thirty)
23
+ end
24
+
25
+ it "returns a hash" do
26
+ @response.should be_kind_of(Hash)
27
+ end
28
+
29
+ it "returns the correct number of fields" do
30
+ @response.should have(1).keys
31
+ end
32
+
33
+ it "won't add the conditional field but all others" do
34
+ @response.keys.should include(:first_name)
35
+ @response.keys.should_not include(:full_name)
36
+ end
37
+
38
+ it "the other specified fields have the correct value" do
39
+ @response.values.should include(@luke.first_name)
40
+ end
41
+
42
+ end
43
+
44
+ describe "that returns nil" do
45
+
46
+ before(:each) do
47
+ @response = @luke.as_api_response(:if_returns_nil)
48
+ end
49
+
50
+ it "returns a hash" do
51
+ @response.should be_kind_of(Hash)
52
+ end
53
+
54
+ it "returns the correct number of fields" do
55
+ @response.should have(1).keys
56
+ end
57
+
58
+ it "won't add the conditional field but all others" do
59
+ @response.keys.should include(:first_name)
60
+ @response.keys.should_not include(:full_name)
61
+ end
62
+
63
+ it "the other specified fields have the correct value" do
64
+ @response.values.should include(@luke.first_name)
65
+ end
66
+
67
+ end
68
+
69
+ describe "that returns true" do
70
+
71
+ before(:each) do
72
+ @response = @han.as_api_response(:if_over_thirty)
73
+ end
74
+
75
+ it "returns a hash" do
76
+ @response.should be_kind_of(Hash)
77
+ end
78
+
79
+ it "returns the correct number of fields" do
80
+ @response.should have(2).keys
81
+ end
82
+
83
+ it "won't add the conditional field but all others" do
84
+ @response.keys.should include(:first_name)
85
+ @response.keys.should include(:last_name)
86
+ end
87
+
88
+ it "the other specified fields have the correct value" do
89
+ @response.values.should include(@han.first_name, @han.last_name)
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+
98
+ describe "passing a proc" do
99
+
100
+ describe "that returns false" do
101
+
102
+ before(:each) do
103
+ @response = @luke.as_api_response(:if_over_thirty_proc)
104
+ end
105
+
106
+ it "returns a hash" do
107
+ @response.should be_kind_of(Hash)
108
+ end
109
+
110
+ it "returns the correct number of fields" do
111
+ @response.should have(1).keys
112
+ end
113
+
114
+ it "won't add the conditional field but all others" do
115
+ @response.keys.should include(:first_name)
116
+ @response.keys.should_not include(:full_name)
117
+ end
118
+
119
+ it "the other specified fields have the correct value" do
120
+ @response.values.should include(@luke.first_name)
121
+ end
122
+
123
+ end
124
+
125
+ describe "that returns nil" do
126
+
127
+ before(:each) do
128
+ @response = @luke.as_api_response(:if_returns_nil_proc)
129
+ end
130
+
131
+ it "returns a hash" do
132
+ @response.should be_kind_of(Hash)
133
+ end
134
+
135
+ it "returns the correct number of fields" do
136
+ @response.should have(1).keys
137
+ end
138
+
139
+ it "won't add the conditional field but all others" do
140
+ @response.keys.should include(:first_name)
141
+ @response.keys.should_not include(:full_name)
142
+ end
143
+
144
+ it "the other specified fields have the correct value" do
145
+ @response.values.should include(@luke.first_name)
146
+ end
147
+
148
+ end
149
+
150
+ describe "that returns true" do
151
+
152
+ before(:each) do
153
+ @response = @han.as_api_response(:if_over_thirty_proc)
154
+ end
155
+
156
+ it "returns a hash" do
157
+ @response.should be_kind_of(Hash)
158
+ end
159
+
160
+ it "returns the correct number of fields" do
161
+ @response.should have(2).keys
162
+ end
163
+
164
+ it "won't add the conditional field but all others" do
165
+ @response.keys.should include(:first_name)
166
+ @response.keys.should include(:last_name)
167
+ end
168
+
169
+ it "the other specified fields have the correct value" do
170
+ @response.values.should include(@han.first_name, @han.last_name)
171
+ end
172
+
173
+ end
174
+
175
+ end
176
+
177
+ end
178
+ end
@@ -0,0 +1,178 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe ActsAsApi::Base do
4
+
5
+ describe "conditional statements", :orm => :active_record do
6
+
7
+ before(:each) do
8
+ setup_models
9
+ end
10
+
11
+ after(:each) do
12
+ clean_up
13
+ end
14
+
15
+ describe "using the :unless option" do
16
+
17
+ describe "passing a symbol" do
18
+
19
+ describe "that returns false" do
20
+
21
+ before(:each) do
22
+ @response = @luke.as_api_response(:unless_under_thirty)
23
+ end
24
+
25
+ it "returns a hash" do
26
+ @response.should be_kind_of(Hash)
27
+ end
28
+
29
+ it "returns the correct number of fields" do
30
+ @response.should have(1).keys
31
+ end
32
+
33
+ it "won't add the conditional field but all others" do
34
+ @response.keys.should include(:first_name)
35
+ @response.keys.should_not include(:full_name)
36
+ end
37
+
38
+ it "the other specified fields have the correct value" do
39
+ @response.values.should include(@luke.first_name)
40
+ end
41
+
42
+ end
43
+
44
+ describe "that returns nil" do
45
+
46
+ before(:each) do
47
+ @response = @luke.as_api_response(:unless_returns_nil)
48
+ end
49
+
50
+ it "returns a hash" do
51
+ @response.should be_kind_of(Hash)
52
+ end
53
+
54
+ it "returns the correct number of fields" do
55
+ @response.should have(2).keys
56
+ end
57
+
58
+ it "won't add the conditional field but all others" do
59
+ @response.keys.should include(:first_name)
60
+ @response.keys.should include(:last_name)
61
+ end
62
+
63
+ it "the other specified fields have the correct value" do
64
+ @response.values.should include(@luke.first_name, @luke.last_name)
65
+ end
66
+
67
+ end
68
+
69
+ describe "that returns true" do
70
+
71
+ before(:each) do
72
+ @response = @han.as_api_response(:unless_under_thirty)
73
+ end
74
+
75
+ it "returns a hash" do
76
+ @response.should be_kind_of(Hash)
77
+ end
78
+
79
+ it "returns the correct number of fields" do
80
+ @response.should have(2).keys
81
+ end
82
+
83
+ it "won't add the conditional field but all others" do
84
+ @response.keys.should include(:first_name)
85
+ @response.keys.should include(:last_name)
86
+ end
87
+
88
+ it "the other specified fields have the correct value" do
89
+ @response.values.should include(@han.first_name, @han.last_name)
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+
98
+ describe "passing a proc" do
99
+
100
+ describe "that returns false" do
101
+
102
+ before(:each) do
103
+ @response = @luke.as_api_response(:unless_under_thirty_proc)
104
+ end
105
+
106
+ it "returns a hash" do
107
+ @response.should be_kind_of(Hash)
108
+ end
109
+
110
+ it "returns the correct number of fields" do
111
+ @response.should have(1).keys
112
+ end
113
+
114
+ it "won't add the conditional field but all others" do
115
+ @response.keys.should include(:first_name)
116
+ @response.keys.should_not include(:full_name)
117
+ end
118
+
119
+ it "the other specified fields have the correct value" do
120
+ @response.values.should include(@luke.first_name)
121
+ end
122
+
123
+ end
124
+
125
+ describe "that returns nil" do
126
+
127
+ before(:each) do
128
+ @response = @luke.as_api_response(:if_returns_nil_proc)
129
+ end
130
+
131
+ it "returns a hash" do
132
+ @response.should be_kind_of(Hash)
133
+ end
134
+
135
+ it "returns the correct number of fields" do
136
+ @response.should have(1).keys
137
+ end
138
+
139
+ it "won't add the conditional field but all others" do
140
+ @response.keys.should include(:first_name)
141
+ @response.keys.should_not include(:full_name)
142
+ end
143
+
144
+ it "the other specified fields have the correct value" do
145
+ @response.values.should include(@luke.first_name)
146
+ end
147
+
148
+ end
149
+
150
+ describe "that returns true" do
151
+
152
+ before(:each) do
153
+ @response = @han.as_api_response(:unless_under_thirty_proc)
154
+ end
155
+
156
+ it "returns a hash" do
157
+ @response.should be_kind_of(Hash)
158
+ end
159
+
160
+ it "returns the correct number of fields" do
161
+ @response.should have(2).keys
162
+ end
163
+
164
+ it "won't add the conditional field but all others" do
165
+ @response.keys.should include(:first_name)
166
+ @response.keys.should include(:last_name)
167
+ end
168
+
169
+ it "the other specified fields have the correct value" do
170
+ @response.values.should include(@han.first_name, @han.last_name)
171
+ end
172
+
173
+ end
174
+
175
+ end
176
+
177
+ end
178
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe ActsAsApi::Base do
4
+
5
+ describe "acts_as_api is enabled", :orm => :active_record do
6
+
7
+ it "indicates that acts_as_api is enabled" do
8
+ User.acts_as_api?.should be_true
9
+ end
10
+
11
+ it "does respond to api_accessible" do
12
+ User.should respond_to :api_accessible
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe ActsAsApi::Base do
4
+
5
+ describe "extending a given api template", :orm => :active_record do
6
+
7
+ before(:each) do
8
+ setup_models
9
+ end
10
+
11
+ after(:each) do
12
+ clean_up
13
+ end
14
+
15
+ describe "multiple times" do
16
+
17
+ before(:each) do
18
+ User.api_accessible :public do |t|
19
+ t.add :first_name
20
+ end
21
+
22
+ User.api_accessible :for_buddies, :extend => :public do |t|
23
+ t.add :age
24
+ end
25
+
26
+ User.api_accessible :private, :extend => :for_buddies do |t|
27
+ t.add :last_name
28
+ end
29
+ @response = @luke.as_api_response(:private)
30
+ end
31
+
32
+ it "returns a hash" do
33
+ @response.should be_kind_of(Hash)
34
+ end
35
+
36
+ it "returns the correct number of fields" do
37
+ @response.should have(3).keys
38
+ end
39
+
40
+ it "returns all specified fields" do
41
+ @response.keys.sort_by(&:to_s).should eql([:age, :first_name, :last_name])
42
+ end
43
+
44
+ it "returns the correct values for the specified fields" do
45
+ @response.values.sort_by(&:to_s).should eql([@luke.age, @luke.first_name, @luke.last_name].sort_by(&:to_s))
46
+ end
47
+
48
+ end
49
+
50
+ describe "and removing a former added value" do
51
+
52
+ before(:each) do
53
+ @response = @luke.as_api_response(:age_and_first_name)
54
+ end
55
+
56
+ it "returns a hash" do
57
+ @response.should be_kind_of(Hash)
58
+ end
59
+
60
+ it "returns the correct number of fields" do
61
+ @response.should have(2).keys
62
+ end
63
+
64
+ it "returns all specified fields" do
65
+ @response.keys.sort_by(&:to_s).should eql([:first_name, :age].sort_by(&:to_s))
66
+ end
67
+
68
+ it "returns the correct values for the specified fields" do
69
+ @response.values.sort_by(&:to_s).should eql([@luke.first_name, @luke.age].sort_by(&:to_s))
70
+ end
71
+
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,33 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe ActsAsApi::Base do
4
+
5
+ describe "calling a method in the api template", :orm => :active_record do
6
+
7
+ before(:each) do
8
+ setup_models
9
+ @response = @luke.as_api_response(:only_full_name)
10
+ end
11
+
12
+ after(:each) do
13
+ clean_up
14
+ end
15
+
16
+ it "returns a hash" do
17
+ @response.should be_kind_of(Hash)
18
+ end
19
+
20
+ it "returns the correct number of fields" do
21
+ @response.should have(1).key
22
+ end
23
+
24
+ it "returns all specified fields by name" do
25
+ @response.keys.should include(:full_name)
26
+ end
27
+
28
+ it "returns the correct values for the specified fields" do
29
+ @response.values.should include(@luke.full_name)
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,63 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe ActsAsApi::Base do
4
+
5
+ describe "renaming", :orm => :active_record do
6
+
7
+ before(:each) do
8
+ setup_models
9
+ end
10
+
11
+ after(:each) do
12
+ clean_up
13
+ end
14
+
15
+ describe "an attribute in the api template" do
16
+
17
+ before(:each) do
18
+ @response = @luke.as_api_response(:rename_last_name)
19
+ end
20
+
21
+ it "returns a hash" do
22
+ @response.should be_kind_of(Hash)
23
+ end
24
+
25
+ it "returns the correct number of fields" do
26
+ @response.should have(1).key
27
+ end
28
+
29
+ it "returns all specified fields" do
30
+ @response.keys.should include(:family_name)
31
+ end
32
+
33
+ it "returns the correct values for the specified fields" do
34
+ @response.values.should include(@luke.last_name)
35
+ end
36
+
37
+ end
38
+
39
+ describe "the node/key of a method in the api template" do
40
+
41
+ before(:each) do
42
+ @response = @luke.as_api_response(:rename_full_name)
43
+ end
44
+
45
+ it "returns a hash" do
46
+ @response.should be_kind_of(Hash)
47
+ end
48
+
49
+ it "returns the correct number of fields" do
50
+ @response.should have(1).key
51
+ end
52
+
53
+ it "returns all specified fields" do
54
+ @response.keys.should include(:other_full_name)
55
+ end
56
+
57
+ it "returns the correct values for the specified fields" do
58
+ @response.values.should include(@luke.full_name)
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,33 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe ActsAsApi::Base do
4
+
5
+ describe "listing attributes in the api template", :orm => :active_record do
6
+
7
+ before(:each) do
8
+ setup_models
9
+ @response = @luke.as_api_response(:name_only)
10
+ end
11
+
12
+ after(:each) do
13
+ clean_up
14
+ end
15
+
16
+ it "returns a hash" do
17
+ @response.should be_kind_of(Hash)
18
+ end
19
+
20
+ it "returns the correct number of fields" do
21
+ @response.should have(2).keys
22
+ end
23
+
24
+ it "returns the specified fields only" do
25
+ @response.keys.should include(:first_name, :last_name)
26
+ end
27
+
28
+ it "the specified fields have the correct value" do
29
+ @response.values.should include(@luke.first_name, @luke.last_name)
30
+ end
31
+
32
+ end
33
+ end