dm-rest-adapter 0.9.10 → 0.9.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. data/History.txt +7 -1
  2. data/Manifest.txt +9 -87
  3. data/README.markdown +47 -0
  4. data/README.txt +44 -0
  5. data/Rakefile +5 -5
  6. data/config/database.rb.example +8 -0
  7. data/dm-rest-adapter.gemspec +34 -0
  8. data/lib/rest_adapter.rb +10 -246
  9. data/lib/rest_adapter/adapter.rb +291 -0
  10. data/lib/rest_adapter/connection.rb +76 -0
  11. data/lib/rest_adapter/exceptions.rb +66 -0
  12. data/lib/rest_adapter/formats.rb +16 -0
  13. data/lib/rest_adapter/version.rb +2 -4
  14. data/spec/connection_spec.rb +130 -0
  15. data/spec/crud_spec.rb +250 -0
  16. data/spec/spec_helper.rb +1 -6
  17. data/tasks/spec.rb +1 -1
  18. metadata +16 -94
  19. data/fixtures/book_service/README +0 -256
  20. data/fixtures/book_service/Rakefile +0 -10
  21. data/fixtures/book_service/app/controllers/application.rb +0 -15
  22. data/fixtures/book_service/app/controllers/books_controller.rb +0 -85
  23. data/fixtures/book_service/app/controllers/shelves_controller.rb +0 -89
  24. data/fixtures/book_service/app/helpers/application_helper.rb +0 -3
  25. data/fixtures/book_service/app/helpers/books_helper.rb +0 -2
  26. data/fixtures/book_service/app/helpers/shelf_helper.rb +0 -2
  27. data/fixtures/book_service/app/models/book.rb +0 -6
  28. data/fixtures/book_service/app/models/shelf.rb +0 -5
  29. data/fixtures/book_service/app/views/books/edit.html.erb +0 -20
  30. data/fixtures/book_service/app/views/books/index.html.erb +0 -22
  31. data/fixtures/book_service/app/views/books/new.html.erb +0 -19
  32. data/fixtures/book_service/app/views/books/show.html.erb +0 -13
  33. data/fixtures/book_service/app/views/layouts/books.html.erb +0 -17
  34. data/fixtures/book_service/app/views/shelves/edit.html.erb +0 -16
  35. data/fixtures/book_service/app/views/shelves/index.html.erb +0 -20
  36. data/fixtures/book_service/app/views/shelves/new.html.erb +0 -15
  37. data/fixtures/book_service/app/views/shelves/show.html.erb +0 -7
  38. data/fixtures/book_service/config/boot.rb +0 -109
  39. data/fixtures/book_service/config/database.yml +0 -19
  40. data/fixtures/book_service/config/environment.rb +0 -67
  41. data/fixtures/book_service/config/environments/development.rb +0 -17
  42. data/fixtures/book_service/config/environments/production.rb +0 -22
  43. data/fixtures/book_service/config/environments/test.rb +0 -22
  44. data/fixtures/book_service/config/initializers/inflections.rb +0 -10
  45. data/fixtures/book_service/config/initializers/mime_types.rb +0 -5
  46. data/fixtures/book_service/config/initializers/new_rails_defaults.rb +0 -15
  47. data/fixtures/book_service/config/routes.rb +0 -44
  48. data/fixtures/book_service/db/development.sqlite3 +0 -0
  49. data/fixtures/book_service/db/migrate/20080608165526_create_books.rb +0 -15
  50. data/fixtures/book_service/db/migrate/20080621171551_create_shelves.rb +0 -13
  51. data/fixtures/book_service/db/migrate/20080629143033_create_fake_books_and_shelves.rb +0 -20
  52. data/fixtures/book_service/db/schema.rb +0 -28
  53. data/fixtures/book_service/public/404.html +0 -30
  54. data/fixtures/book_service/public/422.html +0 -30
  55. data/fixtures/book_service/public/500.html +0 -30
  56. data/fixtures/book_service/public/dispatch.cgi +0 -10
  57. data/fixtures/book_service/public/dispatch.fcgi +0 -24
  58. data/fixtures/book_service/public/dispatch.rb +0 -10
  59. data/fixtures/book_service/public/favicon.ico +0 -0
  60. data/fixtures/book_service/public/images/rails.png +0 -0
  61. data/fixtures/book_service/public/index.html +0 -274
  62. data/fixtures/book_service/public/javascripts/application.js +0 -2
  63. data/fixtures/book_service/public/javascripts/controls.js +0 -963
  64. data/fixtures/book_service/public/javascripts/dragdrop.js +0 -972
  65. data/fixtures/book_service/public/javascripts/effects.js +0 -1120
  66. data/fixtures/book_service/public/javascripts/prototype.js +0 -4225
  67. data/fixtures/book_service/public/robots.txt +0 -5
  68. data/fixtures/book_service/public/stylesheets/scaffold.css +0 -53
  69. data/fixtures/book_service/script/about +0 -3
  70. data/fixtures/book_service/script/console +0 -3
  71. data/fixtures/book_service/script/dbconsole +0 -3
  72. data/fixtures/book_service/script/destroy +0 -3
  73. data/fixtures/book_service/script/generate +0 -3
  74. data/fixtures/book_service/script/performance/benchmarker +0 -3
  75. data/fixtures/book_service/script/performance/profiler +0 -3
  76. data/fixtures/book_service/script/performance/request +0 -3
  77. data/fixtures/book_service/script/plugin +0 -3
  78. data/fixtures/book_service/script/process/inspector +0 -3
  79. data/fixtures/book_service/script/process/reaper +0 -3
  80. data/fixtures/book_service/script/process/spawner +0 -3
  81. data/fixtures/book_service/script/runner +0 -3
  82. data/fixtures/book_service/script/server +0 -3
  83. data/fixtures/book_service/test/fixtures/books.yml +0 -9
  84. data/fixtures/book_service/test/fixtures/shelves.yml +0 -7
  85. data/fixtures/book_service/test/functional/books_controller_test.rb +0 -45
  86. data/fixtures/book_service/test/functional/shelf_controller_test.rb +0 -8
  87. data/fixtures/book_service/test/test_helper.rb +0 -38
  88. data/fixtures/book_service/test/unit/book_test.rb +0 -8
  89. data/fixtures/book_service/test/unit/shelf_test.rb +0 -8
  90. data/spec/create_spec.rb +0 -21
  91. data/spec/delete_spec.rb +0 -22
  92. data/spec/read_spec.rb +0 -101
  93. data/spec/update_spec.rb +0 -36
  94. data/stories/all.rb +0 -5
  95. data/stories/crud/create +0 -39
  96. data/stories/crud/delete +0 -9
  97. data/stories/crud/read +0 -36
  98. data/stories/crud/stories.rb +0 -18
  99. data/stories/crud/update +0 -44
  100. data/stories/helper.rb +0 -19
  101. data/stories/resources/helpers/book.rb +0 -7
  102. data/stories/resources/helpers/story_helper.rb +0 -2
  103. data/stories/resources/steps/read.rb +0 -35
  104. data/stories/resources/steps/using_rest_adapter.rb +0 -99
  105. data/tasks/stories.rb +0 -5
