Permalink
Browse files

sets up the create action :)

  • Loading branch information...
1 parent e974712 commit 66353d0369711062c4ef7887f97ec59b9aaf84fe @hassox hassox committed Jul 28, 2008
Showing with 41 additions and 13 deletions.
  1. +25 −3 merb-rest-client/lib/merb_rest_adapter.rb
  2. +16 −10 merb-rest-client/spec/merb_rest_adapter_spec.rb
@@ -11,7 +11,18 @@ def initialize(name, uri_or_options)
public
def create(resources)
- # created = 0
+ created = 0
+ resources.each do |resource|
+ repository = resource.repository
+ model = resource.model
+ attributes = resource.dirty_attributes
+ storage_name = model.storage_name(repository.name)
+
+ result = api_post(storage_name, storage_name.singular => attributes_hash(attributes) )
+ created += 1
+ end
+ created
+ end
# resources.each do |resource|
# repository = resource.repository
# model = resource.model
@@ -35,7 +46,6 @@ def create(resources)
# end
# end
# created
- end
def read_one(query)
response = api_get(resource_name(query).to_s, api_query_parameters(query))
@@ -46,7 +56,6 @@ def read_one(query)
end
def read_many(query)
- resource = resource_name(query)
Collection.new(query) do |collection|
result = api_get(resource_name(query).to_s, api_query_parameters(query))
values_array =[]
@@ -152,6 +161,19 @@ def order_parameters(order)
end
{"order" => out}
end
+
+ def attributes_hash(hash)
+ out = {}
+ hash.each do |k,v|
+ case k
+ when Property
+ out[k.name.to_s] = v
+ else
+ out[k.to_s] = v
+ end
+ end
+ out
+ end
def parse_results(data)
case @format
@@ -40,10 +40,6 @@ def self.default_repository_name
DataMapper.setup(:merb_rest, "merb_rest://example.com")
@repository = repository(:merb_rest)
@adapter = @repository.adapter
-
- 1.upto(10) do |n|
- Post.create(:title => "title #{n}", :body => "body #{n}")
- end
end
@@ -53,15 +49,25 @@ def self.default_repository_name
it "should setup a connection with basic auth"
describe "create" do
+ before(:each) do
+ @response = mock("response")
+ @adapter.stub!(:abstract_request).and_return(@response)
+ @response.stub!(:body).and_return(@json)
+ end
+
it{@adapter.should respond_to(:create)}
- it "should send a post to the Post resource with parameters" do
- pending
- params = {:post => {:title => "created post", :body => "created_body"}}
- RestClient.should_receive(:post).with("http://example.com/posts", params.to_params)
- Post.create(params[:post])
+
+ it "should create a post" do
+ @adapter.should_receive(:api_post).with("posts", "post" => {"title" => "a title", "body" => "a body"})
+ Post.create(:title => "a title", :body => "a body")
+ end
+
+ it "should return the created item" do
+ post = Post.create(:title => "a title", :body => "a body")
+ post.should be_a_kind_of(Post)
+ post.title.should == "a title"
end
- it "should return the number of created items"
it "should return 0 if a post does not save"
end

0 comments on commit 66353d0

Please sign in to comment.