如何从此对象获取数据?

时间:2013-05-16 11:44:42

标签: ruby-on-rails ruby object oauth

这是返回的对象。我最终需要得到两件事:@token@expires_at。我怎么能从中得到它们呢?

@my_object = access_token(params[:provider])

然后输出:

#<OAuth2::AccessToken:0x007fd03df75db8 
   @client=#<OAuth2::Client:0x007fd035040ad0 
      @id="123xyz", 
      @secret="xyz123", 
      @site="https://graph.facebook.com", 
      @options={
         :authorize_url=>"/oauth/authorize", 
         :token_url=>"oauth/access_token", 
         :token_method=>:post, 
         :connection_opts=>{
            :ssl=>{
               :ca_file=>"ca-bundle.crt"
            }
         }, 
         :connection_build=>nil, 
         :max_redirects=>5, 
         :raise_errors=>true, 
         :mode=>:query, 
         :param_name=>"access_token", 
         :parse=>:query
      }, 
      @auth_code=#<OAuth2::Strategy::AuthCode:0x007fd035040c88 
         @client=#<OAuth2::Client:0x007fd035040ad0 ...>>, 
         @connection=#<Faraday::Connection:0x007fd037aec978 
            @headers={"User-Agent"=>"Faraday v0.8.7"}, 
            @params={}, 
            @options={}, 
            @ssl={:ca_file=>"ca-bundle.crt"}, 
            @parallel_manager=nil, 
            @default_parallel_manager=nil, 
            @builder=#<Faraday::Builder:0x007fd037aeca68 
               @handlers=[Faraday::Request::UrlEncoded, Faraday::Adapter::NetHttp]>, 
               @url_prefix=#<URI::HTTPS:0x007fd037aecf90 URL:https://graph.facebook.com/>, 
                  @proxy=nil, 
                  @app=#<Faraday::Request::UrlEncoded:0x007fd037aeb3c0 
                     @app=#<Faraday::Adapter::NetHttp:0x007fd037aeb370 
                        @app=#<Proc:0x007fd037aeb2a8@/gems/faraday-0.8.7/lib/faraday/connection.rb:87 (lambda)>>>>>, 
                        @token="token_123", 
                        @refresh_token=nil, 
                        @expires_in=5149095, 
                        @expires_at=1373851292, 
                        @options={
                           :mode=>:query, 
                           :header_format=>"Bearer %s", 
                           :param_name=>"access_token"
                        }, 
                        @params={:token_url=>"oauth/access_token"}>

3 个答案:

答案 0 :(得分:2)

编辑它实际应该是

@my_object.token

@my_object.expires_at

我通过像这样重新缩进你的对象转储找到了:

#<OAuth2::AccessToken:0x007fd03df75db8 
  @client=#<OAuth2::Client:0x007fd035040ad0 
    @id="123xyz", 
    @secret="xyz123", 
    @site="https://graph.facebook.com", 
    @options={
      :authorize_url=>"/oauth/authorize", 
      :token_url=>"oauth/access_token", 
      :token_method=>:post, 
      :connection_opts=>{
        :ssl=>{
          :ca_file=>"ca-bundle.crt"
        }
      }, 
      :connection_build=>nil, 
      :max_redirects=>5, 
      :raise_errors=>true, 
      :mode=>:query, 
      :param_name=>"access_token", 
      :parse=>:query
    }, 
    @auth_code=#<OAuth2::Strategy::AuthCode:0x007fd035040c88 
      @client=#<OAuth2::Client:0x007fd035040ad0 ...>
    >, 
    @connection=#<Faraday::Connection:0x007fd037aec978 
      @headers={"User-Agent"=>"Faraday v0.8.7"}, 
      @params={}, 
      @options={}, 
      @ssl={:ca_file=>"ca-bundle.crt"}, 
      @parallel_manager=nil, 
      @default_parallel_manager=nil, 
      @builder=#<Faraday::Builder:0x007fd037aeca68 
        @handlers=[Faraday::Request::UrlEncoded, Faraday::Adapter::NetHttp]
      >, 
      @url_prefix=#<URI::HTTPS:0x007fd037aecf90 URL:https://graph.facebook.com/>, 
      @proxy=nil, 
      @app=#<Faraday::Request::UrlEncoded:0x007fd037aeb3c0 
        @app=#<Faraday::Adapter::NetHttp:0x007fd037aeb370 
          @app=#<Proc:0x007fd037aeb2a8@/gems/faraday-0.8.7/lib/faraday/connection.rb:87 (lambda)>
        >
      >
    >
  >, 
  @token="token_123", 
  @refresh_token=nil, 
  @expires_in=5149095, 
  @expires_at=1373851292, 
  @options={
    :mode=>:query, 
    :header_format=>"Bearer %s", 
    :param_name=>"access_token"
  }, 
  @params={:token_url=>"oauth/access_token"}
>

答案 1 :(得分:2)

基于this,您应该可以通过以下方式访问它们:

@my_object.token
@my_object.expires_at

答案 2 :(得分:1)

正如@grotori指出的那样,您应该查看the documentation而不是对象来定义如何访问对象属性。

请注意,对象中的实例变量通常与对象一起打印,但这并不意味着它位于类预期的接口中。

例如:

class A
  def initialize
    @foo = 'bar'
  end
end
 => nil 

# Have the @foo attribute
A.new
 => <A:0xa232904 @foo="bar">

# Can't access it
p A.new.foo
NoMethodError: undefined method `foo' for #<A:0xa230280 @foo="bar">