data/History.txt CHANGED
@@ -1,6 +1,12 @@
1
+ === 0.9.11 / 2009-03-29
2
+
3
+ * 1 major enhancement:
4
+
5
+ * Updated and refactored entire adapter
6
+
1
7
  === 0.9.10 / 2009-01-19
2
8
 
3
- * No changes this version
9
+ * No changes this version
4
10
 
5
11
  === 0.9.9 / 2009-01-04
6
12
 
data/Manifest.txt CHANGED
@@ -1,100 +1,22 @@
1
1
  History.txt
2
2
  LICENSE
3
3
  Manifest.txt
4
+ README.markdown
4
5
  README.txt
5
6
  Rakefile
6
7
  TODO
7
- fixtures/book_service/README
8
- fixtures/book_service/Rakefile
9
- fixtures/book_service/app/controllers/application.rb
10
- fixtures/book_service/app/controllers/books_controller.rb
11
- fixtures/book_service/app/controllers/shelves_controller.rb
12
- fixtures/book_service/app/helpers/application_helper.rb
13
- fixtures/book_service/app/helpers/books_helper.rb
14
- fixtures/book_service/app/helpers/shelf_helper.rb
15
- fixtures/book_service/app/models/book.rb
16
- fixtures/book_service/app/models/shelf.rb
17
- fixtures/book_service/app/views/books/edit.html.erb
18
- fixtures/book_service/app/views/books/index.html.erb
19
- fixtures/book_service/app/views/books/new.html.erb
20
- fixtures/book_service/app/views/books/show.html.erb
21
- fixtures/book_service/app/views/layouts/books.html.erb
22
- fixtures/book_service/app/views/shelves/edit.html.erb
23
- fixtures/book_service/app/views/shelves/index.html.erb
24
- fixtures/book_service/app/views/shelves/new.html.erb
25
- fixtures/book_service/app/views/shelves/show.html.erb
26
- fixtures/book_service/config/boot.rb
27
- fixtures/book_service/config/database.yml
28
- fixtures/book_service/config/environment.rb
29
- fixtures/book_service/config/environments/development.rb
30
- fixtures/book_service/config/environments/production.rb
31
- fixtures/book_service/config/environments/test.rb
32
- fixtures/book_service/config/initializers/inflections.rb
33
- fixtures/book_service/config/initializers/mime_types.rb
34
- fixtures/book_service/config/initializers/new_rails_defaults.rb
35
- fixtures/book_service/config/routes.rb
36
- fixtures/book_service/db/development.sqlite3
37
- fixtures/book_service/db/migrate/20080608165526_create_books.rb
38
- fixtures/book_service/db/migrate/20080621171551_create_shelves.rb
39
- fixtures/book_service/db/migrate/20080629143033_create_fake_books_and_shelves.rb
40
- fixtures/book_service/db/schema.rb
41
- fixtures/book_service/public/404.html
42
- fixtures/book_service/public/422.html
43
- fixtures/book_service/public/500.html
44
- fixtures/book_service/public/dispatch.cgi
45
- fixtures/book_service/public/dispatch.fcgi
46
- fixtures/book_service/public/dispatch.rb
47
- fixtures/book_service/public/favicon.ico
48
- fixtures/book_service/public/images/rails.png
49
- fixtures/book_service/public/index.html
50
- fixtures/book_service/public/javascripts/application.js
51
- fixtures/book_service/public/javascripts/controls.js
52
- fixtures/book_service/public/javascripts/dragdrop.js
53
- fixtures/book_service/public/javascripts/effects.js
54
- fixtures/book_service/public/javascripts/prototype.js
55
- fixtures/book_service/public/robots.txt
56
- fixtures/book_service/public/stylesheets/scaffold.css
57
- fixtures/book_service/script/about
58
- fixtures/book_service/script/console
59
- fixtures/book_service/script/dbconsole
60
- fixtures/book_service/script/destroy
61
- fixtures/book_service/script/generate
62
- fixtures/book_service/script/performance/benchmarker
63
- fixtures/book_service/script/performance/profiler
64
- fixtures/book_service/script/performance/request
65
- fixtures/book_service/script/plugin
66
- fixtures/book_service/script/process/inspector
67
- fixtures/book_service/script/process/reaper
68
- fixtures/book_service/script/process/spawner
69
- fixtures/book_service/script/runner
70
- fixtures/book_service/script/server
71
- fixtures/book_service/test/fixtures/books.yml
72
- fixtures/book_service/test/fixtures/shelves.yml
73
- fixtures/book_service/test/functional/books_controller_test.rb
74
- fixtures/book_service/test/functional/shelf_controller_test.rb
75
- fixtures/book_service/test/test_helper.rb
76
- fixtures/book_service/test/unit/book_test.rb
77
- fixtures/book_service/test/unit/shelf_test.rb
8
+ config/database.rb.example
9
+ dm-rest-adapter.gemspec
78
10
  lib/rest_adapter.rb
