Permalink
Browse files

import from svn

  • Loading branch information...
0 parents commit 6afa74bb6592f33fa5d328d070fb33f916be3343 @bwlang committed Mar 22, 2011
Showing with 2,671 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +13 −0 README
  3. +23 −0 Rakefile
  4. +145 −0 app/controllers/o_data_controller.rb
  5. +203 −0 app/helpers/o_data_helper.rb
  6. +62 −0 app/views/o_data/metadata.xml.builder
  7. +8 −0 app/views/o_data/resource.atom.builder
  8. +1 −0 app/views/o_data/resource.json.erb
  9. +11 −0 app/views/o_data/service.xml.builder
  10. +11 −0 config/routes.rb
  11. +3 −0 init.rb
  12. +1 −0 install.rb
  13. +12 −0 lib/o_data.rb
  14. +27 −0 lib/o_data/abstract_query.rb
  15. +133 −0 lib/o_data/abstract_query/base.rb
  16. +22 −0 lib/o_data/abstract_query/countable.rb
  17. +117 −0 lib/o_data/abstract_query/errors.rb
  18. +58 −0 lib/o_data/abstract_query/option.rb
  19. +39 −0 lib/o_data/abstract_query/options/enumerated_option.rb
  20. +81 −0 lib/o_data/abstract_query/options/expand_option.rb
  21. +19 −0 lib/o_data/abstract_query/options/format_option.rb
  22. +20 −0 lib/o_data/abstract_query/options/inlinecount_option.rb
  23. +79 −0 lib/o_data/abstract_query/options/orderby_option.rb
  24. +74 −0 lib/o_data/abstract_query/options/select_option.rb
  25. +32 −0 lib/o_data/abstract_query/options/skip_option.rb
  26. +32 −0 lib/o_data/abstract_query/options/top_option.rb
  27. +77 −0 lib/o_data/abstract_query/parser.rb
  28. +43 −0 lib/o_data/abstract_query/segment.rb
  29. +24 −0 lib/o_data/abstract_query/segments/collection_segment.rb
  30. +38 −0 lib/o_data/abstract_query/segments/count_segment.rb
  31. +116 −0 lib/o_data/abstract_query/segments/entity_type_and_key_values_segment.rb
  32. +31 −0 lib/o_data/abstract_query/segments/entity_type_segment.rb
  33. +49 −0 lib/o_data/abstract_query/segments/links_segment.rb
  34. +82 −0 lib/o_data/abstract_query/segments/navigation_property_segment.rb
  35. +50 −0 lib/o_data/abstract_query/segments/property_segment.rb
  36. +40 −0 lib/o_data/abstract_query/segments/value_segment.rb
  37. +9 −0 lib/o_data/abstract_schema.rb
  38. +29 −0 lib/o_data/abstract_schema/association.rb
  39. +48 −0 lib/o_data/abstract_schema/base.rb
  40. +42 −0 lib/o_data/abstract_schema/comparable.rb
  41. +50 −0 lib/o_data/abstract_schema/end.rb
  42. +64 −0 lib/o_data/abstract_schema/entity_type.rb
  43. +37 −0 lib/o_data/abstract_schema/navigation_property.rb
  44. +35 −0 lib/o_data/abstract_schema/property.rb
  45. +37 −0 lib/o_data/abstract_schema/schema_object.rb
  46. +79 −0 lib/o_data/abstract_schema/serializable.rb
  47. +8 −0 lib/o_data/active_record_schema.rb
  48. +125 −0 lib/o_data/active_record_schema/association.rb
  49. +37 −0 lib/o_data/active_record_schema/base.rb
  50. +120 −0 lib/o_data/active_record_schema/entity_type.rb
  51. +45 −0 lib/o_data/active_record_schema/navigation_property.rb
  52. +45 −0 lib/o_data/active_record_schema/property.rb
  53. +36 −0 lib/o_data/active_record_schema/serializable.rb
  54. +13 −0 public/clientaccesspolicy.xml
  55. +4 −0 tasks/o_data_server_tasks.rake
  56. +8 −0 test/o_data_server_test.rb
  57. +3 −0 test/test_helper.rb
  58. +1 −0 uninstall.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2010 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 README
