我正在尝试在我的应用中设置Zendesk API,我决定使用API that was built by Zendesk
我已经设置了初始化对象来加载客户端。
config/initializers/zendesk.rb
require 'zendesk_api'
client = ZendeskAPI::Client.new do |config|
# Mandatory:
config.url = Rails.application.secrets[:zendesk][:url]
# Basic / Token Authentication
config.username = Rails.application.secrets[:zendesk][:username]
config.token = Rails.application.secrets[:zendesk][:token]
# Optional:
# Retry uses middleware to notify the user
# when hitting the rate limit, sleep automatically,
# then retry the request.
config.retry = true
# Logger prints to STDERR by default, to e.g. print to stdout:
require 'logger'
config.logger = Logger.new(STDOUT)
# Changes Faraday adapter
# config.adapter = :patron
# Merged with the default client options hash
# config.client_options = { :ssl => false }
# When getting the error 'hostname does not match the server certificate'
# use the API at https://yoursubdomain.zendesk.com/api/v2
end
这几乎是来自网站的复制粘贴,但我决定使用token
+ username
组合。
然后我创建了一个服务对象,我传递了一个JSON对象并让它构造了票证。从控制器调用此服务对象。
app/services/zendesk_notifier.rb
class ZendeskNotifier
attr_reader :data
def initialize(data)
@data = data
end
def create_ticket
options = {:comment => { :value => data[:reasons] }, :priority => "urgent" }
if for_operations?
options[:subject] = "Ops to get additional info for CC"
options[:requester] = { :email => 'originations@testing1.com' }
elsif school_in_usa_or_canada?
options[:subject] = "SRM to communicate with student"
options[:requester] = { :email => 'srm@testing2.com' }
else
options[:subject] = "SRM to communicate with student"
options[:requester] = { :email => 'srm_row@testing3.com' }
end
ZendeskAPI::Ticket.create!(client, options)
end
private
def for_operations?
data[:delegate] == 1
end
def school_in_usa_or_canada?
data[:campus_country] == "US" || "CA"
end
end
但现在我正在
NameError - undefined local variable or method `client' for #<ZendeskNotifier:0x007fdc7e5882b8>:
app/services/zendesk_notifier.rb:20:in `create_ticket'
app/controllers/review_queue_applications_controller.rb:46:in `post_review'
我认为客户端与我的配置初始化程序中定义的客户端相同。不知怎的,我认为这是一个不同的对象。我已经尝试查看他们的文档以获取更多信息,但我很遗憾这是什么?
答案 0 :(得分:3)
如果您想使用初始值设定项中定义的CREATE FUNCTION [RegexExtract](@input [nvarchar](4000), @pattern [nvarchar](4000), @group [nvarchar](4000), @mask [tinyint])
RETURNS [nvarchar](4000) WITH EXECUTE AS CALLER, RETURNS NULL ON NULL INPUT
AS
EXTERNAL NAME [Microsoft.MasterDataServices.DataQuality].[Microsoft.MasterDataServices.DataQuality.SqlClr].[RegexExtract]
GO
,则需要将其更改为client
,以使其全局化。目前,您已将其设置为本地变量。
答案 1 :(得分:2)
我使用了稍微不同的初始化客户端的方法,使用标准Zendesk API gem从此示例rails app复制: https://github.com/devarispbrown/zendesk_help_rails/blob/master/app/controllers/application_controller.rb
正如danielrsmith指出的那样,client
变量超出了范围。你可以改为使用这样的初始化器:
配置/初始化/ zendesk_client.rb:
class ZendeskClient < ZendeskAPI::Client
def self.instance
@instance ||= new do |config|
config.url = Rails.application.secrets[:zendesk][:url]
config.username = Rails.application.secrets[:zendesk][:username]
config.token = Rails.application.secrets[:zendesk][:token]
config.retry = true
config.logger = Logger.new(STDOUT)
end
end
end
然后通过client = ZendeskClient.instance
将客户端返回到其他位置(为简洁起见):
应用程序/服务/ zendesk_notifier.rb:
class ZendeskNotifier
attr_reader :data
def initialize(data)
@data = data
@client = ZendeskClient.instance
end
def create_ticket
options = {:comment => { :value => data[:reasons] }, :priority => "urgent" }
...
ZendeskAPI::Ticket.create!(@client, options)
end
...
end
希望这有帮助。