fictium 0.2.0 → 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.
@@ -0,0 +1,40 @@
1
+ module Fictium
2
+ module Postman
3
+ class V2Exporter
4
+ class ActionFormatter
5
+ def format(action)
6
+ metadata_formatter.format(action, build_base(action))
7
+ end
8
+
9
+ private
10
+
11
+ def build_base(action)
12
+ {
13
+ id: "#{action.method} - #{action.full_path}",
14
+ name: action.summary,
15
+ description: action.description,
16
+ request: request_formatter.format(action.default_example),
17
+ response: format_responses(action)
18
+ }
19
+ end
20
+
21
+ def format_responses(action)
22
+ result = action.examples.map { |example| response_formatter.format(example) }
23
+ result.reject(&:blank?)
24
+ end
25
+
26
+ def request_formatter
27
+ @request_formatter ||= RequestFormatter.new
28
+ end
29
+
30
+ def response_formatter
31
+ @response_formatter ||= ResponseFormatter.new
32
+ end
33
+
34
+ def metadata_formatter
35
+ @metadata_formatter ||= MetadataFormatter.new
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,15 @@
1
+ module Fictium
2
+ module Postman
3
+ class V2Exporter
4
+ class BodyFormatter
5
+ def format(http_subject, response: false)
6
+ body = http_subject[:body]
7
+ return if body.blank?
8
+ return body if response
9
+
10
+ { mode: 'raw', raw: body }
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ module Fictium
2
+ module Postman
3
+ class V2Exporter
4
+ class FolderFormatter
5
+ def format(item_formatter, resource)
6
+ { name: resource.name }.tap do |result|
7
+ result[:description] = format_description(resource)
8
+ result[:item] = item_formatter.from_resource(resource)
9
+ metadata_formatter.format(resource, result)
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def format_description(resource)
16
+ return resource.description if resource.description.present?
17
+
18
+ resource.summary
19
+ end
20
+
21
+ def metadata_formatter
22
+ @metadata_formatter ||= MetadataFormatter.new
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ module Fictium
2
+ module Postman
3
+ class V2Exporter
4
+ class HeaderFormatter
5
+ def format(http_subject)
6
+ [].tap do |header|
7
+ content = http_subject[:content_type]
8
+ header << { key: 'Content-Type', value: content } if content.present?
9
+ # TODO: Add the rest of the headers
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ module Fictium
2
+ module Postman
3
+ class V2Exporter
4
+ class InfoFormatter
5
+ POSTMAN_SCHEMA_URL = 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'.freeze
6
+
7
+ delegate :configuration, to: :Fictium
8
+ delegate :info, :postman, to: :configuration
9
+
10
+ def format(_document)
11
+ base_info.tap do |result|
12
+ result[:_postman_id] = postman.id if postman.id.present?
13
+ result[:description] = info.description if info.description.present?
14
+ result[:schema] = POSTMAN_SCHEMA_URL
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def base_info
21
+ { name: info.title, version: info.version }
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,29 @@
1
+ module Fictium
2
+ module Postman
3
+ class V2Exporter
4
+ class ItemFormatter
5
+ def format(document)
6
+ document.resources.map do |resource|
7
+ folder_formatter.format(self, resource)
8
+ end
9
+ end
10
+
11
+ def from_resource(resource)
12
+ resource.actions.map do |action|
13
+ action_formatter.format(action)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def folder_formatter
20
+ @folder_formatter ||= FolderFormatter.new
21
+ end
22
+
23
+ def action_formatter
24
+ @action_formatter ||= ActionFormatter.new
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ module Fictium
2
+ module Postman
3
+ class V2Exporter
4
+ class MetadataFormatter
5
+ FIELDS = %i[event variable auth].freeze
6
+
7
+ def format(subject, result)
8
+ FIELDS.each do |field|
9
+ value = subject.postman.public_send(field)
10
+ result[field] = value if value.present?
11
+ end
12
+ result
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,89 @@
1
+ module Fictium
2
+ module Postman
3
+ class V2Exporter
4
+ class RequestFormatter
5
+ PATH_VARIABLE = /{(?<var>[A-Z_\-][A-Z0-9_\-]*)}/i.freeze
6
+
7
+ def format(example)
8
+ {}.tap do |result|
9
+ result.merge!(
10
+ url: format_url(example),
11
+ method: example.action.method.to_s.downcase,
12
+ description: example.action.description,
13
+ header: header_formatter.format(example.request)
14
+ )
15
+ add_optional_values(example, result)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def format_url(example)
22
+ {
23
+ raw: full_path(example),
24
+ host: [api_url],
25
+ path: format_path(example),
26
+ query: format_query(example),
27
+ variable: format_variable(example)
28
+ }
29
+ end
30
+
31
+ def full_path(example)
32
+ "#{api_url}#{convert_path(example.action)}#{query_params_for(example)}"
33
+ end
34
+
35
+ def convert_path(action)
36
+ action.full_path.gsub(PATH_VARIABLE, ':\k<var>')
37
+ end
38
+
39
+ def format_path(example)
40
+ path = convert_path(example.action).split('/')
41
+ path.shift
42
+ path
43
+ end
44
+
45
+ def query_params_for(example)
46
+ params = example.request[:query_parameters]
47
+ params.present? ? "?#{params.to_query}" : ''
48
+ end
49
+
50
+ def api_url
51
+ Fictium.configuration.postman.api_url
52
+ end
53
+
54
+ def format_query(example)
55
+ example.request[:query_parameters].map do |key, value|
56
+ result = { key: key, value: value }
57
+ description = example.action[:query][key] && example.action[:query][key][:description]
58
+ result[:description] = description if description.present?
59
+ result
60
+ end
61
+ end
62
+
63
+ def format_variable(example)
64
+ [].tap do |result|
65
+ example.action[:path].each do |name, _|
66
+ data = { id: name, key: name }
67
+ value = example.request[:path_parameters][name.to_sym]
68
+ data[:value] = value if value.present?
69
+ result << data
70
+ end
71
+ end
72
+ end
73
+
74
+ def add_optional_values(example, result)
75
+ body = body_formatter.format(example.request)
76
+ result[:body] = body if body.present?
77
+ end
78
+
79
+ def body_formatter
80
+ @body_formatter ||= BodyFormatter.new
81
+ end
82
+
83
+ def header_formatter
84
+ @header_formatter ||= HeaderFormatter.new
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,50 @@
1
+ module Fictium
2
+ module Postman
3
+ class V2Exporter
4
+ class ResponseFormatter
5
+ def format(example)
6
+ base_info_for(example).tap do |result|
7
+ body = body_formatter.format(example.response, response: true)
8
+ header = header_formatter.format(example.response)
9
+ result[:body] = body if body.present?
10
+ result[:header] = header if header.present?
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def base_info_for(example)
17
+ status = example.response[:status]
18
+ formatted_status = format_status(status, example)
19
+ {
20
+ originalRequest: request_formatter.format(example),
21
+ responseTime: nil,
22
+ status: format_status(status, example),
23
+ name: formatted_status,
24
+ code: status
25
+ }
26
+ end
27
+
28
+ def request_formatter
29
+ @request_formatter ||= RequestFormatter.new
30
+ end
31
+
32
+ def body_formatter
33
+ @body_formatter ||= BodyFormatter.new
34
+ end
35
+
36
+ def header_formatter
37
+ @header_formatter ||= HeaderFormatter.new
38
+ end
39
+
40
+ def format_status(status, example)
41
+ postman.example_formatter.call(status, example)
42
+ end
43
+
44
+ def postman
45
+ Fictium.configuration.postman
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,61 @@
1
+ require_relative 'v2_exporter/action_formatter'
2
+
3
+ require_relative 'v2_exporter/info_formatter'
4
+ require_relative 'v2_exporter/item_formatter'
5
+ require_relative 'v2_exporter/metadata_formatter'
6
+
7
+ require_relative 'v2_exporter/folder_formatter'
8
+
9
+ require_relative 'v2_exporter/request_formatter'
10
+ require_relative 'v2_exporter/response_formatter'
11
+ require_relative 'v2_exporter/header_formatter'
12
+ require_relative 'v2_exporter/body_formatter'
13
+
14
+ module Fictium
15
+ module Postman
16
+ class V2Exporter
17
+ def export(document)
18
+ result = format_document(document)
19
+ validate!(result)
20
+ FileUtils.mkdir_p(File.dirname(export_file))
21
+ File.write(export_file, pretty_print? ? JSON.pretty_generate(result) : result.to_json)
22
+ end
23
+
24
+ private
25
+
26
+ def export_file
27
+ @export_file ||=
28
+ File.join(Fictium.configuration.export_path, 'postman', '2.1.0', 'collection.json')
29
+ end
30
+
31
+ def validate!(result)
32
+ JSON::Validator.validate!(schema, result)
33
+ end
34
+
35
+ def schema
36
+ @schema ||= JSON.parse(File.read(File.join(__dir__, 'schemas', '2.1.0.json')))
37
+ end
38
+
39
+ def pretty_print?
40
+ Fictium.configuration.pretty_print
41
+ end
42
+
43
+ def format_document(document)
44
+ data = { info: info_formatter.format(document), item: item_formatter.format(document) }
45
+ metadata_formatter.format(document, data)
46
+ end
47
+
48
+ def metadata_formatter
49
+ @metadata_formatter ||= MetadataFormatter.new
50
+ end
51
+
52
+ def info_formatter
53
+ @info_formatter ||= InfoFormatter.new
54
+ end
55
+
56
+ def item_formatter
57
+ @item_formatter ||= ItemFormatter.new
58
+ end
59
+ end
60
+ end
61
+ end
@@ -1,9 +1,11 @@
1
1
  require_relative 'configurations/configuration'
