rest_api_builder 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +29 -1
- data/lib/rest_api_builder/webmock_request_expectations.rb +15 -2
- data/rest_api_builder.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf07f7e26da929aa46e970e076dc189a744fafcb7cac4ccdc532529d3e6b8dd4
|
4
|
+
data.tar.gz: 192ffc62ea57897d606260b0d255722ce23c624be85dc137efa411a6df77c995
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b28c914cdb039919614d82e108d1413c349e9530a659ed78898b2cbe4e6b182a3ebdc3aa34f5b4383a21d68d827c9e7d7aef93fea97656536cde1197d6815500
|
7
|
+
data.tar.gz: 1560932fb31df12f946b56060a5411e249d19926c287fb561c95e863af1d6be37ff106ce304b6eb8dc34dc7b620ecddb2fd0c2fd7a667a7598cc73e7f33071d1
|
data/README.md
CHANGED
@@ -84,7 +84,7 @@ response[:status] #=> 200
|
|
84
84
|
response[:body] #=> ''
|
85
85
|
response[:headers] #=> {}
|
86
86
|
|
87
|
-
# Specifying
|
87
|
+
# Specifying expectation details with WebMock::Request methods
|
88
88
|
Expectations
|
89
89
|
.expect_execute(base_url: "test.com", method: :get)
|
90
90
|
.to_return(status: 404, body: "not found")
|
@@ -93,6 +93,29 @@ response = Request.execute(base_url: "test.com", method: :get)
|
|
93
93
|
response[:success] #=> false
|
94
94
|
response[:status] #=> 404
|
95
95
|
response[:body] #=> "not found"
|
96
|
+
|
97
|
+
# Specifying expectation details with :request and :response options
|
98
|
+
Expectations.expect_execute(
|
99
|
+
base_url: "test.com",
|
100
|
+
method: :post,
|
101
|
+
response: { body: 'hello' },
|
102
|
+
request: { body: WebMock::API.hash_including({foo: "bar"}) }
|
103
|
+
)
|
104
|
+
response = Request.json_execute(base_url: "test.com", method: :post, body: {foo: "bar"})
|
105
|
+
response[:success] #=> true
|
106
|
+
response[:body] #=> 'hello'
|
107
|
+
|
108
|
+
Request.json_execute(base_url: "test.com", method: :post, body: {bar: "baz"}) # => Raises WebMock::NetConnectNotAllowedError
|
109
|
+
|
110
|
+
# Using #expect_json_execute
|
111
|
+
Expectations.expect_json_execute(
|
112
|
+
base_url: "test.com",
|
113
|
+
method: :get,
|
114
|
+
response: { body: {hi: 'hello'} }
|
115
|
+
)
|
116
|
+
response = Request.execute(base_url: "test.com", method: :get)
|
117
|
+
response[:success] #=> true
|
118
|
+
response[:body] #=> "{\"hi\":\"hello\"}"
|
96
119
|
```
|
97
120
|
|
98
121
|
## Request API
|
@@ -127,6 +150,11 @@ Defines a request expectation using WebMock's `stub_request`. Returns an instanc
|
|
127
150
|
* **base_url**: Base URL of the request. Required.
|
128
151
|
* **method**: HTTP method of the request(e.g :get, :post, :patch). Required.
|
129
152
|
* **path**: Path to be appended to the :base_url. Optional.
|
153
|
+
* **request**: request details which will be passed to `WebMock::RequestStub#with` if provided. Optional
|
154
|
+
* **response**: response details which will be passed to `WebMock::RequestStub#to_return` if provided. Optional
|
155
|
+
|
156
|
+
### RestAPIBuilder::WebMockRequestExpectations.expect_json_execute(options)
|
157
|
+
A convenience shortcut for `#json_execute` which will convert `request[:body]` to JSON if it's provided
|
130
158
|
|
131
159
|
## License
|
132
160
|
MIT
|
@@ -6,8 +6,21 @@ module RestAPIBuilder
|
|
6
6
|
include WebMock::API
|
7
7
|
include RestAPIBuilder::UrlHelper
|
8
8
|
|
9
|
-
def
|
10
|
-
|
9
|
+
def expect_json_execute(response: nil, **options)
|
10
|
+
if response && response[:body]
|
11
|
+
response = response.merge(body: JSON.generate(response[:body]))
|
12
|
+
end
|
13
|
+
|
14
|
+
expect_execute(**options, response: response)
|
15
|
+
end
|
16
|
+
|
17
|
+
def expect_execute(base_url:, method:, path: nil, request: nil, response: nil)
|
18
|
+
expectation = stub_request(method, full_url(base_url, path))
|
19
|
+
|
20
|
+
expectation.with(request) if request
|
21
|
+
expectation.to_return(response) if response
|
22
|
+
|
23
|
+
expectation
|
11
24
|
end
|
12
25
|
end
|
13
26
|
|
data/rest_api_builder.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'rest_api_builder'
|
3
|
-
s.version = '0.0.
|
3
|
+
s.version = '0.0.2'
|
4
4
|
s.summary = "A simple wrapper for rest-client"
|
5
5
|
s.description = "A simple wrapper for rest-client aiming to make creation and testing of API clients easier."
|
6
6
|
s.authors = ["Alexey D"]
|