为什么我在 Rails 应用程序中的 lib/assets 目录中的脚本不执行或出错?
Why are my scripts from lib/assets directory in a Rails application not executing, or erring out?
我正在尝试在我的 Rails 3 应用程序中使用 ConnectWise API。我已经使用普通 ruby 构建并测试了 API 并且它成功了。现在我正在尝试将此脚本添加到我的 Rails 应用程序中的 lib/assets
到 运行 但它似乎要么不执行脚本,要么执行并失败但没有给我任何错误输出.
以下是成功的 Ruby 脚本:
CompanyApis Class:
require 'builder'
class CompanyApis
def self.get_company
cw_company_id = 21920
integrator_company_id = 'COMPANY_ID'
integrator_login_id = 'INTERGRATOR_LOGIN'
integrator_password = 'INTEGRATOR PW'
xml = Builder::XmlMarkup.new(:indent=>2)
xml.instruct!
xml.tag!('soap:Envelope',
:'xmlns:soap' => 'http://schemas.xmlsoap.org/soap/envelope/',
:xmlns => 'http://connectwise.com'){
xml.tag!('soap:Body'){
xml.tag!('GetCompany'){
xml.tag!('credentials'){
xml.CompanyId(integrator_company_id)
xml.IntegratorLoginId(integrator_login_id)
xml.IntegratorPassword(integrator_password)
}
xml.id(cw_company_id)
}
}
}
end
end
CwIntegrator Class:
require 'net/https'
require 'uri'
require 'nokogiri'
require './company_api'
class CwIntegrator
cw_company_id = 21920
cw_hostname = 'cw.host.com'
companyapi_url = "https://#{cw_hostname}/v4_6_release/apis/2.0/CompanyApi.asmx"
uri = URI.parse(companyapi_url)
# Use for proxying to Kali
#proxy_addr = '172.16.1.149'
#proxy_port = 8080
request = Net::HTTP::Post.new(uri.path)
request.add_field('Content-Type', 'text/xml; charset=utf-8')
request.add_field('SOAPAction', 'http://connectwise.com/GetCompany')
request.body = CompanyApis.get_company
http = Net::HTTP.new(uri.host, uri.port)
# Use for proxying to Kali
#http = Net::HTTP.new(uri.host, uri.port, proxy_addr, proxy_port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.start {|h| h.request(request)}
company_info = []
xml_doc = Nokogiri::XML(response.body).remove_namespaces!
company_name = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/CompanyName').text
company_street_addr = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/StreetLines/string')[0].text
begin
company_street_addr2 = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/StreetLines/string')[1].text
end
company_city = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/City').text
company_state = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/State').text
company_zip = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/Zip').text
company_country = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/Country').text
company_status = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/Status').text
company_phone = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/PhoneNumber').text
company_fax = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/FaxNumber').text
company_www = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/WebSite').text
company_info += [company_name: company_name, cw_company_id: cw_company_id, company_name: company_name,
company_street_addr: company_street_addr, company_street_addr2: company_street_addr2,
company_city: company_city, company_state: company_state, company_zip: company_zip,
company_country:company_country,company_status: company_status, company_phone: company_phone,
company_fax: company_fax, company_www: company_www]
puts(company_info)
end
运行 这个脚本有这个输出:
ruby cw_integrator.rb
{:company_name=>"Orlando Fake Co, LLC.", :cw_company_id=>21920, :company_street_addr=>"STREET ADDR.", :company_street_addr2=>"Suite 400", :company_city=>"Orlando", :company_state=>"FL", :company_zip=>"32839", :company_country=>"United States", :company_status=>"Active Gold TTS", :company_phone=>"5558765309", :company_fax=>"", :company_www=>"www.myweb.com"}
因此,为了将此添加到 Rails 应用程序,我已将两个 .rb
文件添加到 lib/assets
、cw_apis.rb
和 cw_get_company_integrator.rb
。以下是它们的内容:
CwApis class:
#!/usr/bin/env ruby
require 'builder'
class CwApis
def self.get_company_xml_request
cw_integrator_account = CwIntegratorAccount.first
integrator_company_id = cw_integrator_account.integrator_company_id
integrator_login_id = cw_integrator_account.integrator_login_id
integrator_password = cw_integrator_account.integrator_password
xml = Builder::XmlMarkup.new(:indent=>2)
xml.instruct!
xml.tag!('soap:Envelope',
:'xmlns:soap' => 'http://schemas.xmlsoap.org/soap/envelope/',
:xmlns => 'http://connectwise.com'){
xml.tag!('soap:Body'){
xml.tag!('GetCompany'){
xml.tag!('credentials'){
xml.CompanyId(integrator_company_id)
xml.IntegratorLoginId(integrator_login_id)
xml.IntegratorPassword(integrator_password)
}
xml.id(cw_integrator_account.cw_company_id)
}
}
}
end
end
和 CwGetCompanyIntegrator Class:
#!/usr/bin/env ruby
require 'net/https'
require 'uri'
require 'nokogiri'
require 'assets/cw_apis'
class CwGetCompanyIntegrator
cw_integrator_account = CwIntegratorAccount.first
cw_hostname = cw_integrator_account.cw_hostname
company_api_url = "https://#{cw_hostname}/v4_6_release/apis/2.0/CompanyApi.asmx"
uri = URI.parse(company_api_url)
request = Net::HTTP::Post.new(uri.path)
request.add_field('Content-Type', 'text/xml; charset=utf-8')
request.add_field('SOAPAction', 'http://connectwise.com/GetCompany')
request.body = CwApis.get_company_xml_request
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL
response = http.start {|h| h.request(request)}
xml_doc = Nokogiri::XML(response.body).remove_namespaces!
company_name = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/CompanyName').text
company_street_addr = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/StreetLines/string')[0].text
begin
company_street_addr2 = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/StreetLines/string')[1].text
end
company_city = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/City').text
company_state = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/State').text
company_zip = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/Zip').text
company_country = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/Country').text
company_status = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/Status').text
company_phone = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/PhoneNumber').text
company_fax = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/FaxNumber').text
company_www = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/WebSite').text
CompanyInfosController.create!(cw_company_id: cw_integrator_account.cw_company_id, company_name: company_name,
company_street_addr: company_street_addr, company_street_addr2: company_street_addr2,
company_city: company_city, company_state: company_state, company_zip: company_zip,
company_country:company_country, company_status: company_status, company_phone: company_phone,
company_fax: company_fax, company_www: company_www)
end
我正在尝试 execute
CwGetCompanyIntegrator
class 在我的 CwIntegratorAccountsController
.
这是 CwIntegratorAccountsController
中 create
操作中的代码 我省略了 index
、show
、new
、edit
、update
和 destroy
:
require 'assets/cw_get_company_integrator'
class CwIntegratorAccountsController < ApplicationController
skip_before_filter :require_company, :only => [:create,:new]
# GET /cw_integrator_accounts
# GET /cw_integrator_accounts.json
def create
unless CwIntegratorAccount.count >= 1
@cw_integrator_account = CwIntegratorAccount.new(params[:cw_integrator_account])
respond_to do |format|
if @cw_integrator_account.save
# Run the CW Integrator
CwGetCompanyIntegrator
format.html { redirect_to root_url, notice: 'cw_integrator success' }
#format.json { render json: @cw_integrator_account, status: :created, location: @cw_integrator_account }
else
format.html { render action: 'new' }
format.json { render json: @cw_integrator_account.errors, status: :unprocessable_entity }
end
end
end
end
end
我知道我正在通过 if @cw_integrator_account.save
和 CwGetCompanyIntegrator
,因为我正在从 format.html { redirect_to root_url, notice: 'cw_integrator success' }
获取重定向(并在日志中看到它),但是CwGetCompanyIntegrator
没有犯错或执行(以任何方式正确)。
执行此 class 的正确方法是什么?
lib
默认不自动加载。您需要将其添加到自动加载路径 - 请参阅 Auto-loading lib files in Rails 4
此外,lib/assets
被Asset Pipeline使用。您应该为 类 创建一个不同的文件夹。
您在 CwGetCompanyIntegrator
中的代码需要包装在一个方法中,例如您使用 CwGetCompanyIntegrator.call
调用的以下代码块
class CwGetCompanyIntegrator
def self.call
# execute code here
end
end
我正在尝试在我的 Rails 3 应用程序中使用 ConnectWise API。我已经使用普通 ruby 构建并测试了 API 并且它成功了。现在我正在尝试将此脚本添加到我的 Rails 应用程序中的 lib/assets
到 运行 但它似乎要么不执行脚本,要么执行并失败但没有给我任何错误输出.
以下是成功的 Ruby 脚本:
CompanyApis Class:
require 'builder'
class CompanyApis
def self.get_company
cw_company_id = 21920
integrator_company_id = 'COMPANY_ID'
integrator_login_id = 'INTERGRATOR_LOGIN'
integrator_password = 'INTEGRATOR PW'
xml = Builder::XmlMarkup.new(:indent=>2)
xml.instruct!
xml.tag!('soap:Envelope',
:'xmlns:soap' => 'http://schemas.xmlsoap.org/soap/envelope/',
:xmlns => 'http://connectwise.com'){
xml.tag!('soap:Body'){
xml.tag!('GetCompany'){
xml.tag!('credentials'){
xml.CompanyId(integrator_company_id)
xml.IntegratorLoginId(integrator_login_id)
xml.IntegratorPassword(integrator_password)
}
xml.id(cw_company_id)
}
}
}
end
end
CwIntegrator Class:
require 'net/https'
require 'uri'
require 'nokogiri'
require './company_api'
class CwIntegrator
cw_company_id = 21920
cw_hostname = 'cw.host.com'
companyapi_url = "https://#{cw_hostname}/v4_6_release/apis/2.0/CompanyApi.asmx"
uri = URI.parse(companyapi_url)
# Use for proxying to Kali
#proxy_addr = '172.16.1.149'
#proxy_port = 8080
request = Net::HTTP::Post.new(uri.path)
request.add_field('Content-Type', 'text/xml; charset=utf-8')
request.add_field('SOAPAction', 'http://connectwise.com/GetCompany')
request.body = CompanyApis.get_company
http = Net::HTTP.new(uri.host, uri.port)
# Use for proxying to Kali
#http = Net::HTTP.new(uri.host, uri.port, proxy_addr, proxy_port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.start {|h| h.request(request)}
company_info = []
xml_doc = Nokogiri::XML(response.body).remove_namespaces!
company_name = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/CompanyName').text
company_street_addr = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/StreetLines/string')[0].text
begin
company_street_addr2 = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/StreetLines/string')[1].text
end
company_city = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/City').text
company_state = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/State').text
company_zip = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/Zip').text
company_country = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/Country').text
company_status = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/Status').text
company_phone = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/PhoneNumber').text
company_fax = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/FaxNumber').text
company_www = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/WebSite').text
company_info += [company_name: company_name, cw_company_id: cw_company_id, company_name: company_name,
company_street_addr: company_street_addr, company_street_addr2: company_street_addr2,
company_city: company_city, company_state: company_state, company_zip: company_zip,
company_country:company_country,company_status: company_status, company_phone: company_phone,
company_fax: company_fax, company_www: company_www]
puts(company_info)
end
运行 这个脚本有这个输出:
ruby cw_integrator.rb
{:company_name=>"Orlando Fake Co, LLC.", :cw_company_id=>21920, :company_street_addr=>"STREET ADDR.", :company_street_addr2=>"Suite 400", :company_city=>"Orlando", :company_state=>"FL", :company_zip=>"32839", :company_country=>"United States", :company_status=>"Active Gold TTS", :company_phone=>"5558765309", :company_fax=>"", :company_www=>"www.myweb.com"}
因此,为了将此添加到 Rails 应用程序,我已将两个 .rb
文件添加到 lib/assets
、cw_apis.rb
和 cw_get_company_integrator.rb
。以下是它们的内容:
CwApis class:
#!/usr/bin/env ruby
require 'builder'
class CwApis
def self.get_company_xml_request
cw_integrator_account = CwIntegratorAccount.first
integrator_company_id = cw_integrator_account.integrator_company_id
integrator_login_id = cw_integrator_account.integrator_login_id
integrator_password = cw_integrator_account.integrator_password
xml = Builder::XmlMarkup.new(:indent=>2)
xml.instruct!
xml.tag!('soap:Envelope',
:'xmlns:soap' => 'http://schemas.xmlsoap.org/soap/envelope/',
:xmlns => 'http://connectwise.com'){
xml.tag!('soap:Body'){
xml.tag!('GetCompany'){
xml.tag!('credentials'){
xml.CompanyId(integrator_company_id)
xml.IntegratorLoginId(integrator_login_id)
xml.IntegratorPassword(integrator_password)
}
xml.id(cw_integrator_account.cw_company_id)
}
}
}
end
end
和 CwGetCompanyIntegrator Class:
#!/usr/bin/env ruby
require 'net/https'
require 'uri'
require 'nokogiri'
require 'assets/cw_apis'
class CwGetCompanyIntegrator
cw_integrator_account = CwIntegratorAccount.first
cw_hostname = cw_integrator_account.cw_hostname
company_api_url = "https://#{cw_hostname}/v4_6_release/apis/2.0/CompanyApi.asmx"
uri = URI.parse(company_api_url)
request = Net::HTTP::Post.new(uri.path)
request.add_field('Content-Type', 'text/xml; charset=utf-8')
request.add_field('SOAPAction', 'http://connectwise.com/GetCompany')
request.body = CwApis.get_company_xml_request
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL
response = http.start {|h| h.request(request)}
xml_doc = Nokogiri::XML(response.body).remove_namespaces!
company_name = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/CompanyName').text
company_street_addr = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/StreetLines/string')[0].text
begin
company_street_addr2 = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/StreetLines/string')[1].text
end
company_city = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/City').text
company_state = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/State').text
company_zip = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/Zip').text
company_country = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/DefaultAddress/Country').text
company_status = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/Status').text
company_phone = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/PhoneNumber').text
company_fax = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/FaxNumber').text
company_www = xml_doc.xpath('//Envelope/Body/GetCompanyResponse/GetCompanyResult/WebSite').text
CompanyInfosController.create!(cw_company_id: cw_integrator_account.cw_company_id, company_name: company_name,
company_street_addr: company_street_addr, company_street_addr2: company_street_addr2,
company_city: company_city, company_state: company_state, company_zip: company_zip,
company_country:company_country, company_status: company_status, company_phone: company_phone,
company_fax: company_fax, company_www: company_www)
end
我正在尝试 execute
CwGetCompanyIntegrator
class 在我的 CwIntegratorAccountsController
.
这是 CwIntegratorAccountsController
中 create
操作中的代码 我省略了 index
、show
、new
、edit
、update
和 destroy
:
require 'assets/cw_get_company_integrator'
class CwIntegratorAccountsController < ApplicationController
skip_before_filter :require_company, :only => [:create,:new]
# GET /cw_integrator_accounts
# GET /cw_integrator_accounts.json
def create
unless CwIntegratorAccount.count >= 1
@cw_integrator_account = CwIntegratorAccount.new(params[:cw_integrator_account])
respond_to do |format|
if @cw_integrator_account.save
# Run the CW Integrator
CwGetCompanyIntegrator
format.html { redirect_to root_url, notice: 'cw_integrator success' }
#format.json { render json: @cw_integrator_account, status: :created, location: @cw_integrator_account }
else
format.html { render action: 'new' }
format.json { render json: @cw_integrator_account.errors, status: :unprocessable_entity }
end
end
end
end
end
我知道我正在通过 if @cw_integrator_account.save
和 CwGetCompanyIntegrator
,因为我正在从 format.html { redirect_to root_url, notice: 'cw_integrator success' }
获取重定向(并在日志中看到它),但是CwGetCompanyIntegrator
没有犯错或执行(以任何方式正确)。
执行此 class 的正确方法是什么?
lib
默认不自动加载。您需要将其添加到自动加载路径 - 请参阅 Auto-loading lib files in Rails 4此外,
lib/assets
被Asset Pipeline使用。您应该为 类 创建一个不同的文件夹。您在
调用的以下代码块CwGetCompanyIntegrator
中的代码需要包装在一个方法中,例如您使用CwGetCompanyIntegrator.call
class CwGetCompanyIntegrator def self.call # execute code here end end