2
2
  require_relative 'configurations/info'
3
3
  require_relative 'configurations/api_blueprint'
4
+ require_relative 'configurations/postman'
4
5
 
5
6
  require_relative 'evaluators/parameter_evaluator'
6
7
  require_relative 'evaluators/schema_evaluator'
8
+ require_relative 'evaluators/postman_evaluator'
7
9
 
8
10
  require_relative 'poros/model'
9
11
 
@@ -12,8 +14,11 @@ require_relative 'poros/document'
12
14
  require_relative 'poros/example'
13
15
  require_relative 'poros/resource'
14
16
 
17
+ require_relative 'poros/postman_metadata'
18
+
15
19
  # Require default (OpenApi v3) exporter
16
20
  require_relative 'exporters/open_api/v3_exporter'
17
21
 
18
22
  # Other exporters created by this gem
19
23
  require_relative 'exporters/api_blueprint_exporter'
24
+ require_relative 'exporters/postman/v2_exporter'
@@ -1,4 +1,7 @@
1
1
  module Fictium
2
2
  class Model
3
+ def postman
4
+ @postman ||= PostmanMetadata.new
5
+ end
3
6
  end
4
7
  end
@@ -0,0 +1,5 @@
1
+ module Fictium
2
+ class PostmanMetadata
3
+ attr_accessor :event, :auth, :variable
4
+ end
5
+ end
@@ -8,7 +8,7 @@ module Fictium
8
8
  example.response.merge!(
9
9
  status: response.status,
10
10
  body: response.body,
11
- content_type: response.content_type,
11
+ content_type: response.media_type,
12
12
  header: filter_header(response.header.to_h)
13
13
  )
