3

我正在尝试在我的应用程序中设置 Zendesk API,我决定使用Zendesk 构建的 API

我已经设置了初始化对象来加载客户端。

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'

我认为客户端与我的配置初始化程序中定义的客户端相同。不知何故,我认为这是一个不同的对象。我曾尝试查看他们的文档以获取更多信息,但我不知道这是什么?

4

2 回答 2

4

我使用了一种稍微不同的方式来初始化客户端,使用标准 Zendesk API gem 从这个示例 Rails 应用程序复制: 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

希望这可以帮助。

于 2016-06-13T22:14:36.760 回答
4

如果要使用client初始化程序中定义的 ,则需要通过将其更改为$client. 目前,您将其设置为局部变量。

于 2016-06-09T15:47:00.457 回答