Module: APISmith::WebMockExtensions

Defined in:
lib/api_smith/web_mock_extensions.rb

Overview

A set of extensions to make using APISmith with WebMock (or most test utilities in general) simpler when it comes checking values. Please note this is primarily intended for use with rspec due to dependence on subject in some places.

Author:

  • Darcy Laycock

  • Steve Webb

Instance Method Summary collapse

Instance Method Details

#api_url_for(path) ⇒ String

Expands the given path relative to the API for the current subject class. Namely, this makes it possible to convert a relative path to an endpoint-specified path.

Parameters:

  • path (String)

    the path to expand, minus endpoint etc.

Returns:

  • (String)

    the expanded path


29
30
31
32
33
# File 'lib/api_smith/web_mock_extensions.rb', line 29

def api_url_for(path)
  path     = subject_class_instance.send(:path_for, path)
  base_uri = subject_api_class.base_uri
  File.join base_uri, path
end

#stub_api(type, path) ⇒ Object

Short hand for #stub_request that lets you give it a relative path prior to expanding it.

Parameters:

  • the (:get, :post, :put, :delete)

    verb for the request

  • the (String)

    relative path for the api

Returns:

  • (Object)

    the result from stub_request


39
40
41
# File 'lib/api_smith/web_mock_extensions.rb', line 39

def stub_api(type, path)
  stub_request type, api_url_for(path)
end

#subject_api_classClass

Returns the class of the current subject

Returns:

  • (Class)

    the subject class


12
13
14
# File 'lib/api_smith/web_mock_extensions.rb', line 12

def subject_api_class
  subject.is_a?(Class) ? subject : subject.class
end

#subject_class_instanceObject

Returns an instance of the subject class, created via allocate (vs. new). Useful for giving access to utility methods used inside of the class without having to\ initialize a new client.

Returns:

  • (Object)

    the instance


20
21
22
# File 'lib/api_smith/web_mock_extensions.rb', line 20

def subject_class_instance
  @subject_class_instance ||= subject_api_class.allocate
end