当我的 DeltaSynWorker 运行时,我收到 NoMethodErrors。这发生在作为当前工作应用程序的修订而构建的应用程序中。我不是最初的程序员,我是从 Java 背景开始的(我提到这一点是因为我可能遗漏了一些对其他人来说非常明显的东西)。NoMethodeError
当代码与当前在不同 Web 应用程序中正常工作的代码非常相似时,我无法弄清楚为什么会被抛出。
错误:
NoMethodError: undefined method `client' for #<ApiRequestBuilder:0x0000000705a8f0>
delta_sync_worker.rb
class DeltaSyncWorker < SyncWorker
include Sidekiq::Worker
sidekiq_options queue: "delta_syncs"
def perform(subscriber_id, client_id)
sleep(10)
current_subscriber = ApiSubscriberDecorator.decorate(Subscriber.find(subscriber_id))
Time.zone = current_subscriber.time_zone
client = ApiClientDecorator.decorate(Client.find(client_id))
arb = ApiRequestBuilder.new(URI.parse(SR_HOST + '/servlet/sync/process'))
if arb.client(:subscriber => current_subscriber, :client => client)
arb.transmit
if arb.success?
current_subscriber.touch(:sync_updated_at)
decorated_client = ClientDecorator.decorate(client.model)
ConfirmationsSyncWorker.perform_in(1.hours, current_subscriber.id)
else
error_params = {:subscriber => current_subscriber.id, :response_body => arb.response.body, :request_body => arb.request.body, :sync_type => "DeltaSyncWorker"}
Airbrake.notify(:error_class => "sync_error", :error_message => "Sync Error: #{arb.response.message}", :params => error_params)
end
end
end
end
api_request_builder.rb
require 'nokogiri'
class ApiRequestBuilder < AbstractController::Base
include AbstractController::Rendering
include AbstractController::Layouts
include AbstractController::Helpers
include AbstractController::Translation
include AbstractController::AssetPaths
self.view_paths = "app/api"
attr_accessor :request_body, :request, :response, :append_request_headers, :request_method, :url
def initialize(url, *args)
@url = url
if args
args.each do |arg|
arg.each_pair{ |k,v| instance_variable_set("@#{k.to_s}", v) }
end
end
end
# this will search for an api request template in api/requests, render that template and set any instance variables
def method_missing(meth, *args, &block)
if lookup_context.template_exists?("requests/#{meth.to_s}")
if args
args.each do |arg|
arg.each_pair{|k,v| instance_variable_set("@#{k.to_s}", v) }
end
end
@request_body = (render :template => "requests/#{meth.to_s}")
else
super
end
end
def transmit
@request_method ||= "Post"
@request = "Net::HTTP::#{@request_method}".constantize.new(@url.path)
@request['x-ss-user'] = @subscriber.sr_user if @subscriber && @subscriber.sr_user.present?
@request['x-ss-pwd'] = @subscriber.sr_password if @subscriber && @subscriber.sr_password.present?
unless @append_request_headers.nil?
@append_request_headers.each_pair{ |k,v| @request[k] = v }
end
@request.body = @request_body if request_body? && @request.request_body_permitted?
@http = Net::HTTP.new(@url.host, @url.port)
@http.use_ssl = true
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
@response = @http.request(@request)
end
def success?
if @response.code == 200.to_s
return true
else
return false
end
end
def request_body?
unless @request_body.nil?
return true
else
return false
end
end
end
我一直在NoMethodError
这里查看其他问题,但找不到适合我情况的答案。任何帮助是极大的赞赏。谢谢!