@@ -0,0 +1,13 @@
+ODataServer
+===========
+
+Introduction goes here.
+
+
+Example
+=======
+
+Example goes here.
+
+
+Copyright (c) 2010 [name of plugin creator], released under the MIT license
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the o_data_server plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the o_data_server plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'ODataServer'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,145 @@
+class ODataController < ApplicationController
+ cattr_reader :path_param
+ @@path_param = :__path__.freeze
+
+ cattr_reader :schema
+ @@schema = OData::ActiveRecordSchema::Base.new.freeze
+
+ cattr_reader :parser
+ @@parser = OData::AbstractQuery::Parser.new(@@schema).freeze
+
+ rescue_from OData::ODataException, :with => :handle_exception
+ rescue_from ActiveRecord::RecordNotFound, :with => :handle_exception
+
+ before_filter :extract_resource_path_and_query_string, :only => [:resource]
+ before_filter :parse_resource_path_and_query_string!, :only => [:resource]
+ before_filter :set_request_format!, :only => [:resource]
+
+ %w{service metadata resource}.each do |method_name|
+ define_method(:"redirect_to_#{method_name}") do
+ # redirect_to(send(:"o_data_#{method_name}_url"))
+ redirect_to(params.merge(:action => method_name.to_s))
+ end
+ end
+
+ def service
+ respond_to do |format|
+ format.xml # service.xml.builder
+ format.json { render :json => @@schema.to_json }
+ end
+ end
+
+ def metadata
+ respond_to do |format|
+ format.xml # metadata.xml.builder
+ end
+ end
+
+ def resource
+ @last_segment = @query.segments.last
+
+ @results = @query.execute!
+
+ case @last_segment.class.segment_name
+ when OData::AbstractQuery::Segments::CountSegment.segment_name
+ render :text => @results.to_i
+ when OData::AbstractQuery::Segments::LinksSegment.segment_name
+ request.format = :xml unless request.format == :json
+
+ respond_to do |format|
+ format.xml { render :inline => "xml.instruct!; @results.empty? ? xml.links('xmlns' => 'http://schemas.microsoft.com/ado/2007/08/dataservices') : xml.links('xmlns' => 'http://schemas.microsoft.com/ado/2007/08/dataservices') { @results.each { |r| xml.uri(o_data_resource_url(r[1])) } }", :type => :builder }
+ format.json { render :json => { "links" => @results.collect { |r| { "uri" => r } } }.to_json }
+ end
+ when OData::AbstractQuery::Segments::ValueSegment.segment_name
+ render :text => @results.to_s
+ when OData::AbstractQuery::Segments::PropertySegment.segment_name
+ request.format = :xml unless request.format == :json
+
+ respond_to do |format|
+ format.xml { render :inline => "xml.instruct!; value.blank? ? xml.tag!(key.to_sym, 'm:null' => true, 'xmlns' => 'http://schemas.microsoft.com/ado/2007/08/dataservices', 'xmlns:m' => 'http://schemas.microsoft.com/ado/2007/08/dataservices') : xml.tag!(key.to_sym, value, 'edm:Type' => type, 'xmlns' => 'http://schemas.microsoft.com/ado/2007/08/dataservices', 'xmlns:edm' => 'http://schemas.microsoft.com/ado/2007/05/edm')", :locals => { :key => @results.keys.first.name, :type => @results.keys.first.return_type, :value => @results.values.first }, :type => :builder }
+ format.json { render :json => { @results.keys.first.name => @results.values.first }.to_json }
+ end
+ when OData::AbstractQuery::Segments::NavigationPropertySegment.segment_name
+ @countable = @last_segment.countable?
+
+ @navigation_property = @last_segment.navigation_property
+ @polymorphic = @navigation_property.to_end.polymorphic?
+
+ if @polymorphic
+ @entity_type = nil
+ @entity_type_name = @navigation_property.to_end.name.singularize
+ else
+ @entity_type = @navigation_property.to_end.entity_type
+ @entity_type_name = @entity_type.name
+ end
+
+ @collection_name = @entity_type_name.pluralize
+
+ @expand_navigation_property_paths = {}
+ if expand_option = @query.options.find { |o| o.option_name == OData::AbstractQuery::Options::ExpandOption.option_name }
+ @expand_navigation_property_paths = expand_option.navigation_property_paths
+ end
+
+ respond_to do |format|
+ format.atom # resource.atom.builder
+ format.json # resource.json.erb
+ end
+ when OData::AbstractQuery::Segments::CollectionSegment.segment_name
+ @countable = @last_segment.countable?
+
+ @navigation_property = nil
+ @polymorphic = true
+
+ @entity_type = @last_segment.entity_type
+
+ @expand_navigation_property_paths = {}
+ if expand_option = @query.options.find { |o| o.option_name == OData::AbstractQuery::Options::ExpandOption.option_name }
+ @expand_navigation_property_paths = expand_option.navigation_property_paths
+ end
+
+ respond_to do |format|
+ format.atom # resource.atom.builder
+ format.json # resource.json.erb
+ end
+ else
+ # in theory, this branch is unreachable because the <tt>parse_resource_path_and_query_string!</tt>
+ # method will throw an exception if the <tt>OData::AbstractQuery::Parser</tt> fails to match any
+ # segment of the resource path.
+ raise OData::AbstractQuery::Errors::AbstractQueryException.new(@query)
+ end
+ end
+
+ private
+
+ def extract_resource_path_and_query_string
+ @resource_path = params[@@path_param].join('/')
+
+ @query_string = params.inject({}) { |acc, pair|
+ key, value = pair
+ acc[key.to_sym] = value unless [@@path_param, :controller, :action].include?(key.to_sym)
+ acc
+ }.collect { |key, value|
+ key.to_s + '=' + value.to_s
+ }.join('&')
+ end
+
+ def parse_resource_path_and_query_string!
+ @query = @@parser.parse!([@resource_path, @query_string].compact.join('?'))
+ end
+
+ def set_request_format!
+ if format_option = @query.options.find { |o| o.option_name == OData::AbstractQuery::Options::FormatOption.option_name }
+ if format_value = format_option.value
+ request.format = format_value.to_sym
+ end
+ end
+ end
+
+ def handle_exception(ex)
+ request.format = :xml
+
+ respond_to do |format|
+ format.xml { render :inline => "xml.instruct!; xml.error('xmlns' => 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata') { xml.code(code.to_s); xml.message(message); xml.uri(uri) }", :type => :builder, :locals => { :code => nil, :message => ex.message, :uri => request.url } }
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 6afa74b

Please sign in to comment.