Permalink
Browse files

Service discovery and stuff

  • Loading branch information...
1 parent 052dd93 commit d25e250d66247aafe116d9b3c67568f6392fa453 @paul committed Dec 15, 2011
View
55 .rvmrc
@@ -0,0 +1,55 @@
+#!/usr/bin/env bash
+
+# This is an RVM Project .rvmrc file, used to automatically load the ruby
+# development environment upon cd'ing into the directory
+
+# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
+environment_id="ruby-1.9.3-p0@datapathy"
+
+#
+# Uncomment following line if you want options to be set only for given project.
+#
+# PROJECT_JRUBY_OPTS=( --1.9 )
+
+#
+# First we attempt to load the desired environment directly from the environment
+# file. This is very fast and efficient compared to running through the entire
+# CLI and selector. If you want feedback on which environment was used then
+# insert the word 'use' after --create as this triggers verbose mode.
+#
+if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
+then
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
+
+ if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
+ then
+ . "${rvm_path:-$HOME/.rvm}/hooks/after_use"
+ fi
+else
+ # If the environment file has not yet been created, use the RVM CLI to select.
+ if ! rvm --create "$environment_id"
+ then
+ echo "Failed to create RVM environment '${environment_id}'."
+ exit 1
+ fi
+fi
+
+#
+# If you use an RVM gemset file to install a list of gems (*.gems), you can have
+# it be automatically loaded. Uncomment the following and adjust the filename if
+# necessary.
+#
+# filename=".gems"
+# if [[ -s "$filename" ]]
+# then
+# rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
+# fi
+
+# If you use bundler, this might be useful to you:
+# if command -v bundle && [[ -s Gemfile ]]
+# then
+# bundle install
+# fi
+
+
View
29 README.mkd
@@ -1,19 +1,34 @@
datapathy
=========
-The stupid-simple ORM
+The stupid-simple ORM as an HTTP client.
-Roadmap
-=======
+Examples
+========
- * 0.1.0 - CRUD API in adapters & models completed
- * 0.2.0 - Lazy Collections
- * 0.3.0 - Validations & Errors
+ class Post
+ include Datapathy::Model
+
+ # Using service discovery
+ self.service_name = "AllPosts"
+ # Using manual URLs. Boo.
+ self.service_href = "http://example.com/posts{?author_href,q}
+
+ persists :title, :body, :author_href, :comments_href
+
+ links :author
+ links :comments
+
+ end
+
+ Post.href #=> "http://example.com/posts"
+ Post.select { |p| p.author = author }.href
+ # => "http://example.com/posts?author_href=http://example.com/authors/1"
Copyright
=========
-Copyright (c) 2009 Paul Sadauskas. See LICENSE for details.
+Copyright (c) 2011 Paul Sadauskas. See LICENSE for details.
View
51 resourceful_services.rb
@@ -0,0 +1,51 @@
+
+datapathy = Datapathy::Connection.new("http://api.ls.com") do |conn|
+ conn.add_authenticator LivingSocial::Authenticator.new("token", AUTH_TOKEN)
+end
+
+post = datapathy.resource("/posts/1")
+post = datapathy.discover("LatestPost")
+
+posts = datapathy.collection("/posts")
+posts = datapathy.discover_collection("AllPosts")
+
+posts.members # Triggers http request
+# GET /posts
+#
+# {
+# href: "/posts",
+# _type: "Collection",
+# version: "...",
+# members: [
+# {
+# _type: "Post",
+# href: "/posts/stuff",
+# title: "Stuff",
+# body: "Stuff is cool!",
+# author_href: "api.people.ls.com/people/paul",
+# comments_href: "/posts/stuff/comments"
+# },
+# // ..
+# ]
+# }
+
+post = posts.detect { |p| p.title == "Stuff" }
+
+class Post
+ include Datapathy::Model
+
+ service_name "AllPosts"
+ # OR
+ service_uri "http://api.ls.com/posts{?author_href,q}"
+
+ persists :title, :body, :author_href, :comments_href
+
+ links :author
+ links :comments
+
+end
+
+post = Post.detect { |p| p.author == some_author } # GET /posts?author_href=/authors/1234
+posts = Post.select { |p| p.q == "Stuff" } # GET /posts?q=Stuff
+posts = Post.select { |p| p.title == "Stuff" } # GET /posts (since `title` isn't a available query param, fall back to Enumerable#select)
+
View
24 spec/integration/query_params_spec.rb
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+describe "URI-Templated query params" do
+ before do
+ Datapathy.configure do |config|
+ config.services_uri = "http://datapathy.dev/"
+ end
+
+ Artifice.activate_with(DatapathyTestApp)
+ end
+
+ after do
+ Artifice.deactivate
+ end
+
+ it "should translate filterable attributes to query params" do
+ comments = Comments.select { |c| c.author_href = "/authors/1" }
+ comments.href.should == "/comments?author_href=/authors/1"
+ end
+
+end
+
+
+
View
6 spec/integration/service_spec.rb
@@ -18,5 +18,11 @@
services.size.should == 2
end
+ describe "Discovery" do
+
+ it "should discover the href" do
+ Post.href.should == "/posts"
+ end
+ end
end
View
14 spec/support/datapathy_test_app.rb
@@ -13,7 +13,8 @@ class DatapathyTestApp < Sinatra::Base
:href => '/services',
:members => [
{ :name => "Services", :href => '/services' },
- { :name => "Posts", :href => '/posts' }
+ { :name => "Posts", :href => '/posts' },
+ { :name => "Comments", :href => '/comments{?author_href,date}' }
]
})
end
@@ -39,6 +40,17 @@ class DatapathyTestApp < Sinatra::Base
})
end
+ get "/comments" do # Test uri templated query params
+ headers "Content-Type" => "application/json"
+ json({
+ :href => "/comments",
+ :query => params,
+ :members => [
+ {:href => '/comments/1', :title => 'Buy Viagra Now!!!'}
+ ]
+ })
+ end
+
protected
View
8 spec/support/models.rb
@@ -6,6 +6,13 @@ class Post
end
+class Comments
+ include Datapathy::Model
+ self.service_name = "Comments"
+
+ persists :content
+end
+
class Article
include Datapathy::Model
@@ -28,5 +35,4 @@ class Person
persists :id, :name
validates_presence_of :name
-
end

0 comments on commit d25e250

Please sign in to comment.