dm-restful-adapter 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,39 +2,44 @@ module Restful
2
2
  class Adapter < DataMapper::Adapters::AbstractAdapter
3
3
 
4
4
  def create(resources)
5
- resources.each { |resource|
5
+ resources.map { |resource|
6
6
  dirty_attributes = resource.dirty_attributes.inject({}) { |hash, (k,v)| hash.update(k.name => v) }
7
- response = Request.post(resource.model.storage_name, dirty_attributes)
8
- properties = resource.model.properties
9
- response.each do |attr_name, val|
10
- if prop = properties[attr_name.to_sym]
11
- prop.set!(resource, val)
7
+ response = Request.post(resource.model.storage_name, dirty_attributes)
8
+ if response.status == 200
9
+ properties = resource.model.properties
10
+ response.body.each do |attr_name, val|
11
+ if prop = properties[attr_name.to_sym]
12
+ prop.set!(resource, val)
13
+ end
12
14
  end
13
15
  end
14
- }.size
16
+ }.compact.size
15
17
  end
16
18
 
17
19
  def read(query)
18
- Request.get(query.model.storage_name, query.params)
20
+ Request.get(query.model.storage_name, query.params).body
19
21
  end
20
22
 
21
23
  def update(attrs, resources)
22
- resources.each do |resource|
24
+ resources.map do |resource|
23
25
  attr_hash = attrs.inject({}) { |h, (k, v)| h.merge(k.name => v) }
24
26
  response = Request.put(resource.model.storage_name, resource.key, attr_hash)
25
- properties = resource.model.properties.inspect
26
- response.except(:id).each do |k, v|
27
- if prop = properties[k.to_sym]
28
- prop.set!(resource, v)
27
+ if response.status == 200
28
+ properties = resource.model.properties.inspect
29
+ response.body.except(:id).each do |k, v|
30
+ if prop = properties[k.to_sym]
31
+ prop.set!(resource, v)
32
+ end
29
33
  end
30
34
  end
31
- end.size
35
+ end.compact.size
32
36
  end
33
37
 
34
38
  def delete(resources)
35
- resources.each do |resource|
36
- Request.delete(resource.model.storage_name, resource.key)
37
- end.size
39
+ resources.map do |resource|
40
+ response = Request.delete(resource.model.storage_name, resource.key)
41
+ response.status == 200 ? response.body : nil
42
+ end.compact.size
38
43
  end
39
44
  end
40
45
  end
@@ -3,7 +3,7 @@ module Restful
3
3
  class Typhoeus
4
4
  def self.call(method, path, params={})
5
5
  response = ::Typhoeus::Request.run(path, :method => method, :params => params)
6
- Configuration.parser.decode(response.body)
6
+ ::Restful::Response.new(:body => Configuration.parser.decode(response.body), :status => response.code)
7
7
  end
8
8
  end
9
9
  end
@@ -0,0 +1,16 @@
1
+ module Restful
2
+ class Response
3
+ def initialize(options={})
4
+ @body = options[:body]
5
+ @status = options[:status]
6
+ end
7
+
8
+ def body
9
+ @body
10
+ end
11
+
12
+ def status
13
+ @status
14
+ end
15
+ end
16
+ end
@@ -1,7 +1,7 @@
1
1
  module DataMapper
2
2
  module Adapters
3
3
  module RestfulAdapter
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.5'
5
5
  end
6
6
  end
7
7
  end
@@ -6,6 +6,7 @@ require 'dm-restful-adapter/version'
6
6
  module Restful
7
7
  autoload :Adapter, 'dm-restful-adapter/adapter'
8
8
  autoload :Request, 'dm-restful-adapter/request'
9
+ autoload :Response, 'dm-restful-adapter/response'
9
10
  autoload :Configuration, 'dm-restful-adapter/configuration'
10
11
 
11
12
  module Backends
@@ -3,13 +3,94 @@ require File.dirname(__FILE__) + '/spec_helper'
3
3
  require 'dm-core/spec/shared/adapter_spec'
4
4
 
5
5
  describe DataMapper::Adapters::RestfulAdapter do
