poisol 0.0.8 → 0.0.9
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/lib/poisol/domain.rb +6 -3
- data/lib/poisol/extensions/webmock_extensions.rb +11 -0
- data/lib/poisol/stub/request/query_builder.rb +11 -0
- data/lib/poisol/stub/request/request_body_builder.rb +12 -0
- data/lib/poisol/stub/request/url_builder.rb +17 -0
- data/lib/poisol/{template/response_handler.rb → stub/response/response_body_builder.rb} +22 -40
- data/lib/poisol/stub/response/status_builder.rb +3 -0
- data/lib/poisol/stub/stub.rb +57 -0
- data/lib/poisol/stub/stub_class_builder.rb +35 -0
- data/lib/poisol/stub/webmock_stub_builder.rb +40 -0
- data/lib/poisol/stub_config/stub_config.rb +11 -0
- data/lib/poisol/stub_config/stub_config_builder.rb +100 -0
- data/lib/poisol/stub_factory.rb +26 -15
- data/lib/poisol/utils/file_util.rb +6 -11
- data/lib/poisol/utils/logger.rb +25 -0
- data/lib/poisol/utils/parse.rb +8 -8
- data/lib/poisol.rb +17 -0
- data/spec/functional/array_spec.rb +1 -1
- data/spec/functional/dumb_response_spec.rb +1 -1
- data/spec/functional/get_spec.rb +1 -1
- data/spec/functional/key_value/explicit_inclusion_spec.rb +1 -1
- data/spec/functional/key_value/implicit_inclusion_spec.rb +1 -1
- data/spec/functional/multi_domain_spec.rb +1 -1
- data/spec/functional/nested_array_spec.rb +1 -1
- data/spec/functional/post_spec.rb +1 -1
- data/spec/functional/url_spec.rb +1 -1
- data/spec/unit/exception_spec.rb +5 -0
- data/spec/unit/stub_spec.rb +6 -0
- metadata +18 -9
- data/lib/poisol/stub_config.rb +0 -95
- data/lib/poisol/template/base_template.rb +0 -29
- data/lib/poisol/template/build.rb +0 -29
- data/lib/poisol/template/query_handler.rb +0 -11
- data/lib/poisol/template/request_handler.rb +0 -14
- data/lib/poisol/template/url_handler.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c6cd9d6bddc3b3a855b003566ef8ce6767c1d0c
|
4
|
+
data.tar.gz: 39307a21d0f273ef3ea78a70accaf753d14e83c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56c27ca1e388144a37b04de81e9c09bfa372aa3b030b7a3eb76c31b07c02e8a60d860b52ce3d037cd6b8a7a9245e6a685ccf33af8e434b7dca44714c89a84672
|
7
|
+
data.tar.gz: f54cdbfbd7fc29eb142bc668ef342da63601541b0235fa3cc5d155f5726e69d012fa3b959f0ba8b088d4545f32804e499a14f66e2ab934adc2f62584c333d56e
|
data/lib/poisol/domain.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
class Domain
|
2
|
-
|
3
|
-
|
2
|
+
attr_reader :file,:full_url
|
3
|
+
|
4
|
+
def initialize domain_config_file
|
5
|
+
@file = domain_config_file
|
6
|
+
base_hash = Parse.yaml_file @file
|
4
7
|
domain = base_hash["domain"]
|
5
8
|
port = base_hash["port"]
|
6
|
-
"#{domain.chomp('\\')}#{ port.present? ? ":#{port}" : "" }"
|
9
|
+
@full_url = "#{domain.chomp('\\')}#{ port.present? ? ":#{port}" : "" }"
|
7
10
|
end
|
8
11
|
|
9
12
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module WebMock
|
2
|
+
class NetConnectNotAllowedError
|
3
|
+
alias real_init initialize
|
4
|
+
def initialize(*args)
|
5
|
+
exception = real_init *args
|
6
|
+
request = args[0]
|
7
|
+
PoisolLog.error "**************Failed************\n#{request.method}:#{request.uri}\n#{request.body if request.body.present?}"
|
8
|
+
exception
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module RequestBodyBuilder
|
2
|
+
def generate_request_methods
|
3
|
+
request_body = @stub_config.request.body.deep_dup
|
4
|
+
request_body.each do |field|
|
5
|
+
field_name = field[0]
|
6
|
+
define_method("by_#{field[0].underscore}") do |*value|
|
7
|
+
@request.body[field_name] = value[0]
|
8
|
+
self
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module UrlBuilder
|
2
|
+
def generate_methods_to_alter_path_params
|
3
|
+
url = @stub_config.request.url
|
4
|
+
url.scan(/{(.+?)}/).each do |path_params|
|
5
|
+
path_param = path_params[0]
|
6
|
+
param_name = path_param.split("|")[0]
|
7
|
+
param_default_value = path_param.split("|")[1]
|
8
|
+
method_name = "for_#{param_name.underscore}"
|
9
|
+
define_method(method_name) do |*input_value|
|
10
|
+
input_value = input_value[0]
|
11
|
+
@request.url.sub!("{#{path_param}}","{#{param_name}|#{input_value}}") unless input_value.blank?
|
12
|
+
self
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
|
-
module
|
1
|
+
module ResponseBodyBuilder
|
2
2
|
|
3
3
|
def prepare_response
|
4
|
-
return if @
|
5
|
-
if @
|
4
|
+
return if @stub_config.response.body.blank?
|
5
|
+
if @stub_config.response.is_column_array or @stub_config.response.is_row_array
|
6
6
|
generate_methods_to_alter_response_array
|
7
7
|
else
|
8
8
|
generate_methods_to_alter_response_object
|
@@ -10,41 +10,39 @@ module ClassTemplate
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def generate_methods_to_alter_response_array
|
13
|
-
@
|
14
|
-
@response_body = []
|
13
|
+
@response_array_item = @stub_config.response.body
|
15
14
|
generate_method_to_append_response_array
|
16
15
|
generate_method_to_alter_response_array_object
|
17
16
|
end
|
18
17
|
|
19
18
|
def generate_method_to_alter_response_array_object
|
20
|
-
@
|
19
|
+
@response_array_item.each do |field|
|
21
20
|
field_name = field[0]
|
22
21
|
actual_field_value = field[1]
|
23
22
|
is_array = (actual_field_value.class.to_s == "Array")
|
24
23
|
actual_field_value = actual_field_value[0] if is_array
|
25
|
-
@response_body.last[field_name] = [] if is_array
|
26
24
|
method_name = is_array ? ("with_#{field_name.classify.underscore}") : ("with_#{field_name.underscore}")
|
27
|
-
|
25
|
+
define_method(method_name) do |*input_value|
|
28
26
|
input_value = input_value[0]
|
29
27
|
assignment_value = get_assignment_value actual_field_value,input_value
|
30
|
-
@
|
28
|
+
@response.body.last[field_name] = is_array ? (@response.body.last[field_name] << assignment_value) : assignment_value
|
31
29
|
self
|
32
30
|
end
|
33
31
|
end
|
34
32
|
end
|
35
33
|
|
36
34
|
def generate_method_to_append_response_array
|
37
|
-
|
38
|
-
method_name = "has_#{
|
39
|
-
|
40
|
-
@
|
35
|
+
class_name = self.name.classify.underscore
|
36
|
+
method_name = "has_#{class_name}"
|
37
|
+
define_method(method_name) do
|
38
|
+
@response.body << stub_config.response.body.deep_dup
|
41
39
|
self
|
42
40
|
end
|
43
41
|
end
|
44
42
|
|
45
43
|
def generate_methods_to_alter_response_object
|
46
|
-
|
47
|
-
|
44
|
+
response_body = @stub_config.response.body.clone
|
45
|
+
response_body.each do |field|
|
48
46
|
field_name = field[0]
|
49
47
|
actual_field_value = field[1]
|
50
48
|
is_array = (actual_field_value.class.to_s == "Array")
|
@@ -57,56 +55,40 @@ module ClassTemplate
|
|
57
55
|
end
|
58
56
|
end
|
59
57
|
|
60
|
-
def set_dumb_response response_file
|
61
|
-
@response_body = Parse.json_file_to_hash(response_file)
|
62
|
-
@config.response.is_column_array = false
|
63
|
-
@config.response.is_row_array = false
|
64
|
-
self
|
65
|
-
end
|
66
|
-
|
67
58
|
def generate_method_to_alter_response_field_array field_name,actual_field_values
|
68
59
|
actual_field_value = actual_field_values[0]
|
69
60
|
method_name = "has_#{field_name.classify.underscore}"
|
70
|
-
|
61
|
+
define_method(method_name) do |*input_value|
|
71
62
|
input_value = input_value[0]
|
72
63
|
assignment_value = get_assignment_value actual_field_value,input_value
|
73
|
-
@
|
64
|
+
@response.body[field_name] = [assignment_value]
|
74
65
|
self
|
75
66
|
end
|
76
67
|
|
77
68
|
method_name = "has_another_#{field_name.classify.underscore}"
|
78
|
-
|
69
|
+
define_method(method_name) do |*input_value|
|
79
70
|
input_value = input_value[0]
|
80
71
|
assignment_value = get_assignment_value actual_field_value,input_value
|
81
|
-
@
|
72
|
+
@response.body[field_name] = @response.body[field_name] << assignment_value
|
82
73
|
self
|
83
74
|
end
|
84
75
|
|
85
76
|
method_name = "has_no_#{field_name.classify.underscore}"
|
86
|
-
|
87
|
-
@
|
77
|
+
define_method(method_name) do
|
78
|
+
@response.body[field_name] = []
|
88
79
|
self
|
89
80
|
end
|
90
81
|
end
|
91
82
|
|
92
83
|
def generate_method_to_alter_response_field field_name,actual_field_value
|
93
84
|
method_name = "has_#{field_name.underscore}"
|
94
|
-
|
85
|
+
define_method(method_name) do |*input_value|
|
95
86
|
input_value = input_value[0]
|
96
87
|
assignment_value = get_assignment_value actual_field_value,input_value
|
97
|
-
@
|
88
|
+
@response.body[field_name] = assignment_value
|
98
89
|
self
|
99
90
|
end
|
100
91
|
end
|
101
92
|
|
102
93
|
|
103
|
-
|
104
|
-
if actual_field_value.class.to_s == "Hash"
|
105
|
-
input_value = {} if input_value.blank?
|
106
|
-
actual_field_value.deep_merge(input_value)
|
107
|
-
else
|
108
|
-
input_value
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
end
|
94
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require_relative "webmock_stub_builder"
|
2
|
+
require_relative "stub_class_builder"
|
3
|
+
|
4
|
+
class Stub
|
5
|
+
include WebMockStubBuilder
|
6
|
+
attr_accessor :request,:response
|
7
|
+
class << self
|
8
|
+
include StubClassBuilder
|
9
|
+
|
10
|
+
def set_stub_config stub_config
|
11
|
+
@stub_config = stub_config
|
12
|
+
define_method("stub_config") do
|
13
|
+
return stub_config
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
@request = Request.new
|
20
|
+
@response = Response.new
|
21
|
+
@request.url = stub_config.request.url.deep_dup
|
22
|
+
@request.query = stub_config.request.query.deep_dup
|
23
|
+
@request.body = stub_config.request.body.deep_dup
|
24
|
+
@request.body = {} if stub_config.request.include_explicit_only
|
25
|
+
if stub_config.response.is_column_array or stub_config.response.is_row_array
|
26
|
+
@response.body = []
|
27
|
+
else
|
28
|
+
@response.body = stub_config.response.body.deep_dup
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def set_dumb_response response_file
|
33
|
+
@response.body = Parse.json_file_to_hash(response_file)
|
34
|
+
@is_response_dumped = true
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
def get_assignment_value actual_field_value,input_value
|
39
|
+
if actual_field_value.class.to_s == "Hash"
|
40
|
+
input_value = {} if input_value.blank?
|
41
|
+
actual_field_value.deep_merge(input_value)
|
42
|
+
else
|
43
|
+
input_value
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
class Request
|
50
|
+
attr_accessor :url,:query,:body
|
51
|
+
end
|
52
|
+
|
53
|
+
class Response
|
54
|
+
attr_accessor :body,:status
|
55
|
+
end
|
56
|
+
|
57
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require_relative "request/query_builder"
|
2
|
+
require_relative "request/request_body_builder"
|
3
|
+
require_relative "request/url_builder"
|
4
|
+
require_relative "response/response_body_builder"
|
5
|
+
require_relative "response/status_builder"
|
6
|
+
|
7
|
+
module StubClassBuilder
|
8
|
+
include RequestBodyBuilder
|
9
|
+
include ResponseBodyBuilder
|
10
|
+
include UrlBuilder
|
11
|
+
include QueryBuilder
|
12
|
+
|
13
|
+
def generate_methods_to_alter_sutb
|
14
|
+
prepare_request
|
15
|
+
prepare_response
|
16
|
+
end
|
17
|
+
|
18
|
+
def prepare_request
|
19
|
+
prepare_request_url
|
20
|
+
prepare_request_query
|
21
|
+
prepare_request_body
|
22
|
+
end
|
23
|
+
|
24
|
+
def prepare_request_url
|
25
|
+
generate_methods_to_alter_path_params
|
26
|
+
end
|
27
|
+
|
28
|
+
def prepare_request_query
|
29
|
+
generate_query_methods if @stub_config.request.query.present?
|
30
|
+
end
|
31
|
+
|
32
|
+
def prepare_request_body
|
33
|
+
generate_request_methods if @stub_config.request.body.present?
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module WebMockStubBuilder
|
2
|
+
def build
|
3
|
+
build_url
|
4
|
+
build_query
|
5
|
+
build_request_body
|
6
|
+
build_response_body
|
7
|
+
return @webmock_stub
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
def build_url
|
12
|
+
remove_path_param_name_from_url
|
13
|
+
@webmock_stub = stub_request(stub_config.request.type, "http://#{stub_config.request.domain}/#{@request.url}")
|
14
|
+
end
|
15
|
+
|
16
|
+
def build_query
|
17
|
+
@webmock_stub.with(:query => @request.query) unless @request.query.eql? ""
|
18
|
+
end
|
19
|
+
|
20
|
+
def build_request_body
|
21
|
+
return if @request.body.blank?
|
22
|
+
@request.body = Parse.hash_to_concatenated_key_value(@request.body) if stub_config.request.is_body_key_value
|
23
|
+
@webmock_stub.with(:body => @request.body)
|
24
|
+
end
|
25
|
+
|
26
|
+
def build_response_body
|
27
|
+
@response.body = Parse.hash_array_to_column_hash(@response.body) if stub_config.response.is_column_array and !@is_response_dumped.present?
|
28
|
+
@webmock_stub.to_return(:status => 200, :body => @response.body.to_json, :headers => {'Content-Type' => 'application/json'})
|
29
|
+
end
|
30
|
+
|
31
|
+
def remove_path_param_name_from_url
|
32
|
+
@request.url.scan(/{(.+?)}/).each do |path_params|
|
33
|
+
path_param = path_params[0]
|
34
|
+
param_name = path_param.split("|")[0]
|
35
|
+
param_value = path_param.split("|")[1]
|
36
|
+
@request.url.sub!("{#{path_param}}",param_value)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class StubConfig
|
2
|
+
attr_accessor :response,:request,:is_inline,:file
|
3
|
+
end
|
4
|
+
|
5
|
+
class RequestConfig
|
6
|
+
attr_accessor :domain,:url,:type,:query,:body,:is_body_key_value,:include_explicit_only
|
7
|
+
end
|
8
|
+
|
9
|
+
class ResponseConfig
|
10
|
+
attr_accessor :body,:is_column_array,:is_row_array
|
11
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
class StubConfigBuilder
|
2
|
+
|
3
|
+
def initialize
|
4
|
+
@stub_config = StubConfig.new
|
5
|
+
@stub_config.request = RequestConfig.new
|
6
|
+
@stub_config.response = ResponseConfig.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def with_file file_name
|
10
|
+
@stub_config.file = file_name
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def with_domain domain
|
15
|
+
@stub_config.request.domain = domain
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
def is_inline
|
20
|
+
@stub_config.is_inline = true
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def is_exploded
|
25
|
+
@stub_config.is_inline = false
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def build
|
30
|
+
@raw_config_hash = Parse.yaml_file @stub_config.file
|
31
|
+
build_request
|
32
|
+
build_response
|
33
|
+
return @stub_config
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
def build_request
|
38
|
+
load_url
|
39
|
+
@stub_config.request.type = @raw_config_hash["request"]["type"].intern
|
40
|
+
@stub_config.request.query = @raw_config_hash["request"]["query"]
|
41
|
+
load_request_body_filed_implicit_option
|
42
|
+
@stub_config.is_inline ? load_inline_request_body : load_exploaded_request_body
|
43
|
+
end
|
44
|
+
|
45
|
+
def load_url
|
46
|
+
url = @raw_config_hash["request"]["url"]
|
47
|
+
url.strip!
|
48
|
+
url.sub!("/","") if url[0].eql? "/"
|
49
|
+
@stub_config.request.url = url
|
50
|
+
end
|
51
|
+
|
52
|
+
def load_request_body_filed_implicit_option
|
53
|
+
include_explicit_only = @raw_config_hash["request"]["include_explicit_only"]
|
54
|
+
@stub_config.request.include_explicit_only = include_explicit_only.blank? ? false : include_explicit_only
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
def build_response
|
59
|
+
@stub_config.is_inline ? load_inline_response_body : load_exploaded_response_body
|
60
|
+
load_resonse_array_type
|
61
|
+
end
|
62
|
+
|
63
|
+
def load_resonse_array_type
|
64
|
+
return if @raw_config_hash["response"].blank?
|
65
|
+
array_type = @raw_config_hash["response"]["array_type"]
|
66
|
+
return if array_type.blank?
|
67
|
+
@stub_config.response.is_column_array = true if array_type.eql? "column"
|
68
|
+
@stub_config.response.is_row_array = true if array_type.eql? "row"
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
def load_inline_response_body
|
73
|
+
raw_body = @raw_config_hash["response"]["body"]
|
74
|
+
return if raw_body.blank?
|
75
|
+
@stub_config.response.body = Parse.json_to_hash raw_body
|
76
|
+
end
|
77
|
+
|
78
|
+
def load_inline_request_body
|
79
|
+
raw_body = @raw_config_hash["request"]["body"]
|
80
|
+
return if raw_body.blank?
|
81
|
+
if raw_body.class.name == "String"
|
82
|
+
@stub_config.request.body = Parse.json_to_hash raw_body
|
83
|
+
else
|
84
|
+
@stub_config.request.is_body_key_value = true
|
85
|
+
@stub_config.request.body = raw_body
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def load_exploaded_request_body
|
90
|
+
request_file = "#{File.dirname @stub_config.file}/request.json"
|
91
|
+
return unless File.exists? request_file
|
92
|
+
@stub_config.request.body = Parse.json_file_to_hash(request_file)
|
93
|
+
end
|
94
|
+
|
95
|
+
def load_exploaded_response_body
|
96
|
+
response_file = "#{File.dirname @stub_config.file}/response.json"
|
97
|
+
return unless File.exists? response_file
|
98
|
+
@stub_config.response.body = Parse.json_file_to_hash(response_file)
|
99
|
+
end
|
100
|
+
end
|
data/lib/poisol/stub_factory.rb
CHANGED
@@ -1,36 +1,47 @@
|
|
1
|
+
require_relative "stub/stub"
|
2
|
+
|
1
3
|
class StubFactory
|
2
4
|
def build folder
|
3
|
-
folder.chomp
|
4
|
-
|
5
|
-
|
6
|
-
inline_configs = Dir["#{folder}/**/*.yml"] - ( (explolded_configs.nil?) ? [] : explolded_configs) - [domain_config]
|
7
|
-
@domain = Domain.new.load domain_config
|
8
|
-
generate_exploded_config explolded_configs unless explolded_configs.nil?
|
9
|
-
generate_inline_config inline_configs unless inline_configs.nil?
|
5
|
+
@folder = folder.chomp '/'
|
6
|
+
load_domain
|
7
|
+
load_stub_configs
|
10
8
|
end
|
11
9
|
|
12
10
|
private
|
13
11
|
|
12
|
+
def load_domain
|
13
|
+
domain_config_file = Dir["#{@folder}/domain.yml"].first
|
14
|
+
@domain = Domain.new domain_config_file
|
15
|
+
end
|
16
|
+
|
17
|
+
def load_stub_configs
|
18
|
+
explolded_configs = Dir["#{@folder}/**/config.yml"]
|
19
|
+
inline_configs = Dir["#{@folder}/**/*.yml"] - ( (explolded_configs.nil?) ? [] : explolded_configs) - [@domain.file]
|
20
|
+
generate_exploded_config explolded_configs unless explolded_configs.nil?
|
21
|
+
generate_inline_config inline_configs unless inline_configs.nil?
|
22
|
+
end
|
23
|
+
|
14
24
|
|
15
25
|
def generate_exploded_config explolded_configs
|
16
26
|
explolded_configs.each do |config_file|
|
17
|
-
dynamic_name =
|
18
|
-
config =
|
27
|
+
dynamic_name = (FileName.get_dir_name config_file).camelize
|
28
|
+
config = StubConfigBuilder.new.is_exploded.with_file(config_file).with_domain(@domain.full_url).build
|
19
29
|
create_class dynamic_name,config
|
20
30
|
end
|
21
31
|
end
|
22
32
|
|
23
33
|
def generate_inline_config inline_configs
|
24
34
|
inline_configs.each do |config_file|
|
25
|
-
dynamic_name =
|
26
|
-
|
27
|
-
create_class dynamic_name,
|
35
|
+
dynamic_name = (FileName.get_file_name config_file).camelize
|
36
|
+
stub_config = StubConfigBuilder.new.is_inline.with_file(config_file).with_domain(@domain.full_url).build
|
37
|
+
create_class dynamic_name,stub_config
|
28
38
|
end
|
29
39
|
end
|
30
40
|
|
31
|
-
def create_class class_name,
|
32
|
-
|
33
|
-
|
41
|
+
def create_class class_name,stub_config
|
42
|
+
dynamic_stub_class = Object.const_set class_name,Class.new(Stub)
|
43
|
+
dynamic_stub_class.set_stub_config stub_config
|
44
|
+
dynamic_stub_class.generate_methods_to_alter_sutb
|
34
45
|
end
|
35
46
|
|
36
47
|
end
|
@@ -1,17 +1,12 @@
|
|
1
|
-
|
1
|
+
module FileName
|
2
|
+
extend self
|
2
3
|
|
3
|
-
def
|
4
|
-
|
4
|
+
def get_dir_name file_name
|
5
|
+
File.basename(File.dirname file_name)
|
5
6
|
end
|
6
7
|
|
7
|
-
def
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
def self.titilize input
|
14
|
-
(input).capitalize.split('_').reduce{|c,a| c+a.capitalize}
|
8
|
+
def get_file_name file_name
|
9
|
+
((File.basename file_name).chomp ".yml")
|
15
10
|
end
|
16
11
|
|
17
12
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class PoisolLog
|
2
|
+
class << self
|
3
|
+
|
4
|
+
def logger
|
5
|
+
FileUtils.mkdir_p "log" unless File.exists?("log")
|
6
|
+
@poisol_logger ||= Logger.new("log/poisol_stub.log")
|
7
|
+
@poisol_logger.formatter = proc { |severity, datetime, progname, msg| "#{msg}\n" }
|
8
|
+
@poisol_logger.level = Logger::INFO
|
9
|
+
@poisol_logger
|
10
|
+
end
|
11
|
+
|
12
|
+
def error message
|
13
|
+
logger.error "#{message}\n"
|
14
|
+
end
|
15
|
+
|
16
|
+
def info message
|
17
|
+
logger.info "#{message}\n"
|
18
|
+
end
|
19
|
+
|
20
|
+
def warn message
|
21
|
+
logger.warn "#{message}\n"
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
data/lib/poisol/utils/parse.rb
CHANGED
@@ -1,18 +1,19 @@
|
|
1
|
-
|
1
|
+
module Parse
|
2
|
+
extend self
|
2
3
|
|
3
|
-
def
|
4
|
+
def json_file_to_hash file_name
|
4
5
|
JSON.parse File.read(file_name)
|
5
6
|
end
|
6
7
|
|
7
|
-
def
|
8
|
+
def yaml_file file_name
|
8
9
|
YAML.load_file(file_name)
|
9
10
|
end
|
10
11
|
|
11
|
-
def
|
12
|
+
def json_to_hash json
|
12
13
|
JSON.parse json
|
13
14
|
end
|
14
15
|
|
15
|
-
def
|
16
|
+
def hash_array_to_column_hash hash_array
|
16
17
|
return [] if hash_array.blank?
|
17
18
|
column_hash = Hash.new
|
18
19
|
hash_array[0].each_key { |key| column_hash.merge!(key=>[])}
|
@@ -22,13 +23,12 @@ class Parse
|
|
22
23
|
column_hash
|
23
24
|
end
|
24
25
|
|
25
|
-
def
|
26
|
+
def hash_to_concatenated_key_value hash
|
26
27
|
concatenated_body = ""
|
27
28
|
hash.each do |key,value|
|
28
|
-
|
29
|
+
concatenated_body = concatenated_body + "#{key}=#{value}&"
|
29
30
|
end
|
30
31
|
concatenated_body.chomp('&')
|
31
32
|
end
|
32
33
|
|
33
34
|
end
|
34
|
-
|
data/lib/poisol.rb
CHANGED
@@ -1,2 +1,19 @@
|
|
1
1
|
require 'active_support/all'
|
2
|
+
|
2
3
|
Dir["#{File.dirname(__FILE__)}/poisol/**/*.rb"].each { |f| require(f) }
|
4
|
+
|
5
|
+
module Poisol
|
6
|
+
extend self
|
7
|
+
|
8
|
+
def load folder
|
9
|
+
StubFactory.new.build folder
|
10
|
+
end
|
11
|
+
|
12
|
+
def log_all_calls to_domain
|
13
|
+
WebMock.after_request(:real_requests_only => false) do |req_signature, response|
|
14
|
+
if req_signature.to_s.include? to_domain
|
15
|
+
PoisolLog.info "========================\nRequest\n#{req_signature.uri}\n#{req_signature.body}\nResponse:#{response.status[0]}\n#{JSON.pretty_generate(JSON.parse(response.body)) if response.status.present?}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/spec/functional/get_spec.rb
CHANGED
data/spec/functional/url_spec.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: poisol
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Deepak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -132,15 +132,20 @@ files:
|
|
132
132
|
- lib/poisol.rb
|
133
133
|
- lib/poisol/config_map.rb
|
134
134
|
- lib/poisol/domain.rb
|
135
|
-
- lib/poisol/
|
135
|
+
- lib/poisol/extensions/webmock_extensions.rb
|
136
|
+
- lib/poisol/stub/request/query_builder.rb
|
137
|
+
- lib/poisol/stub/request/request_body_builder.rb
|
138
|
+
- lib/poisol/stub/request/url_builder.rb
|
139
|
+
- lib/poisol/stub/response/response_body_builder.rb
|
140
|
+
- lib/poisol/stub/response/status_builder.rb
|
141
|
+
- lib/poisol/stub/stub.rb
|
142
|
+
- lib/poisol/stub/stub_class_builder.rb
|
143
|
+
- lib/poisol/stub/webmock_stub_builder.rb
|
144
|
+
- lib/poisol/stub_config/stub_config.rb
|
145
|
+
- lib/poisol/stub_config/stub_config_builder.rb
|
136
146
|
- lib/poisol/stub_factory.rb
|
137
|
-
- lib/poisol/template/base_template.rb
|
138
|
-
- lib/poisol/template/build.rb
|
139
|
-
- lib/poisol/template/query_handler.rb
|
140
|
-
- lib/poisol/template/request_handler.rb
|
141
|
-
- lib/poisol/template/response_handler.rb
|
142
|
-
- lib/poisol/template/url_handler.rb
|
143
147
|
- lib/poisol/utils/file_util.rb
|
148
|
+
- lib/poisol/utils/logger.rb
|
144
149
|
- lib/poisol/utils/parse.rb
|
145
150
|
- spec/data/domain/first/domain.yml
|
146
151
|
- spec/data/domain/first/first.yml
|
@@ -167,6 +172,8 @@ files:
|
|
167
172
|
- spec/functional/post_spec.rb
|
168
173
|
- spec/functional/url_spec.rb
|
169
174
|
- spec/spec_helper.rb
|
175
|
+
- spec/unit/exception_spec.rb
|
176
|
+
- spec/unit/stub_spec.rb
|
170
177
|
homepage: https://github.com/paramadeep/poisol
|
171
178
|
licenses:
|
172
179
|
- MIT
|
@@ -207,6 +214,8 @@ test_files:
|
|
207
214
|
- spec/data/main/key_value/explicit.yml
|
208
215
|
- spec/data/main/key_value/key_value.yml
|
209
216
|
- spec/data/main/book.yml
|
217
|
+
- spec/unit/exception_spec.rb
|
218
|
+
- spec/unit/stub_spec.rb
|
210
219
|
- spec/spec_helper.rb
|
211
220
|
- spec/functional/multi_domain_spec.rb
|
212
221
|
- spec/functional/array_spec.rb
|
data/lib/poisol/stub_config.rb
DELETED
@@ -1,95 +0,0 @@
|
|
1
|
-
class StubConfig
|
2
|
-
attr_reader :response,:request
|
3
|
-
|
4
|
-
def with_file file_name
|
5
|
-
@config_yml_file = file_name
|
6
|
-
@config_yml = Parse.yaml_file file_name
|
7
|
-
self
|
8
|
-
end
|
9
|
-
|
10
|
-
def with_domain domain
|
11
|
-
@domain = domain
|
12
|
-
self
|
13
|
-
end
|
14
|
-
|
15
|
-
def is_inline
|
16
|
-
@is_inline = true
|
17
|
-
self
|
18
|
-
end
|
19
|
-
|
20
|
-
def is_exploded
|
21
|
-
@is_inline = false
|
22
|
-
self
|
23
|
-
end
|
24
|
-
|
25
|
-
def build
|
26
|
-
build_request
|
27
|
-
build_response
|
28
|
-
self
|
29
|
-
end
|
30
|
-
|
31
|
-
private
|
32
|
-
def build_request
|
33
|
-
@request = Request.new
|
34
|
-
@request.domain=@domain.clone
|
35
|
-
@request.url = @config_yml["request"]["url"]
|
36
|
-
@request.type = @config_yml["request"]["type"].intern
|
37
|
-
@request.query = @config_yml["request"]["query"]
|
38
|
-
include_explicit_only = @config_yml["request"]["include_explicit_only"]
|
39
|
-
@request.include_explicit_only = include_explicit_only.blank? ? false : include_explicit_only
|
40
|
-
@request.body = @is_inline? get_inline_request_body : get_exploaded_request_body
|
41
|
-
end
|
42
|
-
|
43
|
-
def build_response
|
44
|
-
@response = Response.new
|
45
|
-
response.body = @is_inline? get_inline_response_body : get_exploaded_response_body
|
46
|
-
handle_resonse_array_type
|
47
|
-
end
|
48
|
-
|
49
|
-
def handle_resonse_array_type
|
50
|
-
return if @config_yml["response"].blank?
|
51
|
-
array_type = @config_yml["response"]["array_type"]
|
52
|
-
return if array_type.blank?
|
53
|
-
@response.is_column_array = true if array_type.eql? "column"
|
54
|
-
@response.is_row_array = true if array_type.eql? "row"
|
55
|
-
end
|
56
|
-
|
57
|
-
|
58
|
-
def get_inline_response_body
|
59
|
-
body = @config_yml["response"]["body"]
|
60
|
-
return (body.nil?) ? "": (Parse.json_to_hash body)
|
61
|
-
end
|
62
|
-
|
63
|
-
def get_inline_request_body
|
64
|
-
body = @config_yml["request"]["body"]
|
65
|
-
if body.nil?
|
66
|
-
return ""
|
67
|
-
elsif body.class.name == "String"
|
68
|
-
return Parse.json_to_hash body
|
69
|
-
else
|
70
|
-
@request.is_body_key_value = true
|
71
|
-
return body
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def get_exploaded_request_body
|
76
|
-
request_file = "#{File.dirname @config_yml_file}/request.json"
|
77
|
-
return (File.exists? request_file) ? Parse.json_file_to_hash(request_file) : ""
|
78
|
-
end
|
79
|
-
|
80
|
-
def get_exploaded_response_body
|
81
|
-
response_file = "#{File.dirname @config_yml_file}/response.json"
|
82
|
-
return (File.exists? response_file)? Parse.json_file_to_hash(response_file) : ""
|
83
|
-
end
|
84
|
-
|
85
|
-
end
|
86
|
-
|
87
|
-
|
88
|
-
class Request
|
89
|
-
attr_accessor :domain,:url,:type,:query,:body,:is_body_key_value,:include_explicit_only
|
90
|
-
end
|
91
|
-
|
92
|
-
class Response
|
93
|
-
attr_accessor :body,:is_column_array,:is_row_array
|
94
|
-
end
|
95
|
-
|
@@ -1,29 +0,0 @@
|
|
1
|
-
module ClassTemplate
|
2
|
-
attr_accessor :response_body
|
3
|
-
|
4
|
-
def initialize
|
5
|
-
@config = ConfigMap.file self.class.name
|
6
|
-
prepare_request
|
7
|
-
prepare_response
|
8
|
-
end
|
9
|
-
|
10
|
-
def prepare_request
|
11
|
-
prepare_request_url
|
12
|
-
prepare_request_query
|
13
|
-
prepare_request_body
|
14
|
-
end
|
15
|
-
|
16
|
-
def prepare_request_url
|
17
|
-
generate_methods_to_alter_path_params
|
18
|
-
@type = @config.request.type
|
19
|
-
end
|
20
|
-
|
21
|
-
def prepare_request_query
|
22
|
-
generate_query_methods if @config.request.query.present?
|
23
|
-
end
|
24
|
-
|
25
|
-
def prepare_request_body
|
26
|
-
generate_request_methods if @config.request.body.present?
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
module ClassTemplate
|
2
|
-
def build
|
3
|
-
build_url
|
4
|
-
build_query
|
5
|
-
build_request_body
|
6
|
-
build_response_body
|
7
|
-
end
|
8
|
-
|
9
|
-
def build_url
|
10
|
-
remove_path_param_name_from_url
|
11
|
-
@stub = stub_request(@type, "http://#{@config.request.domain}/#{@url}")
|
12
|
-
end
|
13
|
-
|
14
|
-
def build_query
|
15
|
-
@stub.with(:query => @query) unless @query.eql? ""
|
16
|
-
end
|
17
|
-
|
18
|
-
def build_request_body
|
19
|
-
return if @request_body.blank?
|
20
|
-
@request_body = Parse.hash_to_concatenated_key_value(@request_body) if @config.request.is_body_key_value
|
21
|
-
@stub.with(:body => @request_body)
|
22
|
-
end
|
23
|
-
|
24
|
-
def build_response_body
|
25
|
-
@response_body = Parse.hash_array_to_column_hash(@response_body) if @config.response.is_column_array
|
26
|
-
@stub.to_return(:status => 200, :body => @response_body.to_json, :headers => {'Content-Type' => 'application/json'})
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
@@ -1,14 +0,0 @@
|
|
1
|
-
module ClassTemplate
|
2
|
-
def generate_request_methods
|
3
|
-
@request_body = @config.request.body.deep_dup
|
4
|
-
return if @request_body.nil?
|
5
|
-
@request_body.each do |field|
|
6
|
-
field_name = field[0]
|
7
|
-
define_singleton_method("by_#{field[0].underscore}") do |*value|
|
8
|
-
@request_body[field_name] = value[0]
|
9
|
-
self
|
10
|
-
end
|
11
|
-
end
|
12
|
-
@request_body = {} if @config.request.include_explicit_only
|
13
|
-
end
|
14
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module ClassTemplate
|
2
|
-
def generate_methods_to_alter_path_params
|
3
|
-
@url = @config.request.url.clone
|
4
|
-
@url.strip!
|
5
|
-
@url.sub!("/","") if @url[0].eql? "/"
|
6
|
-
@url.scan(/{(.+?)}/).each do |path_params|
|
7
|
-
path_param = path_params[0]
|
8
|
-
param_name = path_param.split("|")[0]
|
9
|
-
param_default_value = path_param.split("|")[1]
|
10
|
-
method_name = "for_#{param_name.underscore}"
|
11
|
-
define_singleton_method(method_name) do |*input_value|
|
12
|
-
input_value = input_value[0]
|
13
|
-
@url = @url.sub("{#{path_param}}","{#{param_name}|#{input_value}}") unless input_value.blank?
|
14
|
-
self
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def remove_path_param_name_from_url
|
20
|
-
@url.scan(/{(.+?)}/).each do |path_params|
|
21
|
-
path_param = path_params[0]
|
22
|
-
param_name = path_param.split("|")[0]
|
23
|
-
param_value = path_param.split("|")[1]
|
24
|
-
@url.sub!("{#{path_param}}",param_value)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|