11
+ lib/rest_adapter/adapter.rb
12
+ lib/rest_adapter/connection.rb
13
+ lib/rest_adapter/exceptions.rb
14
+ lib/rest_adapter/formats.rb
79
15
  lib/rest_adapter/version.rb
80
- spec/create_spec.rb
81
- spec/delete_spec.rb
82
- spec/read_spec.rb
16
+ spec/connection_spec.rb
17
+ spec/crud_spec.rb
83
18
  spec/ruby_forker.rb
84
19
  spec/spec.opts
85
20
  spec/spec_helper.rb
86
- spec/update_spec.rb
87
- stories/all.rb
88
- stories/crud/create
89
- stories/crud/delete
90
- stories/crud/read
91
- stories/crud/stories.rb
92
- stories/crud/update
93
- stories/helper.rb
94
- stories/resources/helpers/book.rb
95
- stories/resources/helpers/story_helper.rb
96
- stories/resources/steps/read.rb
97
- stories/resources/steps/using_rest_adapter.rb
98
21
  tasks/install.rb
99
22
  tasks/spec.rb
100
- tasks/stories.rb
data/README.markdown ADDED
@@ -0,0 +1,47 @@
1
+ = dm-rest-adapter
2
+
3
+ A DataMapper adapter for REST Web Services
4
+
5
+ == Usage
6
+
7
+ DM Rest Adapter requires the use of a model which is the same name as the resource you are using. For example, if you have a resource named "posts" you will create a standard datamapper object called post.rb in app/models. The only difference in this model is you will need to define the rest adapter for the model. The following is an example of a post model, where the host settings point to the app you are running the resource on. In addition I have included a basic auth login which will be used if your resource requires auth:
8
+
9
+ DataMapper.setup(:default, {
10
+ :adapter => 'rest',
11
+ :format => 'xml',
12
+ :host => 'localhost',
13
+ :port => 4000,
14
+ :login => 'user',
15
+ :password => 'verys3crit'
16
+ })
17
+
18
+ class Post
19
+
20
+ include DataMapper::Resource
21
+
22
+ property :id, Serial
23
+ property :title, String
24
+ property :body, Text
25
+
26
+ end
27
+
28
+
29
+ If you notice this looks exactly like a normal datmapper model. Every property you define will map itself with the xml returned or posted from/to the resource.
30
+
31
+ == Code
32
+
33
+ Now for some code examples. DM Rest Adapter uses the same methods as datamapper including during creation.
34
+
35
+ Post.first => returns the object from the resouce
36
+ Post.get(1) => returns the object from the resource
37
+ p = Post.new(:title => "My awesome blog post", :body => "I really have nothing to say...")
38
+ p.save => saves the resource on the remote
39
+
40
+ == Caveat
41
+
42
+ Posts do not honor RESTful HTTP status codes. I might fix this...
43
+
44
+ == TODO:
45
+
46
+ Nested resources
47
+ Put verb actions
data/README.txt CHANGED
@@ -1,3 +1,47 @@
1
1
  = dm-rest-adapter