6
- before :all do
7
- @adapter = DataMapper.setup(:default, :adapter => 'restful')
8
- end
6
+ let!(:adapter) { DataMapper.setup(:default, :adapter => 'restful') }
7
+ let!(:repository) { DataMapper.repository(:default) }
8
+ let!(:heffalump) {
9
+ class ::Heffalump
10
+ include DataMapper::Resource
11
+
12
+ property :id, Serial
13
+ property :color, String
14
+ property :num_spots, Integer
15
+ property :striped, Boolean
16
+ end
17
+
18
+
19
+ # create all tables and constraints before each spec
20
+ if @repository.respond_to?(:auto_migrate!)
21
+ Heffalump.auto_migrate!
22
+ end
23
+ }
24
+ let!(:failing_heffalump) {
25
+ class FailingHeffalump < ::Heffalump
26
+ storage_names[:default] = 'failing_heffalumps'
27
+ def self.name; 'FailingHeffalump'; end
28
+ end
29
+ }
30
+
31
+ before { DataMapper.finalize }
9
32
 
10
33
  def self.described_type
11
34
  described_class
12
35
  end
13
36
 
14
37
  it_should_behave_like 'An Adapter'
38
+
39
+ describe '#create' do
40
+ context 'success' do
41
+ it 'returns number of records successfully created' do
42
+ heffalump = ::Heffalump.new(:color => 'red')
43
+ adapter.create([heffalump]).should == 1
44
+ end
45
+ end
46
+
47
+ context 'error' do
48
+ it 'returns 0 if no records are successfully created' do
49
+ DataMapper.finalize
50
+
51
+ failing_heffalump = FailingHeffalump.new(:color => 'red')
52
+ adapter.create([failing_heffalump]).should == 0
53
+ end
54
+ end
55
+ end
56
+
57
+ describe '#update' do
58
+ context 'success' do
59
+ it 'returns number of records successfully saved' do
60
+ RemoteHeffalumpModel.create(:color => 'red')
61
+ heffalump = Heffalump.all.first
62
+ props = {heffalump.model.properties.detect { |prop| prop.name == :color} => 'blue'}
63
+ heffalump.color = 'blue'
64
+ adapter.update(props, [heffalump]).should == 1
65
+ end
66
+ end
67
+
68
+ context 'error' do
69
+ it 'returns 0 if no records are successfully saved' do
70
+ RemoteHeffalumpModel.create(:color => 'red')
71
+ failing_heffalump = FailingHeffalump.all.first
72
+ props = {failing_heffalump.model.properties.detect { |prop| prop.name ==:color} => 'blue'}
73
+ adapter.update(props, [failing_heffalump]).should == 0
74
+ end
75
+ end
76
+ end
77
+
78
+ describe '#delete' do
79
+ context 'success' do
80
+ it 'returns number of records successfully saved' do
81
+ RemoteHeffalumpModel.create(:color => 'red')
82
+ heffalump = Heffalump.all.first
83
+ adapter.delete([heffalump]).should == 1
84
+ end
85
+ end
86
+
87
+ context 'error' do
88
+ it 'returns 0 if no records are successfully saved' do
89
+ RemoteHeffalumpModel.create(:color => 'red')
90
+ failing_heffalump = FailingHeffalump.all.first
91
+ adapter.delete([failing_heffalump]).should == 0
92
+ end
93
+ end
94
+
95
+ end
15
96
  end
data/spec/spec_helper.rb CHANGED
@@ -11,7 +11,7 @@ require 'database_cleaner'
11
11
  RSpec.configure do |config|
12
12
  config.treat_symbols_as_metadata_keys_with_true_values = true
13
13
  config.run_all_when_everything_filtered = true
14
- config.fail_fast = true
14
+ #config.fail_fast = true
15
15
  config.filter_run :focus
16
16
 
17
17
  config.before(:suite) do
@@ -2,8 +2,9 @@ require 'mimic'
2
2
  require 'active_record'
3
3
  require 'spec/support/setup_mimic_db'
4
4
 
5
- class HeffalumpModel < ActiveRecord::Base
5
+ class RemoteHeffalumpModel < ActiveRecord::Base
6
6
  self.include_root_in_json = false
7
+ self.table_name = 'heffalump_models'
7
8
 
8
9
  def self.search(params)
9
10
  params = format_params(params)
@@ -79,7 +80,7 @@ end
79
80
 
80
81
  Mimic.mimic(:port => 4000) do
81
82
  post "/heffalumps" do
