Permalink
Browse files

Conflicts:

	merb-rest-client/spec/merb_rest_adapter_spec.rb
  • Loading branch information...
Daniel Neighman
Daniel Neighman committed Jul 28, 2008
1 parent 86aff7e commit 0e2d79a2ca83dd3533f73b9da65995720b7dc86f
@@ -1,5 +1,6 @@
require 'dm-core'
require 'net/http'
+require 'net/https'
require 'uri'
require File.join(File.dirname(__FILE__), "merb_rest_adapter")
@@ -11,30 +11,33 @@ def initialize(name, uri_or_options)
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? }
- #
- # 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
+ 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 if successful?(result)
+ end
+ created
+ end
+
+ def update(attributes, query)
+ updated = 0
+ model = query.model
+ repository = query.repository
+ storage_name = resource_name(query).to_s
+ resources = read_many(query)
+ resources.each do |resource|
+ params = attributes_hash(attributes)
+ params.merge!(attributes_hash( resource.class.key.zip(resource.key)))
+
+ result = api_put(storage_name, params)
+ updated += 1 if successful?(result)
+ end
+ updated
end
def read_one(query)
@@ -46,7 +49,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 =[]
@@ -60,6 +62,17 @@ def read_many(query)
end
end
+ def delete(query)
+ deleted = 0
+ resources = read_many(query)
+ storage_name = resource_name(query).to_s
+ resources.each do |resource|
+ result = api_delete(storage_name, api_query_parameters(query))
+ deleted +=1 if successful?(result)
+ end
+ deleted > 0
+ end
+
protected
def api_get(path, options = {})
abstract_request(
@@ -112,6 +125,8 @@ def abstract_request(options)
req = klass.new(path)
req.basic_auth @uri.user, @uri.password
req.set_form_data(data, seperator)
+ req.use_ssl = true if @uri.scheme == "https"
+
res = Net::HTTP.new(@uri.host, @uri.port).start{|http| http.request(req)}
case res
when Net::HTTPSuccess, Net::HTTPRedirection
@@ -140,7 +155,7 @@ def field_parameters(fields)
def condition_parameters(conditions)
out = {}
conditions.each do |operator, prop, value|
- out.merge!("#{prop.name}.#{operator}" => value)
+ out.merge!("#{prop.name}.#{operator}" => value.to_s)
end
out
end
@@ -152,6 +167,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
@@ -169,25 +197,29 @@ def normalize_uri(uri_or_options)
uri_or_options = Addressable::URI.parse(uri_or_options)
end
if Addressable::URI === uri_or_options
- uri_or_options.scheme = "http"
+ uri_or_options.scheme = "http" if uri_or_options.scheme == "merb_rest"
return uri_or_options.normalize
end
-
- user = uri_or_options.fetch(:username)
- password = uri_or_options.fetch(:password)
- host = uri_or_options.fetch(:host, "")
- 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 == ""
-
+ opts = uri_or_options.dup
+ opts.delete(:adapter)
+ user = opts.delete(:username)
+ password = opts.delete(:password)
+ host = opts.delete(:host) || ""
+ database = opts.delete(:database) || ""
+ scheme = opts.delete(:scheme) || "http"
+ port = opts.delete(:port) || scheme == "https" ? 443 : 80
+ @format = opts.delete(:format) || :json
+ query = opts.to_a.map { |pair| pair.join('=') }.join('&')
+ query = nil if query.blank?
return Addressable::URI.new(
scheme, user, password, host, port, database, query, nil
)
end
+ def successful?(response)
+ (200..299).include?(response.code.to_i)
+ end
+
end # MerbDataMapperRest
end # Adapters
Oops, something went wrong.

0 comments on commit 0e2d79a

Please sign in to comment.