小编Rob*_*yle的帖子

如何从模块中引用的Class Constant数组中编写元编程方法?

我编写了一个基本的Eventable模块,允许其他类为自己创建事件.所有模型都有自己的一组事件,例如Message存在sentUsersign_in事件.我想在类级别创建一个事件数组作为常量,并访问模块内部的数组,以构建辅助方法来创建如下事件:

class User
  EVENT_TYPES = ['sign_in', 'sign_out']
  include Eventable
end
Run Code Online (Sandbox Code Playgroud)
class Message
  EVENT_TYPES = ['sent', 'opened']
  include Eventable
end
Run Code Online (Sandbox Code Playgroud)
module Eventable
  extend ActiveSupport::Concern

  included do
    has_many :events, as: :eventable
    attr_accessor :metadata

    self::EVENT_TYPES.each do |event_type|
      define_method "create_#{event_type}_event" do |args={}|
        Event.create(eventable: self, event_type: event_type)
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

这个问题是:方法没有正确定义.它无法EVENT_TYPES正确引用相应模型的常量.

如何从模块中获取类常量?

ruby inheritance ruby-on-rails

5
推荐指数
1
解决办法
156
查看次数

如何使用OAuth 2.0为Ruby中的google预测API进行授权?

我试图简单地向google的托管模型sample.sentiment发送请求.我无法弄清楚如何通过谷歌使用Oauth 2.0进行授权,而且我正在获得无穷无尽的时间.如果你可以提供我的代码,这将是有帮助的.这就是我的工作方式.

client = Google::APIClient.new({:application_name => "CCE",:application_version => "1.0"} )
plus = client.discovered_api('prediction')

# Initialize OAuth 2.0 client    
client.authorization.client_id = 'my client id'
client.authorization.client_secret = 'my client secret'
client.authorization.redirect_uri = 'my callback url'

client.authorization.scope = 'https://www.googleapis.com/auth/prediction'

# Request authorization
redirect_uri = client.authorization.authorization_uri

# Wait for authorization code then exchange for token
client.authorization.code = '....'
client.authorization.fetch_access_token!

# Make an API call
 result = client.execute(
   :api_method => plus.activities.list,
   :parameters => {'hostedModelName' => 'sample.sentiment', 'userId' => ''})
Run Code Online (Sandbox Code Playgroud)

`

ruby api google-api

2
推荐指数
1
解决办法
1847
查看次数

标签 统计

ruby ×2

api ×1

google-api ×1

inheritance ×1

ruby-on-rails ×1