82
- heffalump = HeffalumpModel.new(params['heffalump'])
83
+ heffalump = RemoteHeffalumpModel.new(params['heffalump'])
83
84
  if heffalump.save
84
85
  response = heffalump.to_json
85
86
  [200, {'Content-Type' => 'application/json', 'Content-Length' => response.bytesize}, response]
@@ -89,27 +90,55 @@ Mimic.mimic(:port => 4000) do
89
90
  end
90
91
  end
91
92
 
93
+ put "/heffalumps/:id" do
94
+ heffalump = RemoteHeffalumpModel.find(params['id'])
95
+ heffalump.update_attributes(params['heffalump'])
96
+ response = heffalump.to_json
97
+ [200, {'Content-Type' => 'application/json', 'Content-Length' => response.bytesize}, response]
98
+ end
99
+
100
+ delete "/heffalumps/:id" do
101
+ heffalump = RemoteHeffalumpModel.find(params['id'])
102
+ heffalump.destroy
103
+ response = [].to_json
104
+ [200, {'Content-Type' => 'application/json', 'Content-Length' => response.bytesize}, response]
105
+ end
106
+
92
107
  get "/heffalumps" do
93
108
  if params.empty?
94
- lumps = HeffalumpModel.all
109
+ lumps = RemoteHeffalumpModel.all
95
110
  else
96
- lumps = HeffalumpModel.search(params)
111
+ lumps = RemoteHeffalumpModel.search(params)
97
112
  end
98
113
  response = lumps.to_json
99
114
  [200, {'Content-Type' => 'application/json', 'Content-Length' => response.bytesize}, response]
100
115
  end
101
116
 
102
- put "/heffalumps/:id" do
103
- heffalump = HeffalumpModel.find(params['id'])
104
- heffalump.update_attributes(params['heffalump'])
105
- response = heffalump.to_json
117
+ get "/failing_heffalumps" do
118
+ if params.empty?
119
+ lumps = RemoteHeffalumpModel.all
120
+ else
121
+ lumps = RemoteHeffalumpModel.search(params)
122
+ end
123
+ response = lumps.to_json
106
124
  [200, {'Content-Type' => 'application/json', 'Content-Length' => response.bytesize}, response]
107
125
  end
108
126
 
109
- delete "/heffalumps/:id" do
110
- heffalump = HeffalumpModel.find(params['id'])
111
- heffalump.destroy
127
+ post "/failing_heffalumps" do
128
+ failing_heffalump = RemoteHeffalumpModel.new(params['failing_heffalump'])
129
+ response = failing_heffalump.to_json
130
+ [422, {'Content-Type' => 'application/json', 'Content-Length' => response.bytesize}, response]
131
+ end
132
+
133
+ put "/failing_heffalumps/:id" do
134
+ heffalump = RemoteHeffalumpModel.find(params['id'])
135
+ response = heffalump.to_json
136
+ [422, {'Content-Type' => 'application/json', 'Content-Length' => response.bytesize}, response]
137
+ end
138
+
139
+ delete "/failing_heffalumps/:id" do
140
+ heffalump = RemoteHeffalumpModel.find(params['id'])
112
141
  response = [].to_json
113
- [200, {'Content-Type' => 'application/json', 'Content-Length' => response.bytesize}, response]
142
+ [422, {'Content-Type' => 'application/json', 'Content-Length' => response.bytesize}, response]
114
143
  end
115
144
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-restful-adapter
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ryan Moran
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2012-04-29 00:00:00 Z
19
+ date: 2012-05-14 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: dm-core
@@ -148,6 +148,7 @@ files:
148
148
  - lib/dm-restful-adapter/backends/typhoeus.rb
149
149
  - lib/dm-restful-adapter/configuration.rb
150
150
  - lib/dm-restful-adapter/request.rb
151
+ - lib/dm-restful-adapter/response.rb
151
152
  - lib/dm-restful-adapter/version.rb
152
153
  - spec/dm-restful-adapter_spec.rb
153
154
  - spec/spec_helper.rb
@@ -184,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
185
  requirements: []
185
186
 
186
187
  rubyforge_project:
187
- rubygems_version: 1.8.15
188
+ rubygems_version: 1.8.24
188
189
  signing_key:
189
190
  specification_version: 3
190
191
  summary: ""