14
14
  process_http_request(example, response.request)
@@ -25,17 +25,23 @@ module Fictium
25
25
 
26
26
  def process_http_request(example, request)
27
27
  example.request ||= {}
28
- example.request.merge!(
29
- content_type: request.content_type,
30
- body: request.body.string,
31
- header: filter_header(request.headers.to_h)
32
- )
28
+ example.request.merge!(process_base_request(request))
33
29
  extract_method(example, request)
34
30
  return unless example.default?
35
31
 
36
32
  autocomplete_params.extract_from_request(example.action, request)
37
33
  end
38
34
 
35
+ def process_base_request(request)
36
+ {
37
+ content_type: request.content_type,
38
+ body: request.body.string,
39
+ header: filter_header(request.headers.to_h),
40
+ path_parameters: request.path_parameters.except(:controller, :action),
41
+ query_parameters: request.query_parameters
42
+ }
43
+ end
44
+
39
45
  def extract_method(example, request)
40
46
  action = example.action
41
47
  action.method = request.method.downcase.to_sym if action.method.blank?
@@ -35,6 +35,11 @@ module Fictium
35
35
  def resource_tags(*tags)
36
36
  metadata[:fictium_resource].tags += tags
37
37
  end
38
+
39
+ def postman_for(object, &block)
40
+ resource = metadata[:"fictium_#{object}"]
41
+ Fictium::PostmanEvaluator.new(resource).evaluate(&block)
42
+ end
38
43
  end
