Skip to content

Commit 09356fe

Browse files
committed
Add yardoc and better documentation
1 parent e826402 commit 09356fe

File tree

17 files changed

+195
-94
lines changed

17 files changed

+195
-94
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ coverage/
1919
**/tmp
2020
*.gem
2121
*.sqlite3
22+
23+
doc/
24+
.yardoc/

.yardopts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--no-private

Gemfile.lock

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
rspec-api-matchers (0.6.1)
4+
rspec-api-matchers (0.6.2)
55
activesupport
66
rack
77
rspec
@@ -50,6 +50,7 @@ GEM
5050
atomic
5151
tins (0.13.0)
5252
tzinfo (0.3.38)
53+
yard (0.8.7.2)
5354

5455
PLATFORMS
5556
ruby
@@ -59,3 +60,4 @@ DEPENDENCIES
5960
coveralls
6061
rake
6162
rspec-api-matchers!
63+
yard

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ RSpecApi::Matchers lets you express outcomes on the response of web APIs.
55

66
expect(response).to have_status(:not_found)
77

8-
More documentation and examples about RSpecApi are available at [http://rspec-api.github.io](http://rspec-api.github.io)
8+
The full documentation is available at [rubydoc.info](http://rubydoc.info/github/rspec-api/rspec-api-matchers/master/frames).
9+
10+
More information about the parent project RSpecApi is available at [rspec-api.github.io](http://rspec-api.github.io)
911

1012
[![Build Status](https://travis-ci.org/rspec-api/rspec-api-matchers.png?branch=master)](https://travis-ci.org/rspec-api/rspec-api-matchers)
1113
[![Code Climate](https://codeclimate.com/github/rspec-api/rspec-api-matchers.png)](https://codeclimate.com/github/rspec-api/rspec-api-matchers)
@@ -21,10 +23,9 @@ Available matchers
2123
expect(response).to be_a_collection
2224
expect(response).to be_wrapped_in_callback('alert')
2325
expect(response).to be_sorted(by: :id, verse: :desc)
24-
expect(response).to be_filtered(by: :id, value: 10)
26+
expect(response).to be_filtered(by: :id, value: 10, compare_with: :>)
2527
expect(response).to have_attributes(id: {value: 1.2}, url: {type: {string: :url}})
2628

27-
2829
How to install
2930
==============
3031

@@ -41,7 +42,6 @@ Indicating the full version in your Gemfile (*major*.*minor*.*patch*) guarantees
4142
that your project won’t occur in any error when you `bundle update` and a new
4243
version of RSpecApi::Matchers is released.
4344

44-
4545
How to contribute
4646
=================
4747

lib/rspec-api/matchers.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@
1111
require 'rspec-api/matchers/status/have_status'
1212

1313
module RSpecApi
14+
# Provides RSpec Matchers for RESTful web APIs.
15+
#
16+
# To have these matchers available inside of an RSpec `describe` block,
17+
# tag that block with the `:rspec_api` metadata, or explicitly include the
18+
# RSpecApi::Matchers module inside the example group
19+
#
20+
# @example Tag a `describe` block as `:rspec_api`:
21+
# describe "Artists", rspec_api: true do
22+
# ... # here you can write `expect(response).to have_status :ok`, etc.
23+
# end
24+
#
25+
# @example Explicitly include the RSpecApi::Matchers module
26+
# describe "Artists" do
27+
# include RSpecApi::Matchers
28+
# ... # here you can write `expect(response).to have_status :ok`, etc.
29+
# end
1430
module Matchers
1531
include Attributes
1632
include Collection
@@ -25,20 +41,4 @@ module Matchers
2541
end
2642
end
2743

28-
# RSpecApi::Matchers adds matchers to test RESTful APIs.
29-
#
30-
# To have these matchers available inside of an RSpec `describe` block, tag that
31-
# block with the `:rspec_api` metadata:
32-
#
33-
# describe "Artists", rspec_api: true do
34-
# ... # here you can write `expect(response).to have_status :ok`, etc.
35-
# end
36-
RSpec.configuration.include RSpecApi::Matchers, rspec_api: true
37-
38-
# You can also explicitly include the RSpec::Api module inside the example group:
39-
#
40-
# describe "Artists" do
41-
# include RSpecApi::Matchers
42-
# ... # here you can write `expect(response).to have_status :ok`, etc.
43-
# end
44-
#
44+
RSpec.configuration.include RSpecApi::Matchers, rspec_api: true

lib/rspec-api/matchers/attributes/have_attributes.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@
33
module RSpecApi
44
module Matchers
55
module Attributes
6-
# Passes if the response body is either a JSON object or a collection of
7-
# JSON objects that include all the +attributes+.
6+
# Passes if the object includes the expected attributes in the body.
87
#
9-
# @example
8+
# @example Passes if the body is a collection of objects with a URL site
9+
# require 'rspec-api-matchers'
1010
#
11-
# # Passes if the body is an object with a URL-formatted String :site
12-
# response = OpenStruct.new body: '{"site": "http://www.example.com"}'
13-
# expect(response).to have_attributes(site: {type: {string: :url}})
11+
# body = '[{"site": "http://www.foo.com"}, {"site": "http://bar.com"}]'
12+
# obj = OpenStruct.new body: body
1413
#
15-
# For more examples check +have_attributes_spec.rb+.
16-
17-
# TODO: add &block options
14+
# describe 'have_attributes' do
15+
# include RSpecApi::Matchers::Attributes
16+
# it { expect(obj).to have_attributes(site: {type: {string: :url}}) }
17+
# end
18+
#
19+
# # => (rspec) 1 example, 0 failures
20+
#
21+
# @param [Hash] attributes The expected attributes in the body
22+
#
23+
# The method works only if the object is a JSON object or a collection of
24+
# JSON objects; in the latter case all the objects need to include the
25+
# expected attributes for the expectation to pass.
26+
#
27+
# @see http://git.io/w2dLnw have_attributes_spec.rb for more examples
1828
def have_attributes(*attributes)
1929
RSpecApi::Matchers::Attributes::Matcher.new *attributes
2030
end

lib/rspec-api/matchers/collection/be_a_collection.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
module RSpecApi
44
module Matchers
55
module Collection
6-
# Passes if the response body is a collection of JSON objects
6+
# Passes if the object has a collection of JSON objects in the body.
77
#
8-
# @example
8+
# @example Passes if the body is a JSON array
9+
# require 'rspec-api-matchers'
910
#
10-
# # Passes if the body is a JSON array
11-
# body = '[{"id": 1}]'
12-
# expect(OpenStruct.new body: body).to be_a_collection
11+
# body = '[{"id": 1}, {"name": "foo"}, {"name": "bar"}]'
12+
# obj = OpenStruct.new body: body
1313
#
14-
# For more examples check +be_a_collection_spec.rb+.
14+
# describe 'be_a_collection' do
15+
# include RSpecApi::Matchers::Collection
16+
# it { expect(obj).to be_a_collection }
17+
# end
18+
#
19+
# # => (rspec) 1 example, 0 failures
20+
#
21+
# @see http://git.io/Bl5qJQ be_a_collection_spec.rb for more examples
1522
def be_a_collection
1623
RSpecApi::Matchers::Collection::Matcher.new
1724
end

lib/rspec-api/matchers/content_type/have_content_type.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,26 @@
33
module RSpecApi
44
module Matchers
55
module ContentType
6-
# Passes if the response headers specify the provided content +type+
6+
# Passes if the object includes the expected content type in the headers.
77
#
8-
# @example
8+
# @param [Symbol or String] type The expected content type. Can either
9+
# be the exact Content-Type string or just the format. The special
10+
# value +:any+ matches any content type.
11+
#
12+
# @example Passes if the headers matches the provided JSON content type
13+
# require 'rspec-api-matchers'
914
#
10-
# # Passes if the headers matches the provided JSON content type
1115
# headers ={'Content-Type' => 'application/json; charset=utf-8'}
12-
# expect(OpenStruct.new headers: headers).to have_content_type(:json)
16+
# obj = OpenStruct.new headers: headers
17+
#
18+
# describe 'have_content_type' do
19+
# include RSpecApi::Matchers::ContentType
20+
# it { expect(obj).to have_content_type(:json) }
21+
# end
22+
#
23+
# # => (rspec) 1 example, 0 failures
1324
#
14-
# For more examples check +have_content_type_spec.rb+.
25+
# @see http://git.io/qwv-RQ have_content_type_spec.rb for more examples
1526
def have_content_type(type = nil)
1627
content_type = case type
1728
when :json then 'application/json; charset=utf-8'

lib/rspec-api/matchers/filter/be_filtered.rb

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,28 @@
33
module RSpecApi
44
module Matchers
55
module Filter
6-
# Passes if the response body is a non-empty filtered JSON collection
6+
# Passes if the object has a non-empty filtered JSON collection in the body.
77
#
8-
# @example
8+
# @param [Hash] options how the body is expected to be filtered.
9+
# @option options [Symbol or String] :by The JSON key to be filtered by
10+
# @option options [Object] :value The expected value for the field
11+
# @option options [Proc or Symbol] :compare_with (:==) The operator used
12+
# to compare the expected value with the values in the collection
913
#
10-
# # Passes if the body only contains objects with ID = 1
11-
# body = '[{"id": 1}, {"id": 1}, {"id": 1}]'
12-
# expect(OpenStruct.new body: body).to be_filtered by: :id, value: 1
14+
# @example Passes if the body only contains objects with ID > 1
15+
# require 'rspec-api-matchers'
1316
#
14-
# For more examples check +be_filtered_spec.rb+.
17+
# body = '[{"id": 2}, {"id": 3}]'
18+
# obj = OpenStruct.new body: body
19+
#
20+
# describe 'be_filtered' do
21+
# include RSpecApi::Matchers::Filter
22+
# it { expect(obj).to be_filtered by: :id, value: 1, compare_with: :> }
23+
# end
24+
#
25+
# # => (rspec) 1 example, 0 failures
26+
#
27+
# @see http://git.io/lHO7nw be_filtered_spec.rb for more examples
1528
def be_filtered(options = {})
1629
RSpecApi::Matchers::Filter::Matcher.new options
1730
end

lib/rspec-api/matchers/headers/have_headers.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
module RSpecApi
44
module Matchers
55
module Headers
6-
# Passes if the response has a non-empty headers Hash.
6+
# Passes if the object has a non-empty Hash in the headers.
77
#
8-
# @example
8+
# @example Passes if the headers include the content length
9+
# require 'rspec-api-matchers'
910
#
10-
# # Passes if the headers include the content length
1111
# headers = {'Content-Length' => 17372}
12-
# expect(OpenStruct.new headers: headers).to have_headers
12+
# obj = OpenStruct.new headers: headers
1313
#
14-
# For more examples check +have_headers_spec.rb+.
14+
# describe 'have_headers' do
15+
# include RSpecApi::Matchers::Headers
16+
# it { expect(obj).to have_headers }
17+
# end
18+
#
19+
# # => (rspec) 1 example, 0 failures
20+
#
21+
# @see http://git.io/-Wb61A have_headers_spec.rb for more examples
1522
def have_headers
1623
RSpecApi::Matchers::Headers::Matcher.new
1724
end

0 commit comments

Comments
 (0)