Module: Restspec

Includes:
ActiveSupport::Configurable
Defined in:
lib/restspec.rb,
lib/restspec/version.rb,
lib/restspec/shortcuts.rb,
lib/restspec/schema/dsl.rb,
lib/restspec/schema/types.rb,
lib/restspec/configuration.rb,
lib/restspec/endpoints/dsl.rb,
lib/restspec/schema/schema.rb,
lib/restspec/schema/checker.rb,
lib/restspec/requirements/dsl.rb,
lib/restspec/rspec/api_macros.rb,
lib/restspec/schema/attribute.rb,
lib/restspec/endpoints/network.rb,
lib/restspec/endpoints/request.rb,
lib/restspec/rspec/api_helpers.rb,
lib/restspec/values/super_hash.rb,
lib/restspec/endpoints/endpoint.rb,
lib/restspec/endpoints/response.rb,
lib/restspec/values/status_code.rb,
lib/restspec/endpoints/namespace.rb,
lib/restspec/stores/schema_store.rb,
lib/restspec/endpoints/has_schemas.rb,
lib/restspec/endpoints/url_builder.rb,
lib/restspec/schema/schema_example.rb,
lib/restspec/stores/endpoint_store.rb,
lib/restspec/stores/namespace_store.rb,
lib/restspec/requirements/requirement.rb,
lib/restspec/runners/docs/docs_runner.rb,
lib/restspec/schema/attribute_example.rb,
lib/restspec/runners/docs/template_context.rb,
lib/restspec/runners/install/install_runner.rb

Defined Under Namespace

Modules: Docs, Endpoints, HeaderTests, Install, RSpec, Requirements, Schema, Stores, Values

Constant Summary collapse

VERSION =
"0.3.0"
SchemaStore =

The Schema Store is a Hash extended using Restspec::Stores::SchemaStoreDelegator This is where we store the schemas to use.

It's important to note that, because this is a Hash, there can't be two schemas with the same name.

Stores::SchemaStore
EndpointStore =

The Endpoint Store is a Hash extended using Stores::EndpointStoreDelegator This is where we store the endpoints to tests.

It's important to note that, because this is a Hash, there can't be two endpoint with the same full name. There can't be two endpoints called books/index for example.

Stores::EndpointStore
NamespaceStore =

The Namespace Store is a Hash extended using Stores::NamespaceStoreDelegator This is where we store the namespaces of the API.

It's important to note that, because this is a Hash, there can't be two namespaces with the same name. Anonymous namespaces can't be stored here. They are just stored as children of each namespace.

Stores::NamespaceStore

Class Method Summary collapse

Class Method Details

.configure {|config| ... } ⇒ Object

Configure Restspec and loads the information of the API defined in the DSL definitions if the DSL definitions are defined.

The following options are available:

  • base_url: The base url of the API. It is a full url, not only a domain, so it can include more than just the api url but a version path like: http://localhost:3000/api/v1.
  • schema_definition: The file location where the file describing the schemas is located.
  • endpoints_definition: The file location where the file describing the endpoints is located.
  • requirements_definition: The file location where the file describing the requirements is located.
  • request: An object that configures the default request. It has a header hash inside to set default headers for every request.
  • custom: An object to hold custom configuration. It can be accessed anywhere using Restspec.custom.

Examples:

Defines some configuration options:

Restspec.configure do |config|
  config.base_url = 'http://localhost:3000/api/v1'

  config.schema_definition = "#{File.dirname __FILE__}/schemas.rb"
  config.endpoints_definition = "#{File.dirname __FILE__}/endpoints.rb"
  config.requirements_definition = "#{File.dirname __FILE__}/requirements.rb"

  config.custom.api_key = ENV['API_KEY']
  config.request.headers['AUTHORIZATION'] = "Token token=\"#{config.custom.api_key}\""
end

Yields:

  • (config)

    A block describing the Restspec configuration



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/restspec/configuration.rb', line 31

def configure
  config.request = OpenStruct.new(headers: {})
  config.request.headers['Content-Type'] = 'application/json'
  config.request.headers['Accept'] = 'application/json'

  config.custom = OpenStruct.new

  yield config

  populate_stores
end

.example_for(schema_name, extensions = {}) ⇒ Object

Shortcut for find a schema by name, create a Restspec::Schema::SchemaExample and call its value method to get a example.

Examples:

Without extensions

Restspec.example_for(:person) # { name: 'John', age: 25 }

With extensions

Restspec.example_for(:person, age: 18) # { name: 'John', age: 18 }

Parameters:

  • schema_name (Symbol)

    The name of the schema.

  • extensions (Hash) (defaults to: {})

    A set of extensions for the example.



13
14
15
16
# File 'lib/restspec/shortcuts.rb', line 13

def example_for(schema_name, extensions = {})
  schema = Restspec::SchemaStore.get(schema_name)
  Schema::SchemaExample.new(schema, extensions).value
end