restrack 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -39,8 +39,21 @@ module RESTRack
39
39
  # all internal methods are protected rather than private so that calling methods *could* be overriden if necessary.
40
40
  protected
41
41
 
42
+ # If called with an array of strings this will return a datastructure which will
43
+ # automatically be converted by RESTRack into the error format expected by ActiveResource.
44
+ # ActiveResource expects attribute input errors to be responded to with a status
45
+ # code of 422, which is a non-standard HTTP code. Use this to produce the required
46
+ # format of "<errors><error>...</error><error>...</error></errors>" for the response XML.
47
+ # This method also will accept any simple data structure when not worried about AR integration.
48
+ def package_errors(errors)
49
+ if errors.is_a? Array and errors.count{|e| e.is_a? String} === errors.length
50
+ # I am AR bound
51
+ errors = ARFormattedError.new(errors)
52
+ end
53
+ @output = @resource_request.package(errors)[0] # TODO: Remove this if change line 97 in resource_request.rb
54
+ end
42
55
  def package_error(error)
43
- @output = @resource_request.package(error)[0] # TODO: Remove this if change line 97 in resource_request.rb
56
+ package_errors(error)
44
57
  end
45
58
 
46
59
  # This method allows one to access a related resource, without providing a direct link to specific relation(s).
@@ -59,6 +59,17 @@ class Object
59
59
  end
60
60
  end
61
61
 
62
+ class ARFormattedError < Array
63
+ # provide this method, as if it is present it will be used to render the xml rather than XmlSimple
64
+ def to_xml
65
+ str = '<?xml version="1.0" encoding="UTF-8"?><errors>'
66
+ self.each do |error|
67
+ str << "<error>#{error}</error>"
68
+ end
69
+ str << "</errors>"
70
+ end
71
+ end
72
+
62
73
  # We will support ".text" as an extension
63
74
  MIME::Types['text/plain'][0].extensions << 'text'
64
75
  MIME::Types.index_extensions( MIME::Types['text/plain'][0] )
@@ -1,3 +1,3 @@
1
1
  module RESTRack
2
- VERSION = "1.2.2"
2
+ VERSION = "1.2.3"
3
3
  end
@@ -43,6 +43,10 @@ class SampleApp::ErrorsController < RESTRack::ResourceController
43
43
  raise HTTP422ResourceInvalid, package_error({:message => 'This is a WebDAV HTTP extension code used by ActiveResource to communicate validation errors, rather than 400.'})
44
44
  end
45
45
 
46
+ def resource_invalid_active_resource_format
47
+ raise HTTP422ResourceInvalid, package_errors(['This is how ActiveResource expects errors to come through.','It has support for multiple errors.'])
48
+ end
49
+
46
50
  def server_error
47
51
  raise HTTP500ServerError, package_error('tester')
48
52
  end
@@ -119,6 +119,31 @@ class SampleApp::TestControllerActions < Test::Unit::TestCase
119
119
  assert JSON.parse(output[2][0]).has_key?('message')
120
120
  end
121
121
 
122
+ def test_resource_invalid_active_record_format
123
+ response_code = 422
124
+ env = Rack::MockRequest.env_for('/errors/resource_invalid_active_resource_format.xml', {
125
+ :method => 'GET'
126
+ })
127
+ output = ''
128
+ assert_nothing_raised do
129
+ output = @ws.call(env)
130
+ end
131
+ assert_equal response_code, output[0]
132
+ assert_equal '<?xml version="1.0" encoding="UTF-8"?><errors><error>This is how ActiveResource expects errors to come through.</error><error>It has support for multiple errors.</error></errors>', output[2][0]
133
+
134
+ response_code = 422
135
+ env = Rack::MockRequest.env_for('/errors/resource_invalid_active_resource_format', {
136
+ :method => 'GET'
137
+ })
138
+ output = ''
139
+ assert_nothing_raised do
140
+ output = @ws.call(env)
141
+ end
142
+ assert_equal response_code, output[0]
143
+ assert_equal 'This is how ActiveResource expects errors to come through.', JSON.parse(output[2][0])[0]
144
+ assert_equal 'It has support for multiple errors.', JSON.parse(output[2][0])[1]
145
+ end
146
+
122
147
  def test_server_error
123
148
  response_code = 500
124
149
  # This will/should spam the log
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restrack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-07 00:00:00.000000000Z
12
+ date: 2011-10-10 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
16
- requirement: &7009360 !ruby/object:Gem::Requirement
16
+ requirement: &10448680 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *7009360
24
+ version_requirements: *10448680
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rack-test
27
- requirement: &7008920 !ruby/object:Gem::Requirement
27
+ requirement: &10447720 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *7008920
35
+ version_requirements: *10447720
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: i18n
38
- requirement: &7008420 !ruby/object:Gem::Requirement
38
+ requirement: &10446540 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *7008420
46
+ version_requirements: *10446540
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: json
49
- requirement: &7007960 !ruby/object:Gem::Requirement
49
+ requirement: &10445940 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *7007960
57
+ version_requirements: *10445940
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: xml-simple
60
- requirement: &7007360 !ruby/object:Gem::Requirement
60
+ requirement: &10445300 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.0.13
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *7007360
68
+ version_requirements: *10445300
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: builder
71
- requirement: &7006900 !ruby/object:Gem::Requirement
71
+ requirement: &10444560 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *7006900
79
+ version_requirements: *10444560
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: activesupport
82
- requirement: &7006240 !ruby/object:Gem::Requirement
82
+ requirement: &10439600 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *7006240
90
+ version_requirements: *10439600
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: mime-types
93
- requirement: &7005740 !ruby/object:Gem::Requirement
93
+ requirement: &10438800 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: '0'
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *7005740
101
+ version_requirements: *10438800
102
102
  description: ! "\nRESTRack is a Rack-based MVC framework that makes it extremely easy
103
103
  to develop RESTful data services. It is inspired by\nRails, and follows a few of
104
104
  its conventions. But it has no routes file, routing relationships are done through\nsupplying