contracto 0.2.1 → 0.3.0
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/.gitignore +1 -0
- data/lib/contracto/constants.rb +5 -0
- data/lib/contracto/contract/response.rb +28 -0
- data/lib/contracto/contract.rb +18 -3
- data/lib/contracto/errors.rb +6 -0
- data/lib/contracto/version.rb +1 -1
- data/script/start_from_remote.sh +4 -1
- data/spec/fixtures/contract.con.json +54 -0
- data/spec/fixtures/users/1.json +5 -0
- data/spec/fixtures/users/2.json +5 -0
- data/spec/fixtures/users.json +12 -0
- data/spec/spec_helper.rb +6 -49
- data/spec/unit/contract_spec.rb +43 -0
- metadata +13 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6906fdf3eeb86872623a497a3c848f613de27402
|
4
|
+
data.tar.gz: 110588c7d3f1faafbc8feb1a9e57f74877accd8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 715b95910d6b0a6f6a76e92cf39d2d983be8a93dc9fd346cc1983c66808cd2836d03c545d959624c832d5aa934d5e2d07f8fdc84b442beea4920f3dace714b58
|
7
|
+
data.tar.gz: 96fa41f07be8dcc22a650d40366b1af3bbb7fa13b881d19868edbd84604ad37a2f789f3e167bbb4afbe78bbe560893bbb9ad96a277d8656bf562f2e8a048b11f
|
data/.gitignore
CHANGED
data/lib/contracto/constants.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module Contracto::Constants
|
2
2
|
GEM_DIR = Gem::Specification.find_by_name('contracto').gem_dir
|
3
|
+
ROOT_DIR = FileUtils.pwd
|
3
4
|
CONTRACTO_DIR = '.contracto'
|
4
5
|
CONTRACTO_TMP_DIR = '.tmp.contracto'
|
5
6
|
RUBY_SERVER_DIR = "#{GEM_DIR}/lib/contracto/server/ruby"
|
@@ -11,6 +12,10 @@ module Contracto::Constants
|
|
11
12
|
GEM_DIR
|
12
13
|
end
|
13
14
|
|
15
|
+
def root_dir
|
16
|
+
ROOT_DIR
|
17
|
+
end
|
18
|
+
|
14
19
|
def contracto_dir
|
15
20
|
CONTRACTO_DIR
|
16
21
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class Contracto::Contract::Response
|
2
|
+
|
3
|
+
include Contracto::Constants
|
4
|
+
|
5
|
+
def initialize(hash)
|
6
|
+
@hash = hash
|
7
|
+
end
|
8
|
+
|
9
|
+
def params
|
10
|
+
@hash.fetch('request')['params'] #TODO: should it be optional or required?
|
11
|
+
end
|
12
|
+
|
13
|
+
def body_path
|
14
|
+
@hash.fetch('response').fetch('body_path')
|
15
|
+
end
|
16
|
+
|
17
|
+
def params_matches?(other_params)
|
18
|
+
return true if params.nil?
|
19
|
+
|
20
|
+
params.keys.all? do |key|
|
21
|
+
other_params[key] == params[key].to_s
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def body
|
26
|
+
File.read(root_dir + body_path)
|
27
|
+
end
|
28
|
+
end
|
data/lib/contracto/contract.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
class Contracto::Contract
|
2
|
+
require_relative 'contract/response'
|
3
|
+
|
2
4
|
def initialize(hash)
|
3
5
|
@hash = hash
|
4
|
-
@request
|
6
|
+
@request = Contracto::Contract::Request.new(@hash.fetch('request'))
|
7
|
+
@responses = Contracto::Contract::Responses.new(@hash.fetch('responses'))
|
5
8
|
end
|
6
9
|
|
7
10
|
def http_method
|
@@ -13,9 +16,21 @@ class Contracto::Contract
|
|
13
16
|
end
|
14
17
|
|
15
18
|
def response_body(params)
|
16
|
-
params
|
19
|
+
response = @responses.find_by_params(params)
|
20
|
+
raise Contracto::ResponseNotFoundError.new(params) unless response
|
21
|
+
response.body
|
17
22
|
end
|
18
|
-
|
23
|
+
|
24
|
+
class Contracto::Contract::Responses
|
25
|
+
def initialize(responses)
|
26
|
+
@responses = responses.map { |response| Contracto::Contract::Response.new(response) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def find_by_params(params)
|
30
|
+
@responses.find { |response| response.params_matches?(params) }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
19
34
|
class Contracto::Contract::Request
|
20
35
|
def initialize(hash)
|
21
36
|
@hash = hash
|
data/lib/contracto/errors.rb
CHANGED
@@ -9,3 +9,9 @@ class Contracto::ServerAlreadyRunningError < StandardError
|
|
9
9
|
super 'Could not start: Contracto server is already running'
|
10
10
|
end
|
11
11
|
end
|
12
|
+
|
13
|
+
class Contracto::ResponseNotFoundError < StandardError
|
14
|
+
def initialize(params)
|
15
|
+
super "Could not find response for params: #{params}"
|
16
|
+
end
|
17
|
+
end
|
data/lib/contracto/version.rb
CHANGED
data/script/start_from_remote.sh
CHANGED
@@ -0,0 +1,54 @@
|
|
1
|
+
[
|
2
|
+
{
|
3
|
+
"request": {
|
4
|
+
"http_method": "get",
|
5
|
+
"path": "/users"
|
6
|
+
},
|
7
|
+
"responses": [
|
8
|
+
{
|
9
|
+
"request": {
|
10
|
+
"headers": {
|
11
|
+
"Content-Type": "application/json"
|
12
|
+
}
|
13
|
+
},
|
14
|
+
"response": {
|
15
|
+
"body_path": "/users.json"
|
16
|
+
}
|
17
|
+
}
|
18
|
+
]
|
19
|
+
},
|
20
|
+
{
|
21
|
+
"request": {
|
22
|
+
"http_method": "get",
|
23
|
+
"path": "/users/:id"
|
24
|
+
},
|
25
|
+
"responses": [
|
26
|
+
{
|
27
|
+
"request": {
|
28
|
+
"headers": {
|
29
|
+
"Content-Type": "application/json"
|
30
|
+
},
|
31
|
+
"params": {
|
32
|
+
"id": 1
|
33
|
+
}
|
34
|
+
},
|
35
|
+
"response": {
|
36
|
+
"body_path": "/users/1.json"
|
37
|
+
}
|
38
|
+
},
|
39
|
+
{
|
40
|
+
"request": {
|
41
|
+
"headers": {
|
42
|
+
"Content-Type": "application/json"
|
43
|
+
},
|
44
|
+
"params": {
|
45
|
+
"id": 2
|
46
|
+
}
|
47
|
+
},
|
48
|
+
"response": {
|
49
|
+
"body_path": "/users/2.json"
|
50
|
+
}
|
51
|
+
}
|
52
|
+
]
|
53
|
+
}
|
54
|
+
]
|
data/spec/spec_helper.rb
CHANGED
@@ -1,52 +1,9 @@
|
|
1
1
|
require_relative '../lib/contracto'
|
2
2
|
|
3
|
-
STRINGS_WITH_JSON = []
|
4
|
-
STRINGS_WITH_JSON << <<JSON
|
5
|
-
{
|
6
|
-
"request": {
|
7
|
-
"http_method": "get",
|
8
|
-
"path": "/api/users"
|
9
|
-
},
|
10
|
-
"examples": [
|
11
|
-
{
|
12
|
-
"request": {
|
13
|
-
"headers": {
|
14
|
-
"Content-Type": "application/json"
|
15
|
-
},
|
16
|
-
"body": {
|
17
|
-
"firstName": "Max",
|
18
|
-
"lastName": "Lincoln"
|
19
|
-
}
|
20
|
-
},
|
21
|
-
"response": {
|
22
|
-
"body": { }
|
23
|
-
}
|
24
|
-
}
|
25
|
-
]
|
26
|
-
}
|
27
|
-
JSON
|
3
|
+
STRINGS_WITH_JSON = [File.read('spec/fixtures/contract.con.json')]
|
28
4
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
},
|
35
|
-
"examples": [
|
36
|
-
{
|
37
|
-
"request": {
|
38
|
-
"headers": {
|
39
|
-
"Content-Type": "application/json"
|
40
|
-
},
|
41
|
-
"body": {
|
42
|
-
"firstName": "Max",
|
43
|
-
"lastName": "Lincoln"
|
44
|
-
}
|
45
|
-
},
|
46
|
-
"response": {
|
47
|
-
"body": { }
|
48
|
-
}
|
49
|
-
}
|
50
|
-
]
|
51
|
-
}
|
52
|
-
JSON
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.before(:each) do
|
7
|
+
allow_any_instance_of(Contracto::Constants).to receive(:root_dir).and_return FileUtils.pwd + '/spec/fixtures'
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Contracto::Contract do
|
4
|
+
|
5
|
+
context '#http_method' do
|
6
|
+
let(:contract) { contracts[1] }
|
7
|
+
subject { contract.http_method }
|
8
|
+
|
9
|
+
it { expect(subject).to eq 'get' }
|
10
|
+
end
|
11
|
+
|
12
|
+
context '#url_pattern' do
|
13
|
+
let(:contract) { contracts[1] }
|
14
|
+
subject { contract.url_pattern }
|
15
|
+
|
16
|
+
it { expect(subject).to eq '/users/:id' }
|
17
|
+
end
|
18
|
+
|
19
|
+
context '#response_body' do
|
20
|
+
|
21
|
+
context 'by params' do
|
22
|
+
|
23
|
+
let(:contract) { contracts[1] }
|
24
|
+
let(:response_0) { JSON.parse contract.response_body('id' => '1', 'foo' => 'bar') }
|
25
|
+
let(:response_1) { JSON.parse contract.response_body('id' => '2') }
|
26
|
+
|
27
|
+
it 'should return body based on params' do
|
28
|
+
expect(response_0).to eq('first_name' => 'Albert', 'last_name' => 'Einstein', 'age' => 30)
|
29
|
+
expect(response_1).to eq('first_name' => 'Kurt', 'last_name' => 'Godel', 'age' => 35)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should raise "ResponseNotFound" exception if no response matches params' do
|
33
|
+
expect{ contract.response_body('id' => '-1') }.to raise_error(Contracto::ResponseNotFoundError)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
def contracts
|
40
|
+
Contracto::Parser.new(STRINGS_WITH_JSON).contracts
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: contracto
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kacper Walanus
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sinatra
|
@@ -91,6 +91,7 @@ files:
|
|
91
91
|
- lib/contracto/config.rb
|
92
92
|
- lib/contracto/constants.rb
|
93
93
|
- lib/contracto/contract.rb
|
94
|
+
- lib/contracto/contract/response.rb
|
94
95
|
- lib/contracto/errors.rb
|
95
96
|
- lib/contracto/parser.rb
|
96
97
|
- lib/contracto/server/ruby/config.ru
|
@@ -101,7 +102,12 @@ files:
|
|
101
102
|
- lib/contracto/version.rb
|
102
103
|
- script/start_from_remote.sh
|
103
104
|
- script/start_locally.sh
|
105
|
+
- spec/fixtures/contract.con.json
|
106
|
+
- spec/fixtures/users.json
|
107
|
+
- spec/fixtures/users/1.json
|
108
|
+
- spec/fixtures/users/2.json
|
104
109
|
- spec/spec_helper.rb
|
110
|
+
- spec/unit/contract_spec.rb
|
105
111
|
- spec/unit/parser_spec.rb
|
106
112
|
homepage: ''
|
107
113
|
licenses:
|
@@ -128,5 +134,10 @@ signing_key:
|
|
128
134
|
specification_version: 4
|
129
135
|
summary: XXX
|
130
136
|
test_files:
|
137
|
+
- spec/fixtures/contract.con.json
|
138
|
+
- spec/fixtures/users.json
|
139
|
+
- spec/fixtures/users/1.json
|
140
|
+
- spec/fixtures/users/2.json
|
131
141
|
- spec/spec_helper.rb
|
142
|
+
- spec/unit/contract_spec.rb
|
132
143
|
- spec/unit/parser_spec.rb
|