ruby-on-railsruby-on-rails-3

How to use a method in a lib?


I'm working to integrate UserVoice Single Single On with my rails app. They provide the following class for 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

What I can't figure out is how to use this. I added this file to my lib directory and am using Rails Console to run. I tried:

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

But can't get it to actually return for the options:

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

How can I actually use this?


Solution

  • Looking at the code, it doesn't appear that the initializer (what gets run when you call new) will take just a hash. The method definition looks like this:

    def initialize(key, api_key, data)
    

    And it seems to treat the data variable as a hash. You might just need to add the key and api_key values when you instantiate a Token. So a call would look like this:

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