mercredi 29 avril 2015

Why am I getting this TypeError? "no implicit conversion of Module into Integer"

I'm getting TypeError in CwIntegratorAccountsController#create. I'm calling a script (CwGetCompanyIntegrator.call) in the create method of the controller.

CwIntegratorAccountsController:

require 'apis/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.call
          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

It looks like it's failing when it starts the https request to the ConnectWise Server at this line: response = http.start {|h| h.request(request)}

CwGetCompanyIntegrator script:

#!/usr/bin/env ruby
require 'net/https'
require 'uri'
require 'nokogiri'
require 'apis/cw_apis'

class CwGetCompanyIntegrator
  def self.call
    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://ift.tt/1GEDlOs')
    request.body = CwApis.get_company_xml_request
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    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
end

Full trace:

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:893:in `initialize'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:893:in `new'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:893:in `connect'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:862:in `do_start'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:851:in `start'
lib/apis/cw_get_company_integrator.rb:21:in `call'
app/controllers/cw_integrator_accounts_controller.rb:54:in `block in create'
actionpack (3.2.14) lib/action_controller/metal/mime_responds.rb:270:in `call'
actionpack (3.2.14) lib/action_controller/metal/mime_responds.rb:270:in `retrieve_collector_from_mimes'
actionpack (3.2.14) lib/action_controller/metal/mime_responds.rb:194:in `respond_to'
app/controllers/cw_integrator_accounts_controller.rb:50:in `create'
actionpack (3.2.14) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.14) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.14) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.14) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.14) lib/active_support/callbacks.rb:447:in `_run__2237874046494148672__process_action__4163123032493016418__callbacks'
activesupport (3.2.14) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.14) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.14) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.14) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.14) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.14) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.14) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.14) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.14) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.14) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.14) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.14) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.14) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.14) lib/abstract_controller/rendering.rb:45:in `process'
rack-mini-profiler (0.9.2) lib/mini_profiler/profiling_methods.rb:108:in `block in profile_method'
actionpack (3.2.14) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.14) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.14) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.14) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.14) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.14) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.14) lib/action_dispatch/routing/route_set.rb:608:in `call'
rack-webconsole-pry (0.1.9) lib/rack/webconsole/assets.rb:26:in `call'
rack-webconsole-pry (0.1.9) lib/rack/webconsole.rb:79:in `call'
actionpack (3.2.14) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.2.14) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.14) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.14) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.14) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.14) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.14) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.14) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.14) lib/active_support/callbacks.rb:405:in `_run__2942276951910103516__call__2669772965393719582__callbacks'
activesupport (3.2.14) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.14) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.14) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.14) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.14) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.14) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.14) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.14) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.14) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.14) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.14) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.14) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.14) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.14) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.14) lib/action_dispatch/middleware/static.rb:63:in `call'
rack-mini-profiler (0.9.2) lib/mini_profiler/profiler.rb:300:in `call'
railties (3.2.14) lib/rails/engine.rb:484:in `call'
railties (3.2.14) lib/rails/application.rb:231:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.14) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

This works in a plain ruby script, is it an environment thing?

Working 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 = 'INTERGRATOR_PW'

    xml = Builder::XmlMarkup.new(:indent=>2)
    xml.instruct!
    xml.tag!('soap:Envelope',
             :'xmlns:soap' => 'http://ift.tt/sVJIaE',
             :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
  def self.call
    cw_company_id = 21920
    cw_hostname = 'cw.i-techsupport.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://ift.tt/1GEDlOs')
    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
end

CwIntegrator.call

Aucun commentaire:

Enregistrer un commentaire