1
\$\begingroup\$

I am trying to write a rspec webmock test for a rails PORO model. What this model does is, query bugzilla with a set of filters for a list of bugs and iterate over it such that:

  1. The bug ids are used to make an xmlprc call to an internal service to get an id
  2. This id is used to call another internal service which returns a url.
  3. The outcome of this iteration is to create a list of hashes that includes the name of the bug, the type of the bug and an url.

The webmocks are getting messy and are forcing me to thing about a rewrite. I would like your advice on how to go about it, if at all.

# frozen_string_literal: true

class BugzillaLookup


  def initialize
    @xmlrpc_client = XMLRPC::Client.new_from_uri('https://foo.com/xmlrpc.cgi')
  end

  def find_url_for(args)
    report = []

    Bicho::Bug.where(query).each do |bug|
      location = get_thing_file(get_service_2_id(bug.id))
      things = create_hash_for_thing(bug['summary'], bug['id'], location)
      report.append(thing)
    end
    osp_sosreport
  end

  private

  def get_service_2_id(bug_id)
    @xmlrpc_client.call('Thing.get_thing_id', 'bug_id' => bug_id)['thing_id']
  rescue XMLRPC::FaultException => msg
    logger.debug msg
  end

  def get_thing_file(thing_id)
    job = HTTParty.get(URL + "thing/#{thing_id}/jobs").last
    return unless valid_response?(job)
    return if last_job_has_no_payloads(job)
    return unless payload_has_no_probe_files(job)
    filename = probe_file(job)
    job_id = job['job_id']
    URL + "reports/#{thing_id}/#{job_id}/probe/#{filename}"
  end

  def valid_response?(response)
    response.to_a.flatten.index('result').nil?
  end

  def last_job_has_no_payloads
    job['result_payloads'].empty?
  end

  def payload_has_no_probe_files(job)
    job['result_payloads'].select { |payload| payload['type'] == 'probe' }.present?
  end

  def probe_file(job)
    job['result_payloads'].select { |payload| payload['type'] == 'probe' }.first['filename']
  end

  def create_hash_for_thing(summary, type, location)
    thing = {}
    thing['name'] = summary
    thing['thing_type'] = type
    thing['file_location'] = location
  end
end
\$\endgroup\$
  • \$\begingroup\$ You are running into problems because you are missing a few more objects. Sadly, I don't have time for another big code review today, but have a look here at another code review I wrote to get an example of how you could approach the rewrite: codereview.stackexchange.com/questions/151041/… \$\endgroup\$ – Damien Roche Dec 30 '16 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.