Skip to content
Browse files

Adds basic read_many functionality

  • Loading branch information...
1 parent 049d1d9 commit ec47e46ede9c9ec06bfc8ed0701fcf62c292ddfe @hassox hassox committed Jul 23, 2008
Showing with 144 additions and 46 deletions.
  1. +64 −41 merb-rest-client/lib/merb_rest_adapter.rb
  2. +80 −5 merb-rest-client/spec/merb_rest_adapter_spec.rb
View
105 merb-rest-client/lib/merb_rest_adapter.rb
@@ -5,38 +5,56 @@ class MerbRestAdapter < AbstractAdapter
private
def initialize(name, uri_or_options)
super
+ @format = :json
@uri = normalize_uri(uri_or_options)
end
public
def create(resources)
- created = 0
- resources.each do |resource|
- repository = resource.repository
- model = resource.model
- attributes = resource.dirty_attributes
-
- # TODO: make a model.identity_field method
- identity_field = model.key(repository.name).detect { |p| p.serial? }
+ # created = 0
+ # resources.each do |resource|
+ # repository = resource.repository
+ # model = resource.model
+ # attributes = resource.dirty_attributes
+ #
+ # # TODO: make a model.identity_field method
+ # identity_field = model.key(repository.name).detect { |p| p.serial? }
+ #
+ # paramteters = {resource_name(model) => model.value_paramters }
+ # # statement = create_statement(repository, model, attributes.keys, identity_field)
+ # # bind_values = attributes.values
+ #
+ # # result = execute(statement, *bind_values)
+ # result = post(query, URI.escape(parameters.to_params))
+ #
+ # if result.to_i == 1
+ # if identity_field
+ # identity_field.set!(resource, result.insert_id)
+ # end
+ # created += 1
+ # end
+ # end
+ # created
+ end
- paramteters = {resource_name(model) => model.value_paramters }
- # statement = create_statement(repository, model, attributes.keys, identity_field)
- # bind_values = attributes.values
+ # def read_one(query)
+ # end
- # result = execute(statement, *bind_values)
- result = post(query, URI.escape(parameters.to_params))
+ def read_many(query)
+ resource = resource_name(query)
+ Collection.new(query) do |collection|
+ parameters = condition_parameters(query.conditions)
+ parameters.merge!(order_parameters(query.order))
+ parameters.merge!(field_parameters(query.fields))
- if result.to_i == 1
- if identity_field
- identity_field.set!(resource, result.insert_id)
- end
- created += 1
- end
+ result = api_get(resource_name(query).to_s, parameters)
+ hash = parse_results(result.body)
+
+ collection.load([hash].flatten)
end
- created
end
- private
+ protected
def api_get(path, options = {})
abstract_request(
:class => Net::HTTP::Get,
@@ -95,36 +113,40 @@ def abstract_request(options)
else
res.error!
end
- end
-
+ end
- def fields_parameters
+ def field_parameters(fields)
+ out = []
+ fields.each{|f| out << f.name}
+ {"fields" => out}
end
- def conditions_parameters
+ def condition_parameters(conditions)
+ out = {}
+ conditions.each do |operator, prop, value|
+ out.merge!("#{prop.name}.#{operator}" => value)
+ end
+ out
end
- def value_parameters; end
-
- def order_parameters
+ def order_parameters(order)
+ out = []
+ order.each do |ord|
+ out << "#{ord.property.name}.#{ord.direction}"
+ end
+ {"order" => out}
end
- def links_parameters; end
-
- def group_by_paramters; end
-
- def property_to_column_name; end
-
- def quote_resource_name; end
+ def parse_results(data)
+ case @format
+ when :json
+ data.blank? ? {} : JSON.parse(data)
+ end
+ end
def resource_name(query)
query.model.storage_name(query.repository.name)
- end
-
- def url_for_resource
-
- end
-
+ end
def normalize_uri(uri_or_options)
if String === uri_or_options
@@ -141,6 +163,7 @@ def normalize_uri(uri_or_options)
port = uri_or_options.fetch(:port)
database = uri_or_options.fetch(:database)
scheme = uri_or_options.fetch(:scheme, "http")
+ @format = uri_or_options.fetch(:format, :json)
query = uri_or_options.to_a.map { |pair| pair.join('=') }.join('&')
query = nil if query == ""
View
85 merb-rest-client/spec/merb_rest_adapter_spec.rb
@@ -1,22 +1,33 @@
require File.join(File.dirname(__FILE__), "spec_helper")
+require 'dm-serializer'
+
describe "DataMapper::Adatapers::MerbRest" do
class Post
include DataMapper::Resource
+
+ def self.default_repository_name
+ :merb_rest
+ end
+
property :id, Serial
property :title, String
- property :body, Text
+ property :body, Text, :lazy => false
has n, :comments
end
class Comment
include DataMapper::Resource
+
+ def self.default_repository_name
+ :merb_rest
+ end
property :id, Serial
property :title, String
- property :body, Text
+ property :body, Text, :lazy => false
belongs_to :post
end
@@ -34,6 +45,10 @@ class Comment
Post.create(:title => "title #{n}", :body => "body #{n}")
end
end
+
+ before(:each) do
+
+ end
it "should handle ssl"
it "should setup an connection"
@@ -53,10 +68,56 @@ class Comment
end
describe "read_many" do
+ before(:all) do
+ @hash = {"title" => "title", "body" => "body", "id" => 3}
+ @json = JSON.generate(@hash)
+
+ end
+
+ 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(:read_many)}
- it "should send a get request to the Post resource"
- it "should send a get request to the Post resource with the requried parameters"
- it "should get all the objects"
+
+ it "should send a get request to the Post resource" do
+ @adapter.should_receive(:api_get).with("posts", {"order" => ["id.asc"], "fields" => [:id, :title, :body]}).and_return(@response)
+ Post.all.inspect
+ end
+
+ it "should return instantiated objects" do
+ @adapter.should_receive(:api_get).with("posts", {"order" => ["id.asc"], "fields" => [:id, :title, :body]}).and_return(@response)
+ @adapter.should_receive(:parse_results).and_return(@hash)
+ Post.all.inspect
+ end
+
+ it "should load all the objects" do
+ Post.all.each{|p| p.should be_a_kind_of(Post)}
+ end
+
+ describe "read many with conditions" do
+
+ it "should use a get with conditional parameters" do
+ @adapter.should_receive(:api_get).with("posts", { "title.like" => "tit%",
+ "body.eql" => "body",
+ "order" => ["id.asc"],
+ "fields" => [:id, :title, :body]
+ }).and_return(@response)
+ Post.all(:title.like => "tit%", :body.eql => "body").inspect
+ end
+
+ it "should add a fields option for fields" do
+ pending "There is an issue with specifying the :fields conditions with dm :|"
+ @adapter.should_receive(:api_get).with("posts", "title.like" => "tit%",
+ "fields" => [:body, :id],
+ "order" => ["id.asc"],
+ "fields" => [:id, :body]).and_return(@response)
+ Post.all(:title.like => "tit%", :fields => [:id, :body]).inspect
+ end
+ end
+
end
describe "read_one" do
@@ -92,6 +153,20 @@ class Comment
it "should get records with multiple matchers"
end
+ describe "formats" do
+ describe "json" do
+ before do
+ @post = Post.new(:title => "title", :body => "body", :id => 3)
+ @post_json = @post.to_json
+ end
+
+ it "should parse the json of an object" do
+ result = @adapter.send(:parse_results, @post_json)
+ result.should == {"title" => "title", "body" => "body", "id" => 3}
+ end
+ end
+ end
+
it "should order records"
it "should handle date/time"
it "should handle date"

0 comments on commit ec47e46

Please sign in to comment.
Something went wrong with that request. Please try again.