[英]Using a module's methods inside a class
I'm trying to integrate an API into a class but can't work out how I put the modules in place. 我正在尝试将API集成到类中,但无法弄清楚如何将模块放置到位。
class PlivoNumber < ActiveRecord::Base
require 'plivo'
include Plivo
def initialize_plivo
@p = RestAPI.new(ENV['PLIVO_AUTH_ID'], ENV['PLIVO_AUTH_TOKEN'])
end
def delete_number
self.initialize_plivo
params = {
'number' => self.number
}
response = @p.unrent_number(params)
end
I've tried both include and Extend 我已经尝试了包括和扩展
if I use in initialize 如果我在初始化中使用
self.RestAPI.new(ENV['PLIVO_AUTH_ID'], ENV['PLIVO_AUTH_TOKEN'])
NoMethodError: undefined method `RestAPI' for #<PlivoNumber:0x007f8eca9523f0>
if I use 如果我用
RestAPI.new(ENV['PLIVO_AUTH_ID'], ENV['PLIVO_AUTH_TOKEN'])
NameError: uninitialized constant PlivoNumber::RestAPI
Basically I want to be able to run @plivo_number.delete_number and have the app hit the api and perform the action. 基本上,我希望能够运行@ plivo_number.delete_number并让应用程序点击api并执行操作。 I appreciate that the initialize step not really doing anything with the class, but I can't do the next step without it.
我赞赏初始化步骤对类没有真正做任何事情,但是如果没有它,我将无法进行下一步。
Hope that makes some kind of sense, I get the impression that what I'm doing is probably a bit confused.... 希望这有道理,我给我的印象是我在做的事情可能有点混乱。
You should be able to access RestAPI
class after including Plivo
module. 包括
Plivo
模块后,您应该能够访问RestAPI
类。 Make sure you have installed plivo gem correctly. 确保正确安装了plivo gem。 Here is more rubyish version of your code:
这是您的代码的更多红宝石版本:
class PlivoNumber < ActiveRecord::Base
include Plivo
def delete_number
api.unrent_number('number' => number)
end
private
def api
@api ||= RestAPI.new(ENV['PLIVO_AUTH_ID'], ENV['PLIVO_AUTH_TOKEN'])
end
end
Also you don't need to include Plivo module into PlivoNumber class, you could just use Plivo::RestAPI instead. 同样,您不需要在PlivoNumber类中包含Plivo模块,您可以只使用Plivo :: RestAPI。
Just to explain Donatas' answer a bit further: 只是为了进一步解释Donatas的答案:
If you check plivo's source code ( https://github.com/plivo/plivo-ruby/blob/master/lib/plivo.rb ), you'll see a structure like: 如果检查plivo的源代码( https://github.com/plivo/plivo-ruby/blob/master/lib/plivo.rb ),则会看到类似以下的结构:
module Plivo
(...)
class RestAPI
(...)
end
(...)
end
In this scenario, if you include Plivo
, you'll get all classes defined inside it (including RestAPI) as if defined locally. 在这种情况下,如果
include Plivo
, include Plivo
获得在其中定义的所有类(包括RestAPI),就像在本地定义的一样。 So, you can access them directly, as in Donatas' code snippet: 因此,您可以直接访问它们,如Donatas的代码片段所示:
def api
@api ||= RestAPI.new(ENV['PLIVO_AUTH_ID'], ENV['PLIVO_AUTH_TOKEN'])
end
You could also, as he mentioned, just require Plivo
in any Ruby class, and you'll have access to the RestAPI through its namespaced path. 正如他提到的那样,您还可以只在任何Ruby类中
require Plivo
,并且可以通过其命名空间访问RestAPI。 In a controller, for example: 在控制器中,例如:
require 'plivo'
class OperationsController < ApplicationController
def plivo
plivo = Plivo::RestAPI.new(ENV['PLIVO_AUTH_ID'], ENV['PLIVO_AUTH_TOKEN'])
...
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.