Skip to content

Instantly share code, notes, and snippets.

@paul
Created December 2, 2008 21:45
Show Gist options
  • Select an option

  • Save paul/31279 to your computer and use it in GitHub Desktop.

Select an option

Save paul/31279 to your computer and use it in GitHub Desktop.
http://core.ssbe.localhost/clients
{
"href": "http://core.ssbe.localhost/clients",
"item_count": 2,
"items": [
{
"_type": "Client",
"href": "http://core.ssbe.localhost/clients/API",
"id": "490a167b-c725-49e3-a917-2a5f5a486b4a",
"name": "API",
"parent_href": null,
"hosts_href": "http://core.ssbe.localhost/clients/API/hosts",
"created_at": "2000-01-01T00:00:00Z",
"updated_at": "2000-01-01T00:00:00Z"
}
,
{
"_type": "Client",
"href": "http://core.ssbe.localhost/clients/legacy_client",
"id": "1af03a8f-c708-4891-aa06-c0af27cef740",
"name": "legacy_client",
"parent_href": "http://core.ssbe.localhost/clients/API",
"hosts_href": "http://core.ssbe.localhost/clients/legacy_client/hosts",
"created_at": "2008-12-02T21:37:39Z",
"updated_at": "2008-12-02T21:37:39Z"
}
]
}
http://core.ssbe.localhost/service_descriptors
{
"href": "http://core.ssbe.localhost/service_descriptors",
"items": [
{
"_type": "ServiceDescriptor",
"href": "http://core.ssbe.localhost/service_descriptors/kernel",
"service_type": "http://systemshepherd.com/services/kernel"
}
,
{
"_type": "ServiceDescriptor",
"href": "http://core.ssbe.localhost/service_descriptors/measurements",
"service_type": "http://systemshepherd.com/services/measurements"
}
,
{
"_type": "ServiceDescriptor",
"href": "http://core.localhost/service_descriptors/message_queues",
"service_type": "http://systemshepherd.com/services/message-queues"
}
]
}
http://core.ssbe.localhost/service_descriptors/kernel
{
"_type" : "ServiceDescriptor",
"href" : "http://core.ssbe.localhost/service_descriptors/kernel",
"service_type" : "http://systemshepherd.com/services/kernel",
"items" : [
{
"_type" : "ResourceDescriptor",
"name" : "AllServiceDescriptors",
"href" : "http://core.ssbe.localhost/service_descriptors"
}
,
{
"_type" : "ResourceDescriptor",
"name" : "AllClients",
"href" : "http://core.ssbe.localhost/clients"
}
,
/* ... */
]
}
module DataMapper
module SsbeModelExtensions
# ...
attr_reader :collection_uri
def collection(uri, query = {})
begin
base_model.instance_variable_set(:@collection_uri, uri)
repository.read_many(Query.new(self.repository, self)).to_a
ensure
base_model.instance_variable_set(:@collection_uri, nil)
end
end
end
Model.send(:include, SsbeModelExtensions)
end
module DataMapper::Adapters
class SsbeAdapter
def read_many(query)
Collection.new(query) do |collection|
records = get_collection(query)
properties = query.fields
records.each do |record|
values = properties.map { |p| record[p.name] }
collection.load(values)
end
end
end
def get_collection(query)
model = query.model
resource = collection_resource_for(model)
response = resource.get
records = parse_collection(response.body)
records = query_records(records, query)
end
def collection_resource_for(model)
if uri = model.instance_variable_get(:@collection_uri)
http.resource(uri, :accept => ServiceIdentifiers[model.service_type].mime_type)
else
service_descriptor = service_descriptors[model.service_type]
resource_descriptor = service_descriptor[model.resource_name]
resource_descriptor.resource
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment