wrest 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. data/CHANGELOG +34 -0
  2. data/README.rdoc +16 -10
  3. data/Rakefile +361 -123
  4. data/VERSION.yml +2 -2
  5. data/examples/delicious.rb +17 -7
  6. data/examples/facebook.rb +101 -0
  7. data/examples/keep_alive.rb +37 -0
  8. data/examples/twitter.rb +3 -3
  9. data/examples/twitter_public_timeline.rb +11 -4
  10. data/examples/wow_realm_status.rb +8 -2
  11. data/{spec/functional/sample_rails_app/public/favicon.ico → init.rb} +0 -0
  12. data/lib/wrest/components/{attributes_container → container}/alias_accessors.rb +4 -4
  13. data/lib/wrest/components/{attributes_container → container}/typecaster.rb +1 -1
  14. data/lib/wrest/components/{attributes_container.rb → container.rb} +46 -16
  15. data/lib/wrest/components/mutators.rb +4 -4
  16. data/lib/wrest/components/translators/json.rb +2 -2
  17. data/lib/wrest/components/translators/xml.rb +3 -2
  18. data/lib/wrest/components/translators.rb +3 -3
  19. data/lib/wrest/components.rb +3 -3
  20. data/lib/wrest/core_ext/hash.rb +1 -1
  21. data/lib/wrest/core_ext/string.rb +1 -1
  22. data/lib/wrest/curl/delete.rb +23 -0
  23. data/lib/wrest/curl/get.rb +23 -0
  24. data/lib/wrest/curl/options.rb +16 -0
  25. data/lib/wrest/curl/post.rb +23 -0
  26. data/lib/wrest/curl/put.rb +23 -0
  27. data/lib/wrest/curl/request.rb +95 -0
  28. data/lib/wrest/curl/response.rb +63 -0
  29. data/lib/wrest/curl/session.rb +57 -0
  30. data/lib/wrest/curl.rb +49 -0
  31. data/lib/wrest/exceptions.rb +16 -1
  32. data/lib/wrest/http_shared/headers.rb +350 -0
  33. data/lib/wrest/http_shared/standard_headers.rb +21 -0
  34. data/lib/wrest/http_shared/standard_tokens.rb +18 -0
  35. data/lib/wrest/http_shared.rb +24 -0
  36. data/lib/wrest/native/connection_factory.rb +23 -0
  37. data/lib/wrest/{http → native}/delete.rb +1 -1
  38. data/lib/wrest/{http → native}/get.rb +1 -1
  39. data/lib/wrest/{http → native}/options.rb +1 -1
  40. data/lib/wrest/{http → native}/post.rb +1 -1
  41. data/lib/wrest/{http → native}/put.rb +1 -1
  42. data/lib/wrest/{http → native}/redirection.rb +4 -1
  43. data/lib/wrest/{http → native}/request.rb +32 -20
  44. data/lib/wrest/{http → native}/response.rb +12 -4
  45. data/lib/wrest/native/session.rb +57 -0
  46. data/lib/wrest/native.rb +32 -0
  47. data/lib/wrest/resource/base.rb +1 -1
  48. data/lib/wrest/resource.rb +1 -1
  49. data/lib/wrest/test/request_patches.rb +5 -0
  50. data/lib/wrest/test.rb +1 -0
  51. data/lib/wrest/uri.rb +31 -3
  52. data/lib/wrest/version.rb +2 -2
  53. data/lib/wrest.rb +52 -16
  54. data/spec/unit/spec_helper.rb +12 -3
  55. data/spec/unit/wrest/components/attributes_container/alias_accessors_spec.rb +2 -2
  56. data/spec/unit/wrest/components/attributes_container/typecaster_spec.rb +6 -6
  57. data/spec/unit/wrest/components/attributes_container_spec.rb +44 -12
  58. data/spec/unit/wrest/components/translators/xml_spec.rb +7 -3
  59. data/spec/unit/wrest/curl/request_spec.rb +19 -0
  60. data/spec/unit/wrest/curl/response_spec.rb +16 -0
  61. data/spec/unit/wrest/http/response_spec.rb +17 -38
  62. data/spec/unit/wrest/{http → native}/redirection_spec.rb +5 -5
  63. data/spec/unit/wrest/{http → native}/request_spec.rb +15 -14
  64. data/spec/unit/wrest/native/response_spec.rb +72 -0
  65. data/spec/unit/wrest/native/session_spec.rb +74 -0
  66. data/spec/unit/wrest/resource/base_spec.rb +2 -2
  67. data/spec/unit/wrest/uri_spec.rb +51 -11
  68. data/wrest.gemspec +164 -0
  69. metadata +51 -165
  70. data/lib/wrest/http.rb +0 -25
  71. data/spec/functional/sample_rails_app/README +0 -3
  72. data/spec/functional/sample_rails_app/Rakefile +0 -10
  73. data/spec/functional/sample_rails_app/app/controllers/application_controller.rb +0 -10
  74. data/spec/functional/sample_rails_app/app/controllers/lead_bottles_controller.rb +0 -7
  75. data/spec/functional/sample_rails_app/app/helpers/application_helper.rb +0 -3
  76. data/spec/functional/sample_rails_app/app/models/bottle.rb +0 -3
  77. data/spec/functional/sample_rails_app/app/models/glass_bottle.rb +0 -3
  78. data/spec/functional/sample_rails_app/app/models/lead_bottle.rb +0 -3
  79. data/spec/functional/sample_rails_app/config/boot.rb +0 -110
  80. data/spec/functional/sample_rails_app/config/database.yml +0 -16
  81. data/spec/functional/sample_rails_app/config/environment.rb +0 -42
  82. data/spec/functional/sample_rails_app/config/environments/development.rb +0 -17
  83. data/spec/functional/sample_rails_app/config/environments/production.rb +0 -28
  84. data/spec/functional/sample_rails_app/config/environments/test.rb +0 -28
  85. data/spec/functional/sample_rails_app/config/initializers/backtrace_silencers.rb +0 -7
  86. data/spec/functional/sample_rails_app/config/initializers/inflections.rb +0 -10
  87. data/spec/functional/sample_rails_app/config/initializers/mime_types.rb +0 -5
  88. data/spec/functional/sample_rails_app/config/initializers/new_rails_defaults.rb +0 -19
  89. data/spec/functional/sample_rails_app/config/initializers/session_store.rb +0 -15
  90. data/spec/functional/sample_rails_app/config/locales/en.yml +0 -5
  91. data/spec/functional/sample_rails_app/config/routes.rb +0 -3
  92. data/spec/functional/sample_rails_app/db/development.sqlite3 +0 -0
  93. data/spec/functional/sample_rails_app/db/migrate/20090319115628_create_bottle.rb +0 -13
  94. data/spec/functional/sample_rails_app/db/schema.rb +0 -20
  95. data/spec/functional/sample_rails_app/db/test.sqlite3 +0 -0
  96. data/spec/functional/sample_rails_app/log/development.log +0 -1
  97. data/spec/functional/sample_rails_app/public/404.html +0 -30
  98. data/spec/functional/sample_rails_app/public/422.html +0 -30
  99. data/spec/functional/sample_rails_app/public/500.html +0 -30
  100. data/spec/functional/sample_rails_app/public/images/rails.png +0 -0
  101. data/spec/functional/sample_rails_app/public/index.html +0 -275
  102. data/spec/functional/sample_rails_app/public/robots.txt +0 -5
  103. data/spec/functional/sample_rails_app/script/about +0 -4
  104. data/spec/functional/sample_rails_app/script/autospec +0 -6
  105. data/spec/functional/sample_rails_app/script/console +0 -3
  106. data/spec/functional/sample_rails_app/script/dbconsole +0 -3
  107. data/spec/functional/sample_rails_app/script/destroy +0 -3
  108. data/spec/functional/sample_rails_app/script/generate +0 -3
  109. data/spec/functional/sample_rails_app/script/performance/benchmarker +0 -3
  110. data/spec/functional/sample_rails_app/script/performance/profiler +0 -3
  111. data/spec/functional/sample_rails_app/script/plugin +0 -3
  112. data/spec/functional/sample_rails_app/script/runner +0 -3
  113. data/spec/functional/sample_rails_app/script/server +0 -3
  114. data/spec/functional/sample_rails_app/script/spec +0 -10
  115. data/spec/functional/sample_rails_app/script/spec_server +0 -9
  116. data/spec/functional/sample_rails_app/test/performance/browsing_test.rb +0 -9
  117. data/spec/functional/sample_rails_app/test/test_helper.rb +0 -38
  118. data/spec/functional/sample_rails_app/tmtags +0 -2559
  119. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/MIT-LICENSE +0 -20
  120. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/README.rdoc +0 -100
  121. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/Rakefile +0 -18
  122. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/init.rb +0 -5
  123. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/install.rb +0 -1
  124. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/base.rb +0 -140
  125. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/resources.rb +0 -16
  126. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/resources_controller.rb +0 -26
  127. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/controllers/routes_controller.rb +0 -16
  128. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/api.rb +0 -26
  129. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/exception.rb +0 -23
  130. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/core_extensions/from_json.rb +0 -15
  131. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/dispatch.rb +0 -235
  132. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/models/resourced_route.rb +0 -84
  133. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/query.rb +0 -337
  134. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/html.rb +0 -50
  135. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/json.rb +0 -75
  136. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render/xml.rb +0 -65
  137. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/render.rb +0 -63
  138. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/retrieve.rb +0 -74
  139. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full/version.rb +0 -9
  140. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/lib/resource_full.rb +0 -14
  141. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/base_spec.rb +0 -88
  142. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/controllers/resources_spec.rb +0 -29
  143. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/dispatch_spec.rb +0 -262
  144. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/models/resourced_route_spec.rb +0 -62
  145. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/query/parameter_spec.rb +0 -57
  146. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/query_spec.rb +0 -462
  147. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/html_spec.rb +0 -4
  148. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/json_spec.rb +0 -107
  149. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render/xml_spec.rb +0 -98
  150. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/render_spec.rb +0 -5
  151. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/resource_full/retrieve_spec.rb +0 -173
  152. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/spec/spec_helper.rb +0 -98
  153. data/spec/functional/sample_rails_app/vendor/plugins/resource_full/uninstall.rb +0 -1
  154. data/spec/functional/spec_helper.rb +0 -0
@@ -1,20 +0,0 @@
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.
@@ -1,100 +0,0 @@
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.
@@ -1,18 +0,0 @@
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
@@ -1,5 +0,0 @@
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'
@@ -1 +0,0 @@
1
- # Install hook code here
@@ -1,140 +0,0 @@
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
@@ -1,16 +0,0 @@
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
@@ -1,26 +0,0 @@
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
@@ -1,16 +0,0 @@
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
@@ -1,26 +0,0 @@
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
@@ -1,23 +0,0 @@
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
@@ -1,15 +0,0 @@
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
@@ -1,235 +0,0 @@
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