wrest 0.0.8-java → 0.0.9-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. data/README.rdoc +24 -17
  2. data/Rakefile +6 -6
  3. data/VERSION.yml +1 -1
  4. data/examples/delicious.rb +7 -3
  5. data/examples/redirection.rb +27 -0
  6. data/examples/twitter.rb +78 -0
  7. data/{spec/wrest/http/request_spec.rb → examples/twitter_public_timeline.rb} +15 -12
  8. data/examples/wow_realm_status.rb +7 -3
  9. data/lib/wrest/components/attributes_container.rb +23 -5
  10. data/lib/wrest/components/attributes_container/alias_accessors.rb +66 -0
  11. data/lib/wrest/components/attributes_container/typecaster.rb +44 -38
  12. data/lib/wrest/components/mutators/base.rb +1 -1
  13. data/lib/wrest/components/translators.rb +1 -1
  14. data/lib/wrest/core_ext/string/conversions.rb +1 -1
  15. data/lib/wrest/exceptions.rb +14 -2
  16. data/lib/wrest/http.rb +1 -0
  17. data/lib/wrest/http/get.rb +2 -0
  18. data/lib/wrest/http/redirection.rb +35 -0
  19. data/lib/wrest/http/request.rb +37 -13
  20. data/lib/wrest/http/response.rb +17 -0
  21. data/lib/wrest/resource/base.rb +1 -2
  22. data/lib/wrest/uri.rb +2 -0
  23. data/lib/wrest/version.rb +1 -1
  24. data/spec/functional/sample_rails_app/README +3 -0
  25. data/spec/functional/sample_rails_app/Rakefile +10 -0
  26. data/spec/functional/sample_rails_app/app/controllers/application_controller.rb +10 -0
  27. data/spec/functional/sample_rails_app/app/controllers/lead_bottles_controller.rb +7 -0
  28. data/spec/functional/sample_rails_app/app/helpers/application_helper.rb +3 -0
  29. data/spec/functional/sample_rails_app/app/models/bottle.rb +3 -0
  30. data/spec/functional/sample_rails_app/app/models/glass_bottle.rb +3 -0
  31. data/spec/functional/sample_rails_app/app/models/lead_bottle.rb +3 -0
  32. data/spec/functional/sample_rails_app/config/boot.rb +110 -0
  33. data/spec/functional/sample_rails_app/config/database.yml +16 -0
  34. data/spec/functional/sample_rails_app/config/environment.rb +42 -0
  35. data/spec/functional/sample_rails_app/config/environments/development.rb +17 -0
  36. data/spec/functional/sample_rails_app/config/environments/production.rb +28 -0
  37. data/spec/functional/sample_rails_app/config/environments/test.rb +28 -0
  38. data/spec/functional/sample_rails_app/config/initializers/backtrace_silencers.rb +7 -0
  39. data/spec/functional/sample_rails_app/config/initializers/inflections.rb +10 -0
  40. data/spec/functional/sample_rails_app/config/initializers/mime_types.rb +5 -0
  41. data/spec/functional/sample_rails_app/config/initializers/new_rails_defaults.rb +19 -0
  42. data/spec/functional/sample_rails_app/config/initializers/session_store.rb +15 -0
  43. data/spec/functional/sample_rails_app/config/locales/en.yml +5 -0
  44. data/spec/functional/sample_rails_app/config/routes.rb +3 -0
  45. data/spec/functional/sample_rails_app/db/development.sqlite3 +0 -0
  46. data/spec/functional/sample_rails_app/db/migrate/20090319115628_create_bottle.rb +13 -0
  47. data/spec/functional/sample_rails_app/db/schema.rb +20 -0
  48. data/spec/functional/sample_rails_app/db/test.sqlite3 +0 -0
  49. data/spec/functional/sample_rails_app/log/development.log +1 -0
  50. data/spec/functional/sample_rails_app/public/404.html +30 -0
  51. data/spec/functional/sample_rails_app/public/422.html +30 -0
  52. data/spec/functional/sample_rails_app/public/500.html +30 -0
  53. data/spec/functional/sample_rails_app/public/favicon.ico +0 -0
  54. data/spec/functional/sample_rails_app/public/images/rails.png +0 -0
  55. data/spec/functional/sample_rails_app/public/index.html +275 -0
  56. data/spec/functional/sample_rails_app/public/robots.txt +5 -0
  57. data/spec/functional/sample_rails_app/script/about +4 -0
  58. data/spec/functional/sample_rails_app/script/autospec +6 -0
  59. data/spec/functional/sample_rails_app/script/console +3 -0
  60. data/spec/functional/sample_rails_app/script/dbconsole +3 -0
  61. data/spec/functional/sample_rails_app/script/destroy +3 -0
  62. data/spec/functional/sample_rails_app/script/generate +3 -0
  63. data/spec/functional/sample_rails_app/script/performance/benchmarker +3 -0
  64. data/spec/functional/sample_rails_app/script/performance/profiler +3 -0
  65. data/spec/functional/sample_rails_app/script/plugin +3 -0
  66. data/spec/functional/sample_rails_app/script/runner +3 -0
  67. data/spec/functional/sample_rails_app/script/server +3 -0
  68. data/spec/functional/sample_rails_app/script/spec +10 -0
  69. data/spec/functional/sample_rails_app/script/spec_server +9 -0
  70. data/spec/functional/sample_rails_app/test/performance/browsing_test.rb +9 -0
  71. data/spec/functional/sample_rails_app/test/test_helper.rb +38 -0
  72. data/spec/functional/sample_rails_app/tmtags +2559 -0
  73. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/MIT-LICENSE +20 -0
  74. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/README.rdoc +100 -0
  75. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/Rakefile +18 -0
  76. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/init.rb +5 -0
  77. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/install.rb +1 -0
  78. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full.rb +14 -0
  79. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/base.rb +140 -0
  80. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/resources.rb +16 -0
  81. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/resources_controller.rb +26 -0
  82. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/routes_controller.rb +16 -0
  83. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/api.rb +26 -0
  84. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/exception.rb +23 -0
  85. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/from_json.rb +15 -0
  86. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/dispatch.rb +235 -0
  87. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/models/resourced_route.rb +84 -0
  88. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/query.rb +337 -0
  89. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render.rb +63 -0
  90. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/html.rb +50 -0
  91. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/json.rb +75 -0
  92. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/xml.rb +65 -0
  93. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/retrieve.rb +74 -0
  94. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/version.rb +9 -0
  95. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/base_spec.rb +88 -0
  96. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/controllers/resources_spec.rb +29 -0
  97. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/dispatch_spec.rb +262 -0
  98. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/models/resourced_route_spec.rb +62 -0
  99. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/query/parameter_spec.rb +57 -0
  100. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/query_spec.rb +462 -0
  101. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/html_spec.rb +4 -0
  102. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/json_spec.rb +107 -0
  103. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/xml_spec.rb +98 -0
  104. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render_spec.rb +5 -0
  105. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/retrieve_spec.rb +173 -0
  106. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/spec_helper.rb +98 -0
  107. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/uninstall.rb +1 -0
  108. data/spec/functional/spec_helper.rb +0 -0
  109. data/spec/{spec_helper.rb → unit/spec_helper.rb} +2 -2
  110. data/spec/unit/wrest/components/attributes_container/alias_accessors_spec.rb +49 -0
  111. data/spec/{wrest → unit/wrest}/components/attributes_container/typecaster_spec.rb +28 -8
  112. data/spec/{wrest → unit/wrest}/components/attributes_container_spec.rb +42 -15
  113. data/spec/{wrest → unit/wrest}/components/mutators/base_spec.rb +1 -1
  114. data/spec/{wrest → unit/wrest}/components/mutators/camel_to_snake_spec.rb +0 -0
  115. data/spec/{wrest → unit/wrest}/components/mutators/xml_mini_type_caster_spec.rb +0 -0
  116. data/spec/{wrest → unit/wrest}/components/mutators/xml_simple_type_caster_spec.rb +0 -0
  117. data/spec/{wrest → unit/wrest}/components/mutators_spec.rb +0 -0
  118. data/spec/{wrest → unit/wrest}/components/translators/xml_spec.rb +0 -0
  119. data/spec/{wrest → unit/wrest}/components/translators_spec.rb +1 -1
  120. data/spec/{wrest → unit/wrest}/core_ext/hash/conversions_spec.rb +0 -0
  121. data/spec/{wrest → unit/wrest}/core_ext/string/conversions_spec.rb +14 -0
  122. data/spec/unit/wrest/http/redirection_spec.rb +42 -0
  123. data/spec/unit/wrest/http/request_spec.rb +70 -0
  124. data/spec/unit/wrest/http/response_spec.rb +45 -0
  125. data/spec/{wrest → unit/wrest}/resource/base_spec.rb +5 -4
  126. data/spec/{wrest → unit/wrest}/uri_spec.rb +68 -67
  127. data/spec/{wrest → unit/wrest}/uri_template_spec.rb +0 -0
  128. metadata +188 -39
  129. data/lib/wrest/exceptions/method_not_overridden_exception.rb +0 -17
  130. data/lib/wrest/exceptions/unsupported_content_type_exception.rb +0 -17
  131. data/spec/wrest/http/response_spec.rb +0 -21
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Brian Guthrie
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,100 @@
1
+ = ResourceFull 0.7.5
2
+
3
+ * http://github.com/bguthrie/resource_full/
4
+
5
+ == DESCRIPTION
6
+
7
+ ResourceFull integrated with ActionController to provide a comprehensive
8
+ RESTful resource modeling and querying framework. It provides parameter
9
+ queryability, paging, sorting, separation of controller concerns, multiple
10
+ formats (HTML, XML, JSON), CRUD access permissions, and API metadata
11
+ surrounding the resource itself. It's opinionated but is intended to provide
12
+ you with as much as possible without limiting your ability to customize its
13
+ behavior.
14
+
15
+ == GOALS
16
+
17
+ The major distinguishing features of ResourceFull are:
18
+
19
+ * Queryability: the ability to designate certain parameters as queryable,
20
+ and map them to columns and SQL queries in the underlying model.
21
+ These queries chain together multiple named or unnamed scopes and can be
22
+ used either for a SQL SELECT or SELECT COUNT.
23
+ This functionality may be moved into a separate plugin in the future.
24
+ * Pagination and orderability: it automatically responds to requests for
25
+ limit, offset, order_by, and order_dir.
26
+ * Implementation: Default implementations for HTML, XML, and JSON controller requests.
27
+ * API documentation: ResourceFull-enabled Rails apps are able to provide automatic
28
+ documentation of the resources they expose, up to a point. (This is enabled
29
+ in large part by the queryability functionality and other resource-level
30
+ descriptors.) It's my hope that this can eventually be consumed by a Rails
31
+ resource registrar that acts as the single source of record for multiple REST
32
+ engines within an organization.
33
+
34
+ == EXAMPLE
35
+
36
+ class UsersController < ResourceFull::Base
37
+ identified_by :username, :unless => lambda { |id| id =~ /^[0-9]+$/ }
38
+
39
+ queryable_with :city, :state, :from => :address
40
+ queryable_with :name, :columns => [:first_name, :last_name]
41
+ queryable_with :email_address, :fuzzy => true
42
+ queryable_with :is_active, :scope => :active
43
+
44
+ orderable_by :city, :from => :address
45
+
46
+ responds_to :html
47
+ responds_to :xml, :only => [:read, :update]
48
+ end
49
+
50
+ class AddressesController < ResourceFull::Base
51
+ nests_within :users
52
+ queryable_with :city, :state
53
+ end
54
+
55
+ This allows for the following:
56
+
57
+ /users/bguthrie.xml
58
+ /users?name=Guthrie
59
+ /users?email_address=gmail
60
+ /users.xml?city=Chicago&name=Brian,Paul,Alicia&order_by=city&order_dir=asc
61
+ /users.xml?limit=10&offset=30
62
+ /users/bguthrie/addresses
63
+
64
+ >> UsersController.to_xml
65
+ => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<resource>\n <parameters type=\"array\"/>..."
66
+
67
+ == API DOCUMENTATION
68
+
69
+ To enable ResourceFull to document your resources at /resources.xml and /resources/<name>/routes.xml,
70
+ add +map.api+ to your +routes.rb+ file.
71
+
72
+ == CONTRIBUTORS
73
+
74
+ * {Vijay Aravamudhan}[http://github.com/vraravam]
75
+ * {Dirk Elmendorf}[http://github.com/economysizegeek]
76
+
77
+ == LICENSE
78
+
79
+ (The MIT License)
80
+
81
+ Copyright (c) 2009 Brian Guthrie
82
+
83
+ Permission is hereby granted, free of charge, to any person obtaining
84
+ a copy of this software and associated documentation files (the
85
+ 'Software'), to deal in the Software without restriction, including
86
+ without limitation the rights to use, copy, modify, merge, publish,
87
+ distribute, sublicense, and/or sell copies of the Software, and to
88
+ permit persons to whom the Software is furnished to do so, subject to
89
+ the following conditions:
90
+
91
+ The above copyright notice and this permission notice shall be
92
+ included in all copies or substantial portions of the Software.
93
+
94
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
95
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
96
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
97
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
98
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
99
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
100
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ require File.dirname(__FILE__) + '/../../../config/environment'
2
+
3
+ require 'rake'
4
+ require 'spec/rake/spectask'
5
+ require 'rcov/rcovtask'
6
+
7
+ task :default => :spec
8
+
9
+ Spec::Rake::SpecTask.new do |t|
10
+ end
11
+
12
+ require 'rcov/version'
13
+
14
+ Rcov::RcovTask.new do |t|
15
+ t.pattern = "spec/**/_spec.rb"
16
+ t.rcov_opts = [ "--spec-only" ]
17
+ t.output_dir = "coverage"
18
+ end
@@ -0,0 +1,5 @@
1
+ if ([Rails::VERSION::MAJOR, Rails::VERSION::MINOR] <=> [2,1]) >= 0 # if the rails version is 2.1 or greater...Í
2
+ ActiveRecord::Base.include_root_in_json = true
3
+ end
4
+
5
+ require 'resource_full'
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,14 @@
1
+ # Core library
2
+ require 'resource_full/core_extensions/exception'
3
+ require 'resource_full/core_extensions/from_json'
4
+ require 'resource_full/dispatch'
5
+ require 'resource_full/query'
6
+ require 'resource_full/render'
7
+ require 'resource_full/retrieve'
8
+ require 'resource_full/version'
9
+ require 'resource_full/base'
10
+
11
+ # REST API
12
+ require 'resource_full/controllers/resources_controller'
13
+ require 'resource_full/controllers/routes_controller'
14
+ require 'resource_full/core_extensions/api'
@@ -0,0 +1,140 @@
1
+ module ResourceFull
2
+ class ResourceNotFound < Exception; end
3
+
4
+ class Base < ActionController::Base
5
+ unless Rails.version == "2.3.2"
6
+ session :off, :if => lambda { |request| request.format.xml? || request.format.json? }
7
+ end
8
+
9
+ def model_name; self.class.model_name; end
10
+ def model_class; self.class.model_class; end
11
+
12
+ class << self
13
+ # Returns the list of all resources handled by ResourceFull.
14
+ def all_resources
15
+ ActionController::Routing.possible_controllers.map do |possible_controller|
16
+ controller_for(possible_controller)
17
+ end.select do |controller_class|
18
+ controller_class.ancestors.include?(self)
19
+ end
20
+ end
21
+
22
+ # Returns the controller for the given resource.
23
+ def controller_for(resource)
24
+ return resource if resource.is_a?(Class) && resource.ancestors.include?(ActionController::Base)
25
+ "#{resource.to_s.underscore}_controller".classify.constantize
26
+ rescue NameError
27
+ raise ResourceFull::ResourceNotFound, "not found: #{resource}"
28
+ end
29
+
30
+ private
31
+
32
+ def inherited(controller)
33
+ super(controller)
34
+ controller.send :extend, ClassMethods
35
+ controller.send :include,
36
+ ResourceFull::Retrieve,
37
+ ResourceFull::Query,
38
+ ResourceFull::Dispatch,
39
+ ResourceFull::Render
40
+ controller.send :alias_retrieval_methods!
41
+ end
42
+ end
43
+ end
44
+
45
+ module ClassMethods
46
+ attr_accessor_with_default :paginatable, true
47
+ attr_accessor_with_default :resource_identifier, :id
48
+
49
+ # Returns true if this resource is paginatable, which is to say, it recognizes and honors
50
+ # the :limit and :offset parameters if present in a query. True by default.
51
+ def paginatable?; paginatable; end
52
+
53
+ # The name of the model exposed by this resource. Derived from the name of the controller
54
+ # by default. See +exposes+.
55
+ def model_name
56
+ @model_class ? @model_class.to_s.underscore : self.controller_name.singularize
57
+ end
58
+
59
+ # Indicates that this resource is identified by a database column other than the default
60
+ # :id.
61
+ # TODO This should honor the model's primary key column but needn't be bound by it.
62
+ # TODO Refactor this.
63
+ # TODO Improve the documentation.
64
+ def identified_by(*args, &block)
65
+ opts = args.extract_options!
66
+ column = args.first
67
+ if !block.nil?
68
+ self.resource_identifier = block
69
+ elsif !column.nil?
70
+ if !opts.empty? && ( opts.has_key?(:if) || opts.has_key?(:unless) )
71
+ if opts[:unless] == :id_numeric
72
+ opts[:unless] = lambda { |id| id =~ /^[0-9]+$/ }
73
+ end
74
+
75
+ # Negate the condition to generate an :if from an :unless.
76
+ condition = opts[:if] || lambda { |id| not opts[:unless].call(id) }
77
+
78
+ self.resource_identifier = lambda do |id|
79
+ if condition.call(id)
80
+ column
81
+ else :id end
82
+ end
83
+ else
84
+ self.resource_identifier = column
85
+ end
86
+ else
87
+ raise ArgumentError, "identified_by expects either a block or a column name and some options"
88
+ end
89
+ end
90
+
91
+ # The class of the model exposed by this resource. Derived from the model name. See +exposes+.
92
+ def model_class
93
+ @model_class ||= model_name.camelize.constantize
94
+ end
95
+
96
+ # Indicates that the CRUD methods should be called on the given class. Accepts
97
+ # either a class object or the name of the desired model.
98
+ def exposes(model_class)
99
+ remove_retrieval_methods!
100
+ @model_class = model_class.to_s.singularize.camelize.constantize
101
+ alias_retrieval_methods!
102
+ end
103
+
104
+ # Renders the resource as XML.
105
+ def to_xml(opts={})
106
+ { :name => self.controller_name,
107
+ :parameters => self.queryable_params,
108
+ :identifier => self.xml_identifier
109
+ }.to_xml(opts.merge(:root => "resource"))
110
+ end
111
+
112
+ protected
113
+
114
+ def xml_identifier
115
+ (self.resource_identifier.is_a?(Proc) ? self.resource_identifier.call(nil) : self.resource_identifier).to_s
116
+ end
117
+
118
+ private
119
+
120
+ def alias_retrieval_methods!
121
+ define_method("new_#{model_name}") { new_model_object }
122
+ define_method("find_#{model_name}") { find_model_object }
123
+ define_method("create_#{model_name}") { create_model_object }
124
+ define_method("update_#{model_name}") { update_model_object }
125
+ define_method("destroy_#{model_name}") { destroy_model_object }
126
+ define_method("find_all_#{model_name.pluralize}") { find_all_model_objects }
127
+ define_method("count_all_#{model_name.pluralize}") { count_all_model_objects }
128
+ end
129
+
130
+ def remove_retrieval_methods!
131
+ remove_method "new_#{model_name}"
132
+ remove_method "find_#{model_name}"
133
+ remove_method "create_#{model_name}"
134
+ remove_method "update_#{model_name}"
135
+ remove_method "destroy_#{model_name}"
136
+ remove_method "find_all_#{model_name.pluralize}"
137
+ remove_method "count_all_#{model_name.pluralize}"
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,16 @@
1
+ module ResourceFull
2
+ module Controllers
3
+ class ResourcesController < ResourceFull::Base
4
+ responds_to :xml
5
+
6
+ protected
7
+ def find_all_resources
8
+ ResourceFull::Base.all_resources
9
+ end
10
+
11
+ def find_resource
12
+ ResourceFull::Base.controller_for(params[:id].pluralize)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,26 @@
1
+ module ResourceFull
2
+ module Controllers
3
+ class ResourcesController < ResourceFull::Base
4
+ responds_to :xml, :only => [ :read ]
5
+
6
+ def index_xml
7
+ render :xml => find_all_resources.to_xml(:root => "resources")
8
+ end
9
+
10
+ def show_xml
11
+ render :xml => find_resource.to_xml
12
+ rescue ResourceFull::ResourceNotFound => e
13
+ render :xml => e.to_xml, :status => :not_found
14
+ end
15
+
16
+ protected
17
+ def find_all_resources
18
+ ResourceFull::Base.all_resources
19
+ end
20
+
21
+ def find_resource
22
+ ResourceFull::Base.controller_for(params[:id])
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ module ResourceFull
2
+ module Controllers
3
+ class RoutesController < ResourceFull::Base
4
+ exposes ResourceFull::Models::ResourcedRoute
5
+ responds_to :xml, :only => [ :read ]
6
+
7
+ def index_xml
8
+ render :xml => find_all_routes.to_xml(:root => "routes")
9
+ end
10
+
11
+ def find_all_routes
12
+ ResourceFull::Models::ResourcedRoute.find :all, params
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,26 @@
1
+ module ResourceFull
2
+ module CoreExtensions
3
+ module API
4
+
5
+ # Generate ActionController routes for RESTful services documentation.
6
+ def api
7
+ with_options :format => 'xml', :conditions => { :method => :get } do |map|
8
+ map.named_route 'resource', '/resources/:id.xml',
9
+ :action => 'show', :controller => 'resource_full/controllers/resources'
10
+ map.named_route 'resources', '/resources.xml',
11
+ :action => 'index', :controller => 'resource_full/controllers/resources'
12
+ map.named_route 'resources_route', '/resources/:resource_id/routes/:id.xml',
13
+ :action => 'show', :controller => 'resource_full/controllers/routes'
14
+ map.named_route 'resources_routes', '/resources/:resource_id/routes.xml',
15
+ :action => 'index', :controller => 'resource_full/controllers/routes'
16
+ map.named_route 'routes', '/routes.xml',
17
+ :action => 'index', :controller => 'resource_full/controllers/routes'
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ class ActionController::Routing::RouteSet::Mapper
25
+ include ResourceFull::CoreExtensions::API
26
+ end
@@ -0,0 +1,23 @@
1
+ module ResourceFull
2
+ module CoreExtensions
3
+ module Exception
4
+ def to_xml(opts={})
5
+ xml = opts[:builder] || Builder::XmlMarkup.new
6
+
7
+ xml.errors {
8
+ xml.error "#{self.class}: #{self.to_s}"
9
+ xml.error self.backtrace
10
+ }
11
+ end
12
+
13
+ def to_json(opts={})
14
+ {"error" => {:text => "#{self.class}: #{self.to_s}",
15
+ :backtrace => self.backtrace}}.to_json
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ class Exception
22
+ include ResourceFull::CoreExtensions::Exception
23
+ end
@@ -0,0 +1,15 @@
1
+ module ResourceFull
2
+ module CoreExtensions
3
+ module Hash
4
+ def from_json(json)
5
+ ActiveSupport::JSON.decode json
6
+ end
7
+ end
8
+ end
9
+ end
10
+
11
+ class Hash
12
+ class << self
13
+ include ResourceFull::CoreExtensions::Hash
14
+ end
15
+ end
@@ -0,0 +1,235 @@
1
+ module ResourceFull
2
+ module Dispatch
3
+ class << self
4
+ def included(controller)
5
+ super(controller)
6
+ controller.send :extend, ClassMethods
7
+ controller.before_filter :ensure_sets_format_when_ie7
8
+ controller.before_filter :ensure_responds_to_format
9
+ controller.before_filter :ensure_responds_to_method
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+ DEFAULT_FORMATS = [ :xml, :html, :json ]
15
+
16
+ CRUD_METHODS_TO_ACTIONS = {
17
+ :create => [ :create, :new ],
18
+ :read => [ :show, :index, :count ],
19
+ :update => [ :update, :edit ],
20
+ :delete => [ :destroy ]
21
+ }
22
+
23
+ # Indicates that the controller responds to one of the requested CRUD (create, read,
24
+ # update, delete) methods. These correspond to controller methods in the following
25
+ # manner:
26
+ #
27
+ # * Create: create, new
28
+ # * Read: show, index, count
29
+ # * Update: update, edit
30
+ # * Delete: destroy
31
+ #
32
+ # By default, a format supports all of the above methods, unless you specify otherwise.
33
+ # Override these defaults by using the :only or :except options. For example,
34
+ #
35
+ # responds_to :xml, :only => [:create, :delete]
36
+ #
37
+ # A controller may be reset back to default responds (xml, html, all CRUD methods) by
38
+ # specifying responds_to :defaults.
39
+ def responds_to(*formats)
40
+ if formats.first == :defaults
41
+ @renderable_formats = default_responds
42
+ @renderable_formats_overridden = false
43
+ return
44
+ end
45
+
46
+ opts = formats.extract_options!
47
+
48
+ supported_crud_methods = if opts[:only]
49
+ [ opts[:only] ].flatten
50
+ elsif opts[:except]
51
+ possible_crud_methods - [ opts[:except] ].flatten
52
+ else
53
+ possible_crud_methods
54
+ end
55
+
56
+ unless renderable_formats_already_overridden?
57
+ @renderable_formats = {}
58
+ @renderable_formats_overridden = true
59
+ end
60
+
61
+ formats.each do |format|
62
+ renderable_formats[format] = supported_crud_methods
63
+ end
64
+ end
65
+
66
+ # A list of symbols of all allowed formats (e.g. :xml, :html)
67
+ def allowed_formats
68
+ renderable_formats.keys
69
+ end
70
+
71
+ # A list of symbols of all allowed CRUD methods (e.g. :create, :delete)
72
+ def allowed_methods(format=:html)
73
+ renderable_formats[format] || []
74
+ end
75
+
76
+ # A list of symbols of all allowed controller actions (e.g. :show, :destroy) derived from
77
+ # the allowed CRUD actions.
78
+ def allowed_actions(format=:html)
79
+ renderable_formats[format].sum {|crud_action| CRUD_METHODS_TO_ACTIONS[crud_action]}
80
+ end
81
+
82
+ # A list of all possible CRUD actions that this framework understands, which is to say,
83
+ # the core Rails actions plus +count+ (and perhaps others eventually).
84
+ def possible_actions
85
+ CRUD_METHODS_TO_ACTIONS.values.sum([])
86
+ end
87
+
88
+ # Returns true if the request format is an allowed format.
89
+ def responds_to_request_format?(request)
90
+ allowed_formats.include? extract_request_format(request)
91
+ end
92
+
93
+ # Returns true if the request action is an allowed action as defined by the allowed CRUD methods.
94
+ def responds_to_request_action?(request, action)
95
+ # TODO Consider using ActionController's +verify+ method in preference to this.
96
+ # TODO We don't verify custom methods yet, so ignore them.
97
+ return true unless possible_actions.include?(action.to_sym)
98
+ allowed_actions(extract_request_format(request)).include? action.to_sym
99
+ end
100
+
101
+ protected
102
+
103
+ def renderable_formats
104
+ @renderable_formats ||= default_responds
105
+ end
106
+
107
+ private
108
+
109
+ def possible_crud_methods
110
+ CRUD_METHODS_TO_ACTIONS.keys
111
+ end
112
+
113
+ def extract_request_format(request)
114
+ request.format.html? ? :html : request.format.to_sym
115
+ end
116
+
117
+ def renderable_formats_already_overridden?
118
+ @renderable_formats_overridden
119
+ end
120
+
121
+ def default_responds
122
+ returning({}) do |responses|
123
+ DEFAULT_FORMATS.each do |format|
124
+ responses[format] = CRUD_METHODS_TO_ACTIONS.keys.dup
125
+ end
126
+ end
127
+ end
128
+ end
129
+
130
+ def show
131
+ dispatch_to :show
132
+ end
133
+
134
+ def index
135
+ dispatch_to :index
136
+ end
137
+
138
+ def create
139
+ dispatch_to :create
140
+ end
141
+
142
+ def update
143
+ dispatch_to :update
144
+ end
145
+
146
+ def destroy
147
+ dispatch_to :destroy
148
+ end
149
+
150
+ def new
151
+ dispatch_to :new
152
+ end
153
+
154
+ # Renders the number of objects in the database, in the following form:
155
+ #
156
+ # <count type="integer">34</count>
157
+ #
158
+ # This accepts the same queryable parameters as the index method.
159
+ #
160
+ # N.B. This may be highly specific to my previous experience and may go away
161
+ # in previous releases.
162
+ def count
163
+ xml = Builder::XmlMarkup.new :indent => 2
164
+ xml.instruct!
165
+ render :xml => xml.count(send("count_all_#{model_name.pluralize}"))
166
+ xml = nil
167
+ end
168
+
169
+ def edit
170
+ self.model_object = send("find_#{model_name}")
171
+ end
172
+
173
+ protected
174
+
175
+ def model_object=(object)
176
+ instance_variable_set "@#{model_name}", object
177
+ end
178
+
179
+ def model_object
180
+ instance_variable_get "@#{model_name}"
181
+ end
182
+
183
+ def model_objects=(objects)
184
+ instance_variable_set "@#{model_name.pluralize}", objects
185
+ end
186
+
187
+ def model_objects
188
+ instance_variable_get "@#{model_name.pluralize}"
189
+ end
190
+
191
+ private
192
+
193
+ def ensure_sets_format_when_ie7
194
+ if user_agent_ie7?
195
+ if request_looks_like?('json', 'javascript')
196
+ request.format = 'json'
197
+ elsif request_looks_like?('xml')
198
+ request.format = 'xml'
199
+ else
200
+ request.format = 'html'
201
+ end
202
+ end
203
+ end
204
+
205
+ def user_agent_ie7?
206
+ request.headers["HTTP_USER_AGENT"] =~ /MSIE 7.0/
207
+ end
208
+
209
+ def request_looks_like?(*formats)
210
+ formats.any? do |format|
211
+ request.format.to_s =~ /#{format}/ || request.headers['REQUEST_URI'] =~ /\.#{format}[?]/
212
+ end
213
+ end
214
+
215
+ def ensure_responds_to_format
216
+ unless self.class.responds_to_request_format?(request)
217
+ render :text => "Resource does not have a representation in #{request.format.to_str} format", :status => :not_acceptable
218
+ end
219
+ end
220
+
221
+ def ensure_responds_to_method
222
+ unless self.class.responds_to_request_action?(request, params[:action])
223
+ render :text => "Resource does not allow #{params[:action]} action", :status => :method_not_allowed
224
+ end
225
+ end
226
+
227
+ def dispatch_to(method)
228
+ respond_to do |requested_format|
229
+ self.class.allowed_formats.each do |renderable_format|
230
+ requested_format.send(renderable_format) { send("#{method}_#{renderable_format}") }
231
+ end
232
+ end
233
+ end
234
+ end
235
+ end