在哪里放置身份验证信息

时间:2013-04-30 15:33:18

标签: ruby authentication user-interface twitter rubygems

所以我在VisualRuby中创建了一个可以推文的小程序。所以这就是我main.rb的样子:

#!/usr/bin/ruby

require 'vrlib'
require 'twitter_oauth'

#make program output in real time so errors visible in VR.
STDOUT.sync = true
STDERR.sync = true

#everything in these directories will be included
my_path = File.expand_path(File.dirname(__FILE__))
require_all Dir.glob(my_path + "/bin/**/*.rb") 

LoginWindow.new.show

我的LoginWindow.rb看起来像这样

require 'twitter_oauth'

class LoginWindow #(change name)

    include GladeGUI

    client = TwitterOAuth::Client.new(
        :consumer_key => '****',
        :consumer_secret => '****',
        :token => '****-****',
        :secret => '****'
    )

    def show()
        load_glade(__FILE__)  #loads file, glade/MyClass.glade into @builder
        set_glade_all() #populates glade controls with insance variables (i.e. Myclass.label1) 
        show_window() 
    end 

    def button1__clicked(*argv)
        if client.authorized? 
            puts "true"
        end
    end

end

最后我的窗口看起来像这样:
enter image description here

现在,当我运行此操作并单击登录按钮时,VR会将其吐出

C:/Users/*/visualruby/Test/bin/LoginWindow.rb:22:in `button1__clicked': undefined local variable or method `client' for #<LoginWindow:0x3f56aa8>
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:146:in `call'
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:146:in `block (3 levels) in parse_signals'
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:331:in `call'
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:331:in `main'
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:331:in `show_window'
     from C:/Users/*/visualruby/Test/bin/LoginWindow.rb:17:in `show'
     from main.rb:14:in `<main>'

我认为我不应该将client = Twitter....内容放在LoginWindow课程中,但我想不出其他任何内容。关于如何解决这个问题的任何想法?

1 个答案:

答案 0 :(得分:2)

这是您所需要的快速解决方案。 在您的LoginWindow.rb

def initialize
   @client = TwitterOAuth::Client.new( 
       :consumer_key => '****',
       :consumer_secret => '****',
       :token => '****-****',
       :secret => '****'
   )
end


def button1__clicked(*argv)
    if @client.authorized? 
        puts "true"
    end
end

这个解决方案的问题是你现在不能在没有初始化LogginWindow的情况下调用button1_clicked,所以要小心。