39
44
  end
40
45
  end
@@ -1,5 +1,5 @@
1
1
  module Fictium
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  RAILS_MIN_VERSION = '>= 5.3'.freeze
4
4
  RAILS_MAX_VERSION = '< 6.2'.freeze
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fictium
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ramiro Rojo
@@ -261,8 +261,10 @@ files:
261
261
  - lib/fictium/configurations/api_blueprint.rb
262
262
  - lib/fictium/configurations/configuration.rb
263
263
  - lib/fictium/configurations/info.rb
264
+ - lib/fictium/configurations/postman.rb
264
265
  - lib/fictium/engine.rb
265
266
  - lib/fictium/evaluators/parameter_evaluator.rb
267
+ - lib/fictium/evaluators/postman_evaluator.rb
266
268
  - lib/fictium/evaluators/schema_evaluator.rb
267
269
  - lib/fictium/exporters/api_blueprint_exporter.rb
268
270
  - lib/fictium/exporters/api_blueprint_exporter/action_formatter.rb
@@ -278,11 +280,23 @@ files:
278
280
  - lib/fictium/exporters/open_api/v3_exporter/param_formatter.rb
279
281
  - lib/fictium/exporters/open_api/v3_exporter/path_formatter.rb
280
282
  - lib/fictium/exporters/open_api/v3_exporter/path_generator.rb
283
+ - lib/fictium/exporters/postman/schemas/2.1.0.json
284
+ - lib/fictium/exporters/postman/v2_exporter.rb
285
+ - lib/fictium/exporters/postman/v2_exporter/action_formatter.rb
286
+ - lib/fictium/exporters/postman/v2_exporter/body_formatter.rb
287
+ - lib/fictium/exporters/postman/v2_exporter/folder_formatter.rb
288
+ - lib/fictium/exporters/postman/v2_exporter/header_formatter.rb
289
+ - lib/fictium/exporters/postman/v2_exporter/info_formatter.rb
290
+ - lib/fictium/exporters/postman/v2_exporter/item_formatter.rb
291
+ - lib/fictium/exporters/postman/v2_exporter/metadata_formatter.rb
292
+ - lib/fictium/exporters/postman/v2_exporter/request_formatter.rb
293
+ - lib/fictium/exporters/postman/v2_exporter/response_formatter.rb
281
294
  - lib/fictium/loader.rb
282
295
  - lib/fictium/poros/action.rb
283
296
  - lib/fictium/poros/document.rb
284
297
  - lib/fictium/poros/example.rb
285
298
  - lib/fictium/poros/model.rb
299
+ - lib/fictium/poros/postman_metadata.rb
286
300
  - lib/fictium/poros/resource.rb
287
301
  - lib/fictium/railtie.rb
288
302
  - lib/fictium/rspec.rb