Sha256: 1eb2fbeed083a10f63daf91834d387fedd9094b879cf7d5d449fa82586cebefc

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

require 'json'

# This class stubs the Unipept API that's being used during the tests.
class ApiStub
  def setup_stubs
    @base_url = 'http://unipept.ugent.be/'

    %w[pept2ec pept2go pept2interpro pept2funct pept2lca pept2prot pept2taxa peptinfo].each do |endpoint|
      setup_endpoint endpoint
    end

    setup_taxa2lca
    setup_taxonomy
  end

  def setup_endpoint(name)
    items = JSON.parse(File.read(File.join(File.dirname(__FILE__), "resources/#{name}.json")))
    Typhoeus.stub("http://api.unipept.ugent.be/api/v1/#{name}.json").and_return do |req|
      peptides = req.options[:body][:input]

      filtered = items.select { |item| peptides.include? item['peptide'] }

      Typhoeus::Response.new(code: 200, body: JSON.dump(filtered))
    end
  end

  def setup_taxa2lca
    Typhoeus.stub('http://api.unipept.ugent.be/api/v1/taxa2lca.json').and_return(
      Typhoeus::Response.new(code: 200, body: '{
        "taxon_id": 1678,
        "taxon_name": "Bifidobacterium",
        "taxon_rank": "genus"
      }')
    )
  end

  def setup_taxonomy
    items = JSON.parse(File.read(File.join(File.dirname(__FILE__), 'resources/taxonomy.json')))
    Typhoeus.stub('http://api.unipept.ugent.be/api/v1/taxonomy.json').and_return do |req|
      taxa = req.options[:body][:input].map(&:to_i)

      filtered = items.select { |item| taxa.include? item['taxon_id'] }

      Typhoeus::Response.new(code: 200, body: JSON.dump(filtered))
    end
  end

  # Expects to be called with taxa "78", "57", "89", "28" and "67"
  def setup_taxa2tree
    Typhoeus.stub('http://api.unipept.ugent.be/api/v1/taxa2tree.json').and_return do |_|
      link = req.options[:body][:link] == 'true'
      if link
        Typhoeus::Response.new(code: 200, body: JSON.dump(gist: 'https://gist.github.com/8837824df7ef9831a9b4216f3fb547ee'))
      else
        result = JSON.parse(File.read(File.join(File.dirname(__FILE__), 'resources/taxa2tree.json')))
        Typhoeus::Response.new(code: 200, body: JSON.dump(result))
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
unipept-2.2.2 test/support/api_stub.rb
unipept-2.2.1 test/support/api_stub.rb
unipept-2.2.0 test/support/api_stub.rb