如何在lib中使用方法?

时间:2013-03-11 00:42:33

标签: ruby-on-rails ruby-on-rails-3

我正在努力将UserVoice Single Single On与我的rails应用程序集成。他们为ruby提供以下课程:

require 'rubygems'
require 'ezcrypto'
require 'json'
require 'cgi'
require 'base64'

module Uservoice
  class Token
    attr_accessor :data

    USERVOICE_SUBDOMAIN = "FILL IN"
    USERVOICE_SSO_KEY = "FILL IN"

    def initialize(options = {})
      options.merge!({:expires => (Time.zone.now.utc + 5 * 60).to_s})

      key = EzCrypto::Key.with_password USERVOICE_SUBDOMAIN, USERVOICE_SSO_KEY
      encrypted = key.encrypt(options.to_json)
      @data = Base64.encode64(encrypted).gsub(/\n/,'') # Remove line returns where are annoyingly placed every 60 characters
    end

    def to_s
      @data
    end
  end
end

我无法弄清楚如何使用它。我将此文件添加到我的lib目录并使用Rails控制台运行。我试过了:

1.9.3-p125 :013 > Uservoice::Token
 => Uservoice::Token 

但无法让它实际返回选项:

Uservoice::Token.new(:guid => 1, :display_name => "jeff goldmen", :email => "jeff@google.com")

任何想法如何实际使用它?感谢

1 个答案:

答案 0 :(得分:2)

查看code,初始化程序(当您调用new时运行的内容)将只显示哈希值。方法定义如下所示:

def initialize(key, api_key, data)

它似乎将data变量视为哈希值。在实例化令牌时,您可能只需要添加keyapi_key值。所以电话会是这样的:

Uservoice::Token.new(KEY, API_KEY, {guid:1, display_name:'foo', email:'f@b.com'})