<commit_before># Mock5
[![Gem Version](https://img.shields.io/gem/v/mock5.svg)](https://rubygems.org/gems/mock5)
[![Build Status](https://img.shields.io/travis/rwz/mock5.svg)](http://travis-ci.org/rwz/mock5)
[![Code Climate](https://img.shields.io/codeclimate/github/rwz/mock5.svg)](https://codeclimate.com/github/rwz/mock5)

Mock5 allows to mock external APIs with simple Sinatra Rack apps.

## Installation

Add this line to your application's Gemfile:

    gem "mock5"

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install mock5

## Usage

Say you're writing a nice wrapper around remote user management REST API.
You want your library to handle any unexpected situation aproppriately and
show a relevant error message, or schedule a retry some time later.

Obviously, you can't rely on a production API to test all these codepaths. You
probably want a way to emulate all these situations locally. Enter Mock5:

```ruby
# user registers successfully
SuccessfulRegistration = Mock5.mock("http://example.com") do
  post "/users" do
    MultiJson.dump(
      first_name: "Zapp",
      last_name: "Brannigan",
      email: "<EMAIL>"
    )
  end
end

# registration returns validation error
UnsuccessfulRegistration = Mock5.mock("http://example.com") do
  post "/users" do
    halt 406, MultiJson.dump(<commit_msg>Add some examples and information to README<commit_after># Mock5
[![Gem Version](https://img.shields.io/gem/v/mock5.svg)](https://rubygems.org/gems/mock5)
[![Build Status](https://img.shields.io/travis/rwz/mock5.svg)](http://travis-ci.org/rwz/mock5)
[![Code Climate](https://img.shields.io/codeclimate/github/rwz/mock5.svg)](https://codeclimate.com/github/rwz/mock5)

Mock5 allows to mock external APIs with simple Sinatra Rack apps.

## Installation

This gem could be useful for testing, and maybe development purposes.
Add it to the relevant groups in your Gemfile.

```ruby
gem "mock5", groups: [:test, :development]
```

and run `bundle`.

## Usage

### mock
Use this method to describe API you're trying to mock.

```ruby
weather_api = Mock5.mock("http://weather-api.com") do
  get "/weather.json" do
    MultiJson.dump(
      location: "Philadelphia, PA",
      temperature: "60F",
      description: "Sunny"
    )
  end
end
```

### mount
Use this method to enable API mocks you've defined previously.

```ruby
Mock5.mount weather_api, some_other_api
Net::HTTP.get("weather-api.com", "/weather.json") # => "{\"location\":...
```

### unmount
Unmounts one of the already mounted APIs

```ruby
Mock5.unmount some_other_api # [, and_another_api... ]
```

### mounted_apis
This method returns a Set of all currently mounted APIs

```ruby
Mock5.mounted_apis # => { weather_api }
Mock5.mount another_api
Mock5.mounted_apis # => { weather_api, another_api }
```

### with_mounted
Executes the block with all given APIs mounted, and then unmounts them.

```ruby
Mock5.mounted_apis # => { other_api }
Mock5.with_mounted weather_api, other_api do
  Mock5.mounted_apis # => { other_api, weather_api }
  run_weather_api_test_suite!
end
Mock5.mounted_apis # => { other_api }
```

## Example

Say you're writing a nice wrapper around remote user management REST API.
You want your library to handle any unexpected situation aproppriately and
show a relevant error message, or schedule a retry some time later.

Obviously, you can't rely on a production API to test all these codepaths. You
probably want a way to emulate all these situations locally. Enter Mock5:

```ruby
# user registers successfully
SuccessfulRegistration = Mock5.mock("http://example.com") do
  post "/users" do
    MultiJson.dump(
      first_name: "Zapp",
      last_name: "Brannigan",
      email: "<EMAIL>"
    )
  end
end

# registration returns validation error
UnsuccessfulRegistration = Mock5.mock("http://example.com") do
  post "/users" do
    halt 406, MultiJson.dump(
{'fraction_non_alphanumeric': 0.1213292117465224, 'fraction_numerical': 0.011076764554353425, 'mean_word_length': 4.184245660881175, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1389426', 'n_tokens_mistral': 1287, 'n_tokens_neox': 1210, 'n_words': 388}