2
2
 
3
3
  A DataMapper adapter for REST Web Services
4
+
5
+ == Usage
6
+
7
+ DM Rest Adapter requires the use of a model which is the same name as the resource you are using. For example, if you have a resource named "posts" you will create a standard datamapper object called post.rb in app/models. The only difference in this model is you will need to define the rest adapter for the model. The following is an example of a post model, where the host settings point to the app you are running the resource on. In addition I have included a basic auth login which will be used if your resource requires auth:
8
+
9
+ DataMapper.setup(:default, {
10
+ :adapter => 'rest',
11
+ :format => 'xml',
12
+ :host => 'localhost',
13
+ :port => 4000,
14
+ :login => 'user',
15
+ :password => 'verys3crit'
16
+ })
17
+
18
+ class Post
19
+
20
+ include DataMapper::Resource
21
+
22
+ property :id, Serial
23
+ property :title, String
24
+ property :body, Text
25
+
26
+ end
27
+
28
+
29
+ If you notice this looks exactly like a normal datmapper model. Every property you define will map itself with the xml returned or posted from/to the resource.
30
+
31
+ == Code
32
+
33
+ Now for some code examples. DM Rest Adapter uses the same methods as datamapper including during creation.
34
+
35
+ Post.first => returns the object from the resouce
36
+ Post.get(1) => returns the object from the resource
37
+ p = Post.new(:title => "My awesome blog post", :body => "I really have nothing to say...")
38
+ p.save => saves the resource on the remote
39
+
40
+ == Caveat
41
+
42
+ Posts do not honor RESTful HTTP status codes. I might fix this...
43
+
44
+ == TODO:
45
+
46
+ Nested resources
47
+ Put verb actions
data/Rakefile CHANGED
@@ -8,16 +8,16 @@ SUDO = (WINDOWS || JRUBY) ? '' : ('sudo' unless ENV['SUDOLESS'])
8
8
 
9
9
  require ROOT + 'lib/rest_adapter/version'
10
10
 
11
- AUTHOR = 'Potomac Ruby Hackers'
12
- EMAIL = 'potomac-ruby-hackers [a] googlegroups [d] com'
11
+ AUTHOR = 'Scott Burton @ Joyent Inc'
12
+ EMAIL = 'scott.burton [a] joyent [d] com'
13
13
  GEM_NAME = 'dm-rest-adapter'
