apia-insomnia 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7a1544a5b227ca74b2e3d239178e62f7aae387eb663fae30d2aafdf4058b5919
4
+ data.tar.gz: ca641e4c63a055b3e6c57fa3e17cd2381ce17601fc077d9188ba63366d2df1c6
5
+ SHA512:
6
+ metadata.gz: 817d441b2dbddea0627a5127fe7feda72ec5b10e50278dabd819c8dde215c48039aa61096c7301f3113e7511841c548766a9b9f045d682644b9e0decc483d0b1
7
+ data.tar.gz: 2fb2c1c496f453435d85c514cf1256a88e881caaf9d513c2b326391026b90c638e48cc50327431847884c76181cbb6dc614d65a19cb674280c98be17d90088e4
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 2.0.0
@@ -0,0 +1,7 @@
1
+ # rubocop:disable Naming/FileName
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require 'apia/insomnia/rack'
6
+
7
+ # rubocop:enable Naming/FileName
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'apia/insomnia/schema'
4
+
5
+ module Apia
6
+ module Insomnia
7
+ class Rack
8
+
9
+ def initialize(app, api, path, **options)
10
+ @app = app
11
+ @api = api
12
+ @path = "/#{path.sub(/\A\/+/, '').sub(/\/+\z/, '')}"
13
+ @options = options
14
+ end
15
+
16
+ def development?
17
+ env_is_dev = ENV['RACK_ENV'] == 'development'
18
+ return true if env_is_dev && @options[:development].nil?
19
+
20
+ @options[:development] == true
21
+ end
22
+
23
+ def api
24
+ return Object.const_get(@api) if @api.is_a?(String) && development?
25
+ return @cached_api ||= Object.const_get(@api) if @api.is_a?(String)
26
+
27
+ @api
28
+ end
29
+
30
+ def base_url
31
+ @options[:base_url] || 'https://api.example.com/api/v1'
32
+ end
33
+
34
+ def call(env)
35
+ if @options[:hosts]&.none? { |host| host == env['HTTP_HOST'] }
36
+ return @app.call(env)
37
+ end
38
+
39
+ unless env['PATH_INFO'] == @path
40
+ return @app.call(env)
41
+ end
42
+
43
+ schema = Schema.new(api, base_url)
44
+ body = schema.json
45
+
46
+ [200, { 'Content-Type' => 'application/json', 'Content-Length' => body.bytesize.to_s }, [body]]
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,160 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Apia
4
+ module Insomnia
5
+ class Schema
6
+
7
+ def initialize(api, base_url)
8
+ @api = api
9
+ @base_url = base_url
10
+ @spec = {
11
+ '_type' => 'export',
12
+ '__export_format' => 3,
13
+ '__export__date' => Time.now.to_s,
14
+ 'resources' => []
15
+ }
16
+ setup
17
+ end
18
+
19
+ def json
20
+ @spec.to_json
21
+ end
22
+
23
+ private
24
+
25
+ def setup
26
+ add_resource('workspace', '__WORKSPACE_ID__',
27
+ name: @api.definition.name,
28
+ description: @api.definition.description)
29
+
30
+ add_resource('environment', get_id('environment', @api),
31
+ name: 'Default Environment',
32
+ data: {
33
+ base_url: @base_url,
34
+ bearer_token: 'Add token here...'
35
+ })
36
+
37
+ add_groups(@api.definition.route_set.groups)
38
+ add_requests(@api.definition.route_set.routes)
39
+ end
40
+
41
+ def add_groups(groups)
42
+ groups.sort_by(&:name).each_with_index do |group, index|
43
+ add_resource('request_group', get_id('folder', group), {
44
+ name: group.name,
45
+ parentId: get_id('folder', group.parent),
46
+ metaSortKey: index,
47
+ enivronment: {}
48
+ })
49
+ add_groups(group.groups) if group.groups
50
+ end
51
+ nil
52
+ end
53
+
54
+ def add_requests(routes)
55
+ routes.each do |route|
56
+ next unless route.endpoint.definition.schema?
57
+
58
+ hash = {
59
+ name: route.endpoint.definition.name,
60
+ description: route.endpoint.definition.description,
61
+ url: "{{ base_url }}/#{route.path}",
62
+ parentId: get_id('folder', route.group),
63
+ method: route.request_method.to_s.upcase
64
+ }
65
+
66
+ authenticator = route.endpoint.definition.authenticator ||
67
+ route.controller&.definition&.authenticator ||
68
+ @api.definition.authenticator
69
+
70
+ case authenticator&.definition&.type
71
+ when :bearer
72
+ hash[:authentication] = { type: 'bearer', token: '{{ bearer_token }}' }
73
+ end
74
+
75
+ if route.request_method == :get
76
+ hash[:parameters] = []
77
+ add_parameters_to_array(hash[:parameters], route.endpoint.definition.argument_set)
78
+
79
+ else
80
+ hash[:body] = { mimeType: 'application/json' }
81
+ hash[:headers] = [{ name: 'Content-Type', value: 'application/json' }]
82
+
83
+ body_arguments = {}
84
+ add_arguments_to_hash(body_arguments, route.endpoint.definition.argument_set)
85
+ hash[:body][:text] = JSON.pretty_generate(body_arguments)
86
+ end
87
+
88
+ add_resource('request', get_id('request', route), hash)
89
+ end
90
+ end
91
+
92
+ def add_parameters_to_array(array, set, prefix = '')
93
+ set.definition.arguments.each_value do |arg|
94
+ if arg.type.argument_set?
95
+ sub_array = []
96
+ add_parameters_to_array(sub_array, arg.type.klass, arg.name.to_s)
97
+ sub_array.each { |a| array << a }
98
+ else
99
+
100
+ if prefix.empty?
101
+ name_for_param = arg.name.to_s
102
+ else
103
+ name_for_param = prefix + "[#{arg.name}]"
104
+ end
105
+
106
+ param = {
107
+ name: name_for_param,
108
+ value: nil,
109
+ disabled: !arg.required?
110
+ }
111
+ array << param
112
+ end
113
+ end
114
+ end
115
+
116
+ def add_arguments_to_hash(hash, set)
117
+ set.definition.arguments.each_value do |arg|
118
+ name_for_hash = arg.name.to_s
119
+ hash[name_for_hash] = get_default_value_for_argument(arg)
120
+ end
121
+ end
122
+
123
+ def get_default_value_for_argument(argument)
124
+ if argument.type.argument_set? && argument.type.klass.definition.is_a?(Apia::Definitions::LookupArgumentSet)
125
+ {
126
+ argument.type.klass.definition.arguments.values.map(&:name).join('|') => ''
127
+ }
128
+ elsif argument.type.argument_set?
129
+ hash = {}
130
+ add_arguments_to_hash(hash, argument.type.klass)
131
+ hash
132
+ elsif argument.type.enum?
133
+ argument.type.klass.definition.values.keys.join(' | ')
134
+ end
135
+ end
136
+
137
+ def add_resource(type, id, options)
138
+ @spec['resources'] << {
139
+ '_type' => type,
140
+ '_id' => id
141
+ }.merge(options)
142
+ end
143
+
144
+ def get_id(prefix, object)
145
+ return nil if object.nil?
146
+
147
+ prefix = prefix.upcase
148
+
149
+ @ids ||= {}
150
+ @ids[prefix] ||= []
151
+ unless @ids[prefix].include?(object)
152
+ @ids[prefix] << object
153
+ end
154
+ id = @ids[prefix].index(object) + 1
155
+ "__#{prefix}_#{id}__"
156
+ end
157
+
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Apia
4
+ module Insomnia
5
+
6
+ VERSION_FILE_ROOT = File.expand_path('../../../VERSION', __dir__)
7
+ if File.file?(VERSION_FILE_ROOT)
8
+ VERSION = File.read(VERSION_FILE_ROOT).strip.sub(/\Av/, '')
9
+ else
10
+ VERSION = '0.0.0.dev'
11
+ end
12
+
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apia-insomnia
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Cooke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A Raid schema generator for Insomnia.
42
+ email:
43
+ - adam@k.io
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - VERSION
49
+ - lib/apia-insomnia.rb
50
+ - lib/apia/insomnia/rack.rb
51
+ - lib/apia/insomnia/schema.rb
52
+ - lib/apia/insomnia/version.rb
53
+ homepage: https://github.com/krystal/apia-insomnia
54
+ licenses: []
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '2.6'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.1.6
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: This gem provides a tool for generating an Insomnia compatible schema for
75
+ a Apia API.
76
+ test_files: []