Ruby Rails,Zendesk API 集成未加载客户端
Ruby on Rails, Zendesk API integration not loading the client
我正在尝试在我的应用程序中设置 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'
我认为客户端与我的配置初始值设定项中定义的相同。不知何故,我认为现在这是一个不同的对象。我曾尝试查看他们的文档以获取更多信息,但我不知道这是什么?
如果您想使用初始化程序中定义的 client
,您需要通过将其更改为 $client
使其成为全局变量。目前您已将其设置为局部变量。
我使用了一种稍微不同的方式来初始化客户端,从这个示例 rails 应用程序中复制,使用标准的 Zendesk API gem:
https://github.com/devarispbrown/zendesk_help_rails/blob/master/app/controllers/application_controller.rb
正如 danielrsmith 指出的,client
变量超出范围。您可以使用这样的初始化程序:
config/initializers/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
然后 return 其他地方的客户端 client = ZendeskClient.instance
(为简洁起见删节):
app/services/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
希望这对您有所帮助。
我正在尝试在我的应用程序中设置 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'
我认为客户端与我的配置初始值设定项中定义的相同。不知何故,我认为现在这是一个不同的对象。我曾尝试查看他们的文档以获取更多信息,但我不知道这是什么?
如果您想使用初始化程序中定义的 client
,您需要通过将其更改为 $client
使其成为全局变量。目前您已将其设置为局部变量。
我使用了一种稍微不同的方式来初始化客户端,从这个示例 rails 应用程序中复制,使用标准的 Zendesk API gem: https://github.com/devarispbrown/zendesk_help_rails/blob/master/app/controllers/application_controller.rb
正如 danielrsmith 指出的,client
变量超出范围。您可以使用这样的初始化程序:
config/initializers/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
然后 return 其他地方的客户端 client = ZendeskClient.instance
(为简洁起见删节):
app/services/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
希望这对您有所帮助。