14
- GEM_VERSION = DataMapper::RestAdapter::VERSION
15
- GEM_DEPENDENCIES = [['dm-core', "~>#{GEM_VERSION}"]]
14
+ GEM_VERSION = DataMapperRest::VERSION
15
+ GEM_DEPENDENCIES = [['dm-core', GEM_VERSION]]
16
16
  GEM_CLEAN = %w[ log pkg coverage ]
17
17
  GEM_EXTRAS = { :has_rdoc => true, :extra_rdoc_files => %w[ README.txt LICENSE TODO History.txt ] }
18
18
 
19
19
  PROJECT_NAME = 'datamapper'
20
- PROJECT_URL = "http://github.com/sam/dm-more/tree/master/adapters/#{GEM_NAME}"
20
+ PROJECT_URL = "http://github.com/datamapper/dm-more/tree/master/adapters/#{GEM_NAME}"
21
21
  PROJECT_DESCRIPTION = PROJECT_SUMMARY = 'REST Adapter for DataMapper'
22
22
 
23
23
  [ ROOT, ROOT.parent.parent ].each do |dir|
@@ -0,0 +1,8 @@
1
+ DataMapper.setup(:default, {
2
+ :adapter => 'rest',
3
+ :format => 'xml',
4
+ :host => 'localhost',
5
+ :port => '4000',
6
+ :username => 'admin',
7
+ :password => 'secret'
8
+ })
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{dm-rest-adapter}
5
+ s.version = "0.9.11"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Scott Burton @ Joyent Inc"]
9
+ s.date = %q{2009-03-23}
10
+ s.description = %q{REST Adapter for DataMapper}
11
+ s.email = ["scott.burton [a] joyent [d] com"]
12
+ s.extra_rdoc_files = ["README.txt", "LICENSE", "TODO", "History.txt"]
13
+ s.files = ["History.txt", "LICENSE", "Manifest.txt", "README.markdown", "README.txt", "Rakefile", "TODO", "config/database.rb.example", "dm-rest-adapter.gemspec", "lib/rest_adapter.rb", "lib/rest_adapter/adapter.rb", "lib/rest_adapter/connection.rb", "lib/rest_adapter/exceptions.rb", "lib/rest_adapter/formats.rb", "lib/rest_adapter/version.rb", "spec/connection_spec.rb", "spec/crud_spec.rb", "spec/ruby_forker.rb", "spec/spec.opts", "spec/spec_helper.rb", "tasks/install.rb", "tasks/spec.rb"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/datamapper/dm-more/tree/master/adapters/dm-rest-adapter}
16
+ s.rdoc_options = ["--main", "README.txt"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{datamapper}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{REST Adapter for DataMapper}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ s.add_runtime_dependency(%q<dm-core>, ["= 0.9.11"])
28
+ else
29
+ s.add_dependency(%q<dm-core>, ["= 0.9.11"])
30
+ end
31
+ else
32
+ s.add_dependency(%q<dm-core>, ["= 0.9.11"])
33
+ end
34
+ end
data/lib/rest_adapter.rb CHANGED
@@ -1,251 +1,15 @@
1
- require 'net/http'
2
- require 'pathname'
3
- require 'rexml/document'
4
- require 'rubygems'
1
+ $:.push File.expand_path(File.dirname(__FILE__))
5
2
 
6
- gem 'dm-core', '~>0.9.10'
7
3
  require 'dm-core'
8
-
9
- gem 'extlib', '~>0.9.10'
10
4
  require 'extlib'
11
-
12
- #gem 'dm-serializer', '~>0.9.10'
5
+ require 'pathname'
6
+ require 'rexml/document'
7
+ require 'rubygems'
13
8
  require 'dm-serializer'
9
+ require 'rest_adapter/version'
10
+ require 'rest_adapter/adapter'
11
+ require 'rest_adapter/connection'
12
+ require 'rest_adapter/formats'
13
+ require 'rest_adapter/exceptions'
14
14
 
15
- require Pathname(__FILE__).dirname + 'rest_adapter/version'
16
-
17
- # TODO: Abstract XML support out from the protocol
18
- # TODO: Build JSON support
19
- module DataMapper
20
- module Adapters
21
- class RestAdapter < AbstractAdapter
22
- include Extlib
23
-
24
- # Creates a new resource in the specified repository.
25
- def create(resources)
26
- count = 0
27
- resources.each do |resource|
28
- resource_name = Inflection.underscore(resource.class.name)
29
- result = http_post("/#{resource_name.pluralize}.xml", resource.to_xml)
30
- # TODO: Raise error if cannot reach server
31
- success = result.instance_of?(Net::HTTPCreated)
32
- if success
33
- count += 1
34
- # TODO: Fix commented out code below to work through the identity_map of the repository
35
- # values = parse_resource(result.body, resource.class)
36
- # resource.id = updated_resource.id
37
- end
38
- # TODO: We're not using the response to update the DataMapper::Resource with the newly acquired ID!!!
39
- end
40
- count
41
- end
42
-
43
- # read_set
44
- #
45
- # Examples of query string:
46
- # A. []
47
- # GET /books/
48
- #
49
- # B. [[:eql, #<Property:Book:id>, 4200]]
50
- # GET /books/4200
51
- #
52
- # IN PROGRESS
53
- # TODO: Need to account for query.conditions (i.e., [[:eql, #<Property:Book:id>, 1]] for books/1)
54
- def read_many(query)
55
- resource_name = Inflection.underscore(query.model.name)
56
- Collection.new(query) do |collection|
57
- case query.conditions
58
- when []
59
- resources_meta = read_set_all(repository, query, resource_name)
60
- else
61
- resources_meta = read_set_for_condition(repository, query, resource_name)
62
- end
63
- resources_meta.each do |resource_meta|
64
- if resource_meta.has_key?(:associations)
65
- load_nested_resources_from resource_meta[:associations], query
66
- end
67
- collection.load(resource_meta[:values])
68
- end
69
- end
70
- end
71
-
72
- def read_one(query)
73
- resource = nil
74
- resource_name = resource_name_from_query(query)
75
- resources_meta = nil
76
- if query.conditions.empty? && query.limit == 1
77
- results = read_set_all(repository, query, resource_name)
78
- resource_meta = results.first unless results.empty?
79
- else
80
- id = query.conditions.first[2]
81
- # KLUGE: Again, we're assuming below that we're dealing with a pluralized resource mapping
82
-
83
- response = http_get("/#{resource_name.pluralize}/#{id}.xml")
84
-
85
- # KLUGE: Rails returns HTML if it can't find a resource. A properly RESTful app would return a 404, right?
86
- return nil if response.is_a? Net::HTTPNotFound || response.content_type == "text/html"
87
-
88
- data = response.body
89
- resource_meta = parse_resource(data, query.model, query)
90
- end
91
- if resource_meta
92
- if resource_meta.has_key?(:associations)
93
- load_nested_resources_from resource_meta[:associations], query
94
- end
95
- resource = query.model.load(resource_meta[:values], query)
96
- end
97
- resource
98
- end
99
-
100
- def update(attributes, query)
101
- # TODO What if we have a compound key?
102
- raise NotImplementedError.new unless is_single_resource_query? query
103
- id = query.conditions.first[2]
104
- resource = nil
105
- query.repository.scope do
106
- resource = query.model.get(id)
107
- end
108
- attributes.each do |attr, val|
109
- resource.send("#{attr.name}=", val)
110
- end
111
- # KLUGE: Again, we're assuming below that we're dealing with a pluralized resource mapping
112
- res = http_put("/#{resource_name_from_query(query).pluralize}/#{id}.xml", resource.to_xml)
113
- # TODO: Raise error if cannot reach server
114
- res.kind_of?(Net::HTTPSuccess) ? 1 : 0
115
- end
116
-
117
- def delete(query)
118
- raise NotImplementedError.new unless is_single_resource_query? query
119
- id = query.conditions.first[2]
120
- res = http_delete("/#{resource_name_from_query(query).pluralize}/#{id}.xml")
121
- res.kind_of?(Net::HTTPSuccess) ? 1 : 0
122
- end
123
-
124
- protected
125
- def load_nested_resources_from(nested_resources, query)
126
- nested_resources.each do |resource_meta|
127
- # TODO: Houston, we have a problem. Model#load expects a Query. When we're nested, we don't have a query yet...
128
- #resource_meta[:model].load(resource_meta[:values])
129
- #if resource_meta.has_key? :associations
130
- # load_nested_resources_from resource_meta, query
131
- #end
132
- end
133
- end
134
-
135
- def read_set_all(repository, query, resource_name)
136
- # TODO: how do we know whether the resource we're talking to is singular or plural?
137
- res = http_get("/#{resource_name.pluralize}.xml")
138
- data = res.body
139
- parse_resources(data, query.model, query)
140
- # TODO: Raise error if cannot reach server
141
- end
142
-
143
- # GET /books/4200
144
- def read_set_for_condition(repository, query, resource_name)
145
- # More complex conditions
146
- raise NotImplementedError.new
147
- end
148
-
149
- # query.conditions like [[:eql, #<Property:Book:id>, 4200]]
150
- def is_single_resource_query?(query)
151
- query.conditions.length == 1 && query.conditions.first.first == :eql && query.conditions.first[1].name == :id
152
- end
153
-
154
- def http_put(uri, data = nil)
155
- request do |http|
156
- request = Net::HTTP::Put.new(uri, data)
157
- request.basic_auth(@uri[:login], @uri[:password]) unless @uri[:login].blank?
158
- http.request(request)
159
- end
160
- end
161
-
162
- def http_post(uri, data)
163
- request do |http|
164
- request = Net::HTTP::Post.new(uri, data, 'Content-Type' => 'application/xml')
165
- request.basic_auth(@uri[:login], @uri[:password]) unless @uri[:login].blank?
166
- http.request(request)
167
- end
168
- end
169
-
170
- def http_get(uri)
171
- request do |http|
172
- request = Net::HTTP::Get.new(uri)
173
- request.basic_auth(@uri[:login], @uri[:password]) unless @uri[:login].blank?
174
- http.request(request)
175
- end
176
- end
177
-
178
- def http_delete(uri)
179
- request do |http|
180
- request = Net::HTTP::Delete.new(uri)
181
- request.basic_auth(@uri[:login], @uri[:password]) unless @uri[:login].blank?
182
- http.request(request)
183
- end
184
- end
185
-
186
- def request(&block)
187
- res = nil
188
- Net::HTTP.start(@uri[:host], @uri[:port].to_i) do |http|
189
- res = yield(http)
190
- end
191
- res
192
- end
193
-
194
- def values_from_rexml(entity_element, dm_model_class)
195
- resource = {}
196
- resource[:values] = []
197
- entity_element.elements.each do |field_element|
198
- attribute = dm_model_class.properties(repository.name).find do |property|
199
- property.name.to_s == field_element.name.to_s.tr('-', '_')
200
- end
201
- if attribute
202
- resource[:values] << field_element.text
203
- next
204
- end
205
- association = dm_model_class.relationships.find do |name, dm_relationship|
206
- field_element.name.to_s == Inflection.pluralize(Inflection.underscore(dm_relationship.child_model.to_s))
207
- end
208
- if association
209
- field_element.each_element do |associated_element|
210
- model = association[1].child_model
211
- (resource[:associations] ||= []) << {
212
- :model => model,
213
- :value => values_from_rexml(associated_element, association[1].child_model)
214
- }
215
- end
216
- end
217
- end
218
- resource
219
- end
220
-
221
- def parse_resource(xml, dm_model_class, query = nil)
222
- doc = REXML::Document::new(xml)
223
- # TODO: handle singular resource case as well....
224
- entity_element = REXML::XPath.first(doc, "/#{resource_name_from_model(dm_model_class)}")
225
- return nil unless entity_element
226
- values_from_rexml(entity_element, dm_model_class)
227
- end
228
-
229
- def parse_resources(xml, dm_model_class, query = nil)
230
- doc = REXML::Document::new(xml)
231
- # # TODO: handle singular resource case as well....
232
- # array = XPath(doc, "/*[@type='array']")
233
- # if array
234
- # parse_resources()
235
- # else
236
- resource_name = resource_name_from_model dm_model_class
237
- doc.elements.collect("#{resource_name.pluralize}/#{resource_name}") do |entity_element|
238
- values_from_rexml(entity_element, dm_model_class)
239
- end
240
- end
241
-
242
- def resource_name_from_model(model)
243
- Inflection.underscore(model.name)
244
- end
245
-
246
- def resource_name_from_query(query)
247
- resource_name_from_model(query.model)
248
- end
249
- end
250
- end
251
- end
15
+ DataMapper::Adapters::RestAdapter = DataMapperRest::Adapter