在前面的教程(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={}>
>>
本文介绍了一种利用Ruby元编程思想解决ActiveResource在处理多用户REST服务时的问题。通过创建特定用户的API模块,实现了不同用户对同一REST服务的安全访问。
7406

被折叠的 条评论
为什么被折叠?



