Permalink
Browse files

Modified the configuration methods.

  • Loading branch information...
1 parent 4bbee60 commit d3708233c9ab1f28765739a1b8757a2d6440e23c @bananagi bananagi committed May 3, 2011
@@ -1,13 +1,14 @@
# -*- coding: utf-8 -*-
+# OAuth 関係設定.
module OpensocialWap
module Config
-
- # OAuth 関係設定.
- class OAuth
- def initialize
- yield self
- self.freeze
+ module OAuth
+ extend self
+
+ def configure(&blk)
+ instance_eval(&blk)
+ self
end
def helper_class(klass = nil)
@@ -1,13 +1,14 @@
# -*- coding: utf-8 -*-
+# Url 関係設定.
module OpensocialWap
module Config
+ module Url
+ extend self
- # Url 関係設定.
- class Url
- def initialize
- yield self
- self.freeze
+ def configure(&blk)
+ instance_eval(&blk)
+ self
end
def default(options = nil)
@@ -7,14 +7,6 @@ module Helpers
class BasicHelper < Base
DEFAULT_PROXY_CLASS = ::OpensocialWap::OAuth::RequestProxy::OAuthRackRequestProxy
-
- def self.setup(options)
- # Set class instance variables.
- options.each do |k, v|
- self.instance_variable_set "@#{k.to_s}", v
- end
- self
- end
def verify(options = nil)
request_proxy = self.class.proxy_class.new(@request)
@@ -54,21 +46,38 @@ def api_endpoint
self.class.api_endpoint
end
+ def self.configure(&blk)
+ instance_eval(&blk)
+ self
+ end
+
private
- def self.consumer_key
+ def self.consumer_key(arg = nil)
+ if arg
+ @consumer_key = arg
+ end
@consumer_key.dup if @consumer_key
end
- def self.consumer_secret
+ def self.consumer_secret(arg = nil)
+ if arg
+ @consumer_secret = arg
+ end
@consumer_secret.dup if @consumer_secret
end
- def self.api_endpoint
+ def self.api_endpoint(arg = nil)
+ if arg
+ @api_endpoint = arg
+ end
@api_endpoint.dup if @api_endpoint
end
- def self.proxy_class
+ def self.proxy_class(arg = nil)
+ if arg
+ @proxy_class = arg
+ end
@proxy_class || DEFAULT_PROXY_CLASS
end
@@ -20,7 +20,10 @@ def authorization_header(api_request, options = nil)
private
- def self.app_id
+ def self.app_id(arg = nil)
+ if arg
+ @app_id = arg
+ end
@app_id.dup if @app_id
end
end
@@ -7,21 +7,23 @@ def gree(config, &block)
@sandbox = false
instance_eval(&block)
- container_host = @sandbox ? 'mgadget-sb.gree.jp' : "mgadget.gree.jp"
- api_endpoint = @sandbox ? 'http://os-sb.gree.jp/api/rest/' : "http://os.gree.jp/api/rest/"
+ consumer_key = @consumer_key
+ consumer_secret = @consumer_secret
+ container_host = @sandbox ? 'mgadget-sb.gree.jp' : "mgadget.gree.jp"
+ api_endpoint = @sandbox ? 'http://os-sb.gree.jp/api/rest/' : "http://os.gree.jp/api/rest/"
- @config.opensocial_wap.oauth = OpensocialWap::Config::OAuth.new do |config|
- options = {
- :consumer_key => @consumer_key,
- :consumer_secret => @consumer_secret,
- :api_endpoint => api_endpoint,
- }
- config.helper_class OpensocialWap::OAuth::Helpers::BasicHelper.setup(options)
+ OpensocialWap::OAuth::Helpers::BasicHelper.configure do
+ consumer_key consumer_key
+ consumer_secret consumer_secret
+ api_endpoint api_endpoint
end
- @config.opensocial_wap.url = OpensocialWap::Config::Url.new do |config|
- config.default :format => :query, :params => { :guid => 'ON' }
- config.redirect :format => :local
- config.public_path :format => :local
+ @config.opensocial_wap.oauth = OpensocialWap::Config::OAuth.configure do
+ helper_class OpensocialWap::OAuth::Helpers::BasicHelper
+ end
+ @config.opensocial_wap.url = OpensocialWap::Config::Url.configure do
+ default :format => :query, :params => { :guid => 'ON' }
+ redirect :format => :local
+ public_path :format => :local
end
@config.opensocial_wap.session_id = @session ? :parameter : :cookie
end
@@ -7,21 +7,23 @@ def mixi(config, &block)
@access_from_pc = false
instance_eval(&block)
- container_host = @access_from_pc ? 'ma.test.mixi.net' : 'ma.mixi.net'
+ consumer_key = @consumer_key
+ consumer_secret = @consumer_secret
+ container_host = @access_from_pc ? 'ma.test.mixi.net' : 'ma.mixi.net'
- @config.opensocial_wap.oauth = OpensocialWap::Config::OAuth.new do |config|
- options = {
- :proxy_class => ::OpensocialWap::OAuth::RequestProxy::OAuthRackRequestProxyForMixi,
- :consumer_key => @consumer_key,
- :consumer_secret => @consumer_secret,
- :api_endpoint => 'http://api.mixi-platform.com/os/0.8/',
- }
- config.helper_class OpensocialWap::OAuth::Helpers::BasicHelper.setup(options)
+ OpensocialWap::OAuth::Helpers::BasicHelper.configure do
+ proxy_class OpensocialWap::OAuth::RequestProxy::OAuthRackRequestProxyForMixi
+ consumer_key consumer_key
+ consumer_secret consumer_secret
+ api_endpoint 'http://api.mixi-platform.com/os/0.8/'
end
- @config.opensocial_wap.url = OpensocialWap::Config::Url.new do |config|
- config.default :format => :query, :params => { :guid => 'ON' }
- config.redirect :format => :full, :container_host => container_host, :params => { :guid => 'ON' }
- config.public_path :format => :local
+ @config.opensocial_wap.oauth = OpensocialWap::Config::OAuth.configure do
+ helper_class OpensocialWap::OAuth::Helpers::BasicHelper
+ end
+ @config.opensocial_wap.url = OpensocialWap::Config::Url.configure do
+ default :format => :query, :params => { :guid => 'ON' }
+ redirect :format => :full, :container_host => container_host, :params => { :guid => 'ON' }
+ public_path :format => :local
end
@config.opensocial_wap.session_id = @session ? :parameter : :cookie
end
@@ -7,22 +7,25 @@ def mobage(config, &block)
@sandbox = false
instance_eval(&block)
- container_host = @sandbox ? 'sb.pf.mbga.jp' : 'pf.mbga.jp'
- api_endpoint = @sandbox ? 'http://sb.app.mbga.jp/api/restful/v1/' : "http://app.mbga.jp/api/restful/v1/"
+ consumer_key = @consumer_key
+ consumer_secret = @consumer_secret
+ app_id = @app_id
+ container_host = @sandbox ? 'sb.pf.mbga.jp' : 'pf.mbga.jp'
+ api_endpoint = @sandbox ? 'http://sb.app.mbga.jp/api/restful/v1/' : "http://app.mbga.jp/api/restful/v1/"
- @config.opensocial_wap.oauth = OpensocialWap::Config::OAuth.new do |config|
- options = {
- :consumer_key => @consumer_key,
- :consumer_secret => @consumer_secret,
- :api_endpoint => api_endpoint,
- :app_id => @app_id
- }
- config.helper_class OpensocialWap::OAuth::Helpers::MobageHelper.setup(options)
+ OpensocialWap::OAuth::Helpers::MobageHelper.configure do
+ consumer_key consumer_key
+ consumer_secret consumer_secret
+ api_endpoint api_endpoint
+ app_id app_id
end
- @config.opensocial_wap.url = OpensocialWap::Config::Url.new do |config|
- config.default :format => :query, :params => { :guid => 'ON' }
- config.redirect :format => :local
- config.public_path :format => :local
+ @config.opensocial_wap.oauth = OpensocialWap::Config::OAuth.configure do
+ helper_class OpensocialWap::OAuth::Helpers::MobageHelper
+ end
+ @config.opensocial_wap.url = OpensocialWap::Config::Url.configure do
+ default :format => :query, :params => { :guid => 'ON' }
+ redirect :format => :local
+ public_path :format => :local
end
@config.opensocial_wap.session_id = @session ? :parameter : :cookie
end
@@ -20,7 +20,6 @@ class Railtie < Rails::Railtie
config.opensocial_wap = ActiveSupport::OrderedOptions.new
initializer "opensocial-wap.initialize" do |app|
- # do something..
end
initializer 'opensocial-wap.load_middleware', :after=> :load_config_initializers do
View
@@ -4,13 +4,19 @@ module OpensocialWap
module Config
describe OAuth do
it do
- config = ::OpensocialWap::Config::OAuth.new do |config|
- config.helper_class "OAuth Helper Class"
+ config = OpensocialWap::Config::OAuth.configure do
+ OpensocialWap::OAuth::Helpers::BasicHelper.configure do
+ consumer_key '1234'
+ consumer_secret 'abcd'
+ api_endpoint 'http://api.example.com/'
+ end
+ helper_class OpensocialWap::OAuth::Helpers::BasicHelper
end
- config.helper_class.should == "OAuth Helper Class"
-
- lambda{config.helper_class "Other Class"}.should raise_error(Exception)
+ config.helper_class.should == OpensocialWap::OAuth::Helpers::BasicHelper
+ config.helper_class.consumer_key.should == '1234'
+ config.helper_class.consumer_secret.should == 'abcd'
+ config.helper_class.api_endpoint.should == 'http://api.example.com/'
end
end
end
View
@@ -3,20 +3,17 @@
module OpensocialWap
module Config
describe Url do
+
it do
- config = ::OpensocialWap::Config::Url.new do |config|
- config.default :format => :full, :container_host => 'c.example.com', :params => { :guid => 'ON' }
- config.redirect :format => :query, :params => { :guid => 'ON' }
- config.public_path :format => :plain
+ config = ::OpensocialWap::Config::Url.configure do
+ default :format => :full, :container_host => 'c.example.com', :params => { :guid => 'ON' }
+ redirect :format => :query, :params => { :guid => 'ON' }
+ public_path :format => :plain
end
config.default.should == {:format => :full, :container_host => 'c.example.com', :params => { :guid => 'ON' }}
config.redirect.should == { :format => :query, :params => { :guid => 'ON' }}
config.public_path.should == { :format => :plain }
-
- lambda{config.default :format => :plain }.should raise_error(Exception)
- lambda{config.redirect :format => :plain }.should raise_error(Exception)
- lambda{config.public_path :format => :plain }.should raise_error(Exception)
end
end
end
@@ -6,11 +6,11 @@
describe "#uri" do
it "構築したAPIエンドポイントが正しいこと" do
- OpensocialWap::OAuth::Helpers::BasicHelper.setup(
- :consumer_key => 'abcde',
- :consumer_secret => 'fghijk',
- :api_endpoint => 'http://api.example.com/rest/'
- )
+ OpensocialWap::OAuth::Helpers::BasicHelper.configure do
+ consumer_key 'abcde'
+ consumer_secret 'fghijk'
+ api_endpoint 'http://api.example.com/rest/'
+ end
oauth_helper = OpensocialWap::OAuth::Helpers::BasicHelper.new
client_helper = OpensocialWap::OAuth::ClientHelper.new(oauth_helper,
'people',
@@ -8,8 +8,12 @@
env = ::Rack::MockRequest.env_for('http://example.com/?opensocial_app_id=877&opensocial_owner_id=23&sample_key=sample_value')
request = ::Rack::Request.new(env)
- oauth_helper = OpensocialWap::OAuth::Helpers::BasicHelper.setup(:consumer_key => 'sample_consumer_key',
- :consumer_secret => 'sample_consumer_secret').new(request)
+ OpensocialWap::OAuth::Helpers::BasicHelper.configure do
+ consumer_key 'sample_consumer_key'
+ consumer_secret 'sample_consumer_secret'
+ end
+ oauth_helper = OpensocialWap::OAuth::Helpers::BasicHelper.new(request)
+
result = oauth_helper.verify
result.should be_false
@@ -22,8 +26,12 @@
'HTTP_AUTHORIZATION' => http_oauth_header('GET'))
request = ::Rack::Request.new(env)
- oauth_helper = OpensocialWap::OAuth::Helpers::BasicHelper.setup(:consumer_key => 'sample_consumer_key',
- :consumer_secret => 'sample_consumer_secret').new(request)
+ OpensocialWap::OAuth::Helpers::BasicHelper.configure do
+ consumer_key 'sample_consumer_key'
+ consumer_secret 'sample_consumer_secret'
+ end
+ oauth_helper = OpensocialWap::OAuth::Helpers::BasicHelper.new(request)
+
result = oauth_helper.verify
result.should be_true
@@ -35,16 +43,17 @@
request = ::Rack::Request.new(env)
# invalid consumer secret
- oauth_helper = OpensocialWap::OAuth::Helpers::BasicHelper.setup(:consumer_key => 'sample_consumer_key',
- :consumer_secret => 'foobar').new(request)
+ OpensocialWap::OAuth::Helpers::BasicHelper.configure do
+ consumer_key 'sample_consumer_key'
+ consumer_secret 'foobar'
+ end
+ oauth_helper = OpensocialWap::OAuth::Helpers::BasicHelper.new(request)
result = oauth_helper.verify
result.should be_false
end
end
-
-
context "a normal (oauth NOT signed) post request from sns" do
it "must fail to verify" do
# without http authorization header
@@ -53,8 +62,11 @@
:params => {'post_sample_key'=>'post_sample_value'})
request = ::Rack::Request.new(env)
- oauth_helper = OpensocialWap::OAuth::Helpers::BasicHelper.setup(:consumer_key => 'sample_consumer_key',
- :consumer_secret => 'sample_consumer_secret').new(request)
+ OpensocialWap::OAuth::Helpers::BasicHelper.configure do
+ consumer_key 'sample_consumer_key'
+ consumer_secret 'sample_consumer_secret'
+ end
+ oauth_helper = OpensocialWap::OAuth::Helpers::BasicHelper.new(request)
result = oauth_helper.verify
result.should be_false
@@ -69,8 +81,11 @@
'HTTP_AUTHORIZATION' => http_oauth_header('POST', {'post_sample_key'=>'post_sample_value'}))
request = ::Rack::Request.new(env)
- oauth_helper = OpensocialWap::OAuth::Helpers::BasicHelper.setup(:consumer_key => 'sample_consumer_key',
- :consumer_secret => 'sample_consumer_secret').new(request)
+ OpensocialWap::OAuth::Helpers::BasicHelper.configure do
+ consumer_key 'sample_consumer_key'
+ consumer_secret 'sample_consumer_secret'
+ end
+ oauth_helper = OpensocialWap::OAuth::Helpers::BasicHelper.new(request)
result = oauth_helper.verify
result.should be_true
@@ -84,8 +99,11 @@
request = ::Rack::Request.new(env)
# invalid consumer secret
- oauth_helper = OpensocialWap::OAuth::Helpers::BasicHelper.setup(:consumer_key => 'sample_consumer_key',
- :consumer_secret => 'foobar').new(request)
+ OpensocialWap::OAuth::Helpers::BasicHelper.configure do
+ consumer_key 'sample_consumer_key'
+ consumer_secret 'foobar'
+ end
+ oauth_helper = OpensocialWap::OAuth::Helpers::BasicHelper.new(request)
result = oauth_helper.verify
result.should be_false
Oops, something went wrong.

0 comments on commit d370823

Please sign in to comment.