2008-02-14
ActiveResource探究(二)--多用户的客户端
在前面的教程(ActiveResource探究(一))中,在服务器端每个用户都很直接,但是处理多用户的时候就会遇到麻烦,假如我有两个servers,一个处理REAST service,另一个处理html文件。但是ActiveResource假设认证实在类的层面上处理。这个时候ruby的元编程思想可以解决问题!
使用前面的例子,我扩展客户端如下:
require 'activeresource' module Sample module Client class API # # Creates a module that serves as an ActiveResource # client for the specified user # def self.create_api(login = nil, password = nil) # specify the site. Default to no credentials @url_base = "http://localhost:3000" @url_base = "http://#{login}:#{password}@localhost:3000" if login # build a module name. This assumes that logins are unique. # it also assumes they're valid ruby module names when capitalized @module = login ? login.capitalize : "Default" class_eval <<-"end_eval",__FILE__, __LINE__ module #{@module} class Post < ActiveResource::Base self.site = "#{@url_base}" end class Comment < ActiveResource::Base self.site = "#{@url_base}/posts/:post_id" end # return the module, not the last site String self end end_eval end end end end
这次允许我们为特定的用户创建特定的api,这样多个用户都可以链接到rest service了,来看看吧:
>> require 'ares_sample_client'
=> ["Sample"]
>> api = Sample::Client::API.create_api
=> Sample::Client::API::Default
>> p = api::Post.find(1)
=> #<Sample::Client::API::Default::Post:0xb715af74 @attributes={"updated_at"=>Wed Jan 09 02:36:34 UTC 2008, "id"=>1, "content"=>"The first post", "user_id"=>1, "created_at"=>Wed Jan 09 02:36:34 UTC 2008}, @prefix_options={}>
>> p = api::Post.create(:content => "should fail")
ActiveResource::UnauthorizedAccess: Failed with 401 Unauthorized
from /usr/lib/ruby/gems/1.8/gems/activeresource-2.0.2/lib/active_resource/connection.rb:125:in `handle_response'
from /usr/lib/ruby/gems/1.8/gems/activeresource-2.0.2/lib/active_resource/connection.rb:112:in `request'
from /usr/lib/ruby/gems/1.8/gems/activeresource-2.0.2/lib/active_resource/connection.rb:101:in `post'
from /usr/lib/ruby/gems/1.8/gems/activeresource-2.0.2/lib/active_resource/base.rb:803:in `create'
from /usr/lib/ruby/gems/1.8/gems/activeresource-2.0.2/lib/active_resource/base.rb:636:in `save_without_validation'
from /usr/lib/ruby/gems/1.8/gems/activeresource-2.0.2/lib/active_resource/validations.rb:262:in `save'
from /usr/lib/ruby/gems/1.8/gems/activeresource-2.0.2/lib/active_resource/base.rb:339:in `create'
from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/core_ext/object/misc.rb:28:in `returning'
from /usr/lib/ruby/gems/1.8/gems/activeresource-2.0.2/lib/active_resource/base.rb:339:in `create'
from (irb):4
>> auth_api = Sample::Client::API.create_api('test1','test1')
=> Sample::Client::API::Test1
>> p = auth_api::Post.find(1)
=> #<Sample::Client::API::Default::Post:0xb713dde8 @attributes={"updated_at"=>Wed Jan 09 02:36:34 UTC 2008, "id"=>1, "content"=>"The first post", "user_id"=>1, "created_at"=>Wed Jan 09 02:36:34 UTC 2008}, @prefix_options={}>
>> p = auth_api::Post.create(:content => "should succeed!")
=> #<Sample::Client::API::Test1::Post:0xb713312c @attributes={"updated_at"=>Thu Jan 10 04:01:53 UTC 2008, "id"=>7, "content"=>"should succeed!", "user_id"=>nil, "created_at"=>Thu Jan 10 04:01:53 UTC 2008}, @prefix_options={}>
>>发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 7621 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
链接
最新评论
-
[转贴]关于豆瓣网的调查 ...
我也做了个网站。呵呵 ,也有读书,歌曲,交流等,还有小组,心情。。。www.fy ...
-- by 木石流云 -
Cache_fu插件使用方法(翻 ...
把 reset_cache 当作一个异步操作来用。这样虽然可能会牺牲页面的及时刷 ...
-- by rubyonrailscn.org -
Cache_fu插件使用方法(翻 ...
reset_cache:重置缓存(这段不知道改怎么翻译了:reset_cache ...
-- by rubyonrailscn.org -
ActiveResource探究(一) ...
没有错阿,我是想加上原文地址的,但是当我翻译的时候原文已经打不开了。 我想这就是 ...
-- by weskycn -
ActiveResource探究(一) ...
翻译的还不错
-- by blackanger






评论排行榜