Skip to content

Commit

Permalink
Create resources through an association
Browse files Browse the repository at this point in the history
  • Loading branch information
paul committed Oct 8, 2009
1 parent 3a22d38 commit 764b6d6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
13 changes: 10 additions & 3 deletions lib/datapathy-ssbe-adapter.rb
Expand Up @@ -30,16 +30,23 @@ def initialize(options = {})
end

def create(resources)
if resources.is_a?(Datapathy::Collection)
query = resources.query
end

resources.each do |resource|
http_resource = http_resource_for(resource)
http_resource = http_resource_for(query || resource)
record = serialize(resource)
content_type = ServiceIdentifiers[resource.model.service_type].mime_type

begin
response = http_resource.post(record, "Content-Type" => content_type)
resource.merge!(deserialize(response))
resource.key = response.header['Location']
resource.merge!(deserialize(response)) unless response.body.blank?
rescue Resourceful::UnsuccessfulHttpRequestError => e
puts "ERRORS! #{e.inspect}"
# TODO check for invalid record, and populate errors
#puts "ERRORS! #{e.inspect}"
raise e
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/ssbe_model.rb
@@ -1,3 +1,4 @@
require 'addressable/template'

class SsbeModel
include Datapathy::Model
Expand Down Expand Up @@ -33,9 +34,9 @@ def self.at(href)
end

# Get a collection from a location other than the default
def self.from(href)
def self.from(href, mappings = {})
query = SsbeConsole::SsbeQuery.new(model)
query.location = href
query.location = Addressable::Template.new(href).expand(mappings.stringify_keys).to_str
Datapathy::Collection.new(query)
end

Expand Down

0 comments on commit 764b6d6

Please sign in to comment.