ruby - Why are my scripts from lib/assets directory in a Rails application not executing, or erring out? -
i'm trying use connectwise api's in rails 3 application. i've built , tested api using plain ruby , it's successful. i'm trying add script lib/assets
in rails app run seems either not executing script, or executing , failing without giving me error output.
here successful ruby scripts:
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 proxying 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 proxying 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
running script has output:
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"}
so add rails app i've added 2 .rb
files lib/assets
, cw_apis.rb
, cw_get_company_integrator.rb
. here contents:
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
and 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
i'm trying execute
cwgetcompanyintegrator
class in cwintegratoraccountscontroller
.
here code inside create
action cwintegratoraccountscontroller
i've omitted index
, show
, new
, edit
, update
, , destroy
:
require 'assets/cw_get_company_integrator' class cwintegratoraccountscontroller < applicationcontroller skip_before_filter :require_company, :only => [:create,:new] # /cw_integrator_accounts # /cw_integrator_accounts.json def create unless cwintegratoraccount.count >= 1 @cw_integrator_account = cwintegratoraccount.new(params[:cw_integrator_account]) respond_to |format| if @cw_integrator_account.save # run 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
i know i'm getting past if @cw_integrator_account.save
, , cwgetcompanyintegrator
because i'm getting redirect (and see in logs) format.html { redirect_to root_url, notice: 'cw_integrator success' }
, cwgetcompanyintegrator
not erring or executing (properly way).
what proper way make class execute?
lib
not autoloaded default. need add autoload path - see auto-loading lib files in rails 4in addition,
lib/assets
used asset pipeline. should create different folder classes.your code in
cwgetcompanyintegrator
needs wrapped in method, following block you'd call usingcwgetcompanyintegrator.call
class cwgetcompanyintegrator def self.call # execute code here end end
Comments
Post a Comment