raml_parser 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c566421a59a1d92ee889714c3c84f37e6c1673d0
4
- data.tar.gz: 0a572f77481d481ccba55d87eb09f34962e34abd
3
+ metadata.gz: dd25d7244f2ee30c63c0af8ffdf113abf3a2f4bf
4
+ data.tar.gz: 0050bfd6cbe239d7e9bd2aaf663895eb7c93a68b
5
5
  SHA512:
6
- metadata.gz: b3520c84d38f67cfca3073be3c03ece838c40a54bb07b592e62b2e16a0a722b1fbb008195689c3b4202d22b839318bce7429f144b949d9edb8e18c89ad723e50
7
- data.tar.gz: 52a5b63afb2a9c76702d13326a063e76af27481241d833e2085bb365c0892dd7faa586c4d47347e26266432407f962e1866e803bece269f11598ce3e0497080f
6
+ metadata.gz: c9415cff318565dd2ee372e5e6194b754ecf473c5370b2f7dddc0ddaa3686e99020de86e316ce00d36c22e233fce809d1274b0ebfdc8916d7d34f963f1af444b
7
+ data.tar.gz: 6a5d550de3bd9b9359a7fbd3a75457f5d1ded727d3430679df75c90f98b865b1bce80af7004eceec1e11944e1ee9fc4345873aa09a65701d98be3a089a2bf037
@@ -0,0 +1,63 @@
1
+ module RamlParser
2
+ class SnippetGenerator
3
+ attr_accessor :raml
4
+
5
+ def initialize(raml)
6
+ @raml = raml
7
+ end
8
+
9
+ def curl(resource, method_name)
10
+ method = resource.methods[method_name]
11
+
12
+ curl_method = "-X#{method.method.upcase}"
13
+ curl_content_type = map_nonempty(method.bodies.values.first) { |b| "-H \"Accept: #{b.media_type}\"" }
14
+ query_parameters = map_nonempty(render_query_parameters(method.query_parameters.values)) { |s| '?' + s }
15
+ curl_data = map_nonempty(method.bodies.values.first) { |b| "-d \"#{b.example}\"" }
16
+
17
+ ['curl', curl_method, curl_content_type, resource.absolute_uri + query_parameters, curl_data].select { |p| not is_falsy(p) }.join(' ')
18
+ end
19
+
20
+ def javascript_vanilla(resource, method_name)
21
+ method = resource.methods[method_name]
22
+
23
+ query_parameters = map_nonempty(render_query_parameters(method.query_parameters.values)) { |s| '?' + s }
24
+ js_content_type = map_nonempty(method.bodies.values.first) { |b| "xhr.setRequestHeader('Accept', '#{b.media_type}');\n" } || ''
25
+ data = map_nonempty(method.bodies.values.first) { |b| b.example.strip.gsub("\r\n", "\\r\\n").gsub("\n", "\\n") }
26
+
27
+ result = "var xhr = new XMLHttpRequest();\n"
28
+ result += "xhr.open('#{method.method.upcase}', '#{resource.absolute_uri + query_parameters}', true);\n"
29
+ result += "xhr.onreadystatechange = function () {\n"
30
+ result += " if (xhr.readyState != 4 || xhr.status != 200) return;\n"
31
+ result += " console.log('Success', xhr.responseText);\n"
32
+ result += "};\n"
33
+ result += js_content_type
34
+ result += "xhr.send(\"#{data}\");"
35
+
36
+ result
37
+ end
38
+
39
+ private
40
+ def is_falsy(value)
41
+ if value.is_a?(String)
42
+ value.length == 0
43
+ else
44
+ value.nil?
45
+ end
46
+ end
47
+
48
+ def map_nonempty(value, &code)
49
+ if not is_falsy(value)
50
+ code.call(value)
51
+ else
52
+ value
53
+ end
54
+ end
55
+
56
+ def render_query_parameters(query_parameters)
57
+ query_parameters
58
+ .select { |q| q.required }
59
+ .map { |q| "#{q.name}=#{q.example || 'value'}"}
60
+ .join('&')
61
+ end
62
+ end
63
+ end
@@ -1,3 +1,3 @@
1
1
  module RamlParser
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -0,0 +1,43 @@
1
+ require 'raml_parser/snippet_generator'
2
+
3
+ RSpec.describe RamlParser::SnippetGenerator do
4
+ it 'generate curl snippets' do
5
+ raml1 = RamlParser::Parser.parse_file('spec/examples/raml/resources.raml')
6
+ gen1 = RamlParser::SnippetGenerator.new(raml1)
7
+ res1 = gen1.curl(raml1.resources[0], 'get')
8
+ expect(res1).to eq 'curl -XGET http://localhost:3000/first'
9
+
10
+ raml2 = RamlParser::Parser.parse_file('spec/examples/raml/requestbodies.raml')
11
+ gen2 = RamlParser::SnippetGenerator.new(raml2)
12
+ res2 = gen2.curl(raml2.resources[0], 'post')
13
+ expect(res2).to include '-XPOST'
14
+ expect(res2).to include 'Accept: application/json'
15
+ expect(res2).to include '"foo": "bar"'
16
+
17
+ raml3 = RamlParser::Parser.parse_file('spec/examples/raml/required.raml')
18
+ gen3 = RamlParser::SnippetGenerator.new(raml3)
19
+ res3 = gen3.curl(raml3.resources[4], 'get')
20
+ expect(res3).to eq 'curl -XGET http://localhost:3000/i?l=value'
21
+ end
22
+
23
+ it 'generate javascript vanilla snippets' do
24
+ raml1 = RamlParser::Parser.parse_file('spec/examples/raml/resources.raml')
25
+ gen1 = RamlParser::SnippetGenerator.new(raml1)
26
+ res1 = gen1.javascript_vanilla(raml1.resources[0], 'get')
27
+ expect(res1).to include 'GET'
28
+ expect(res1).to include 'http://localhost:3000/first'
29
+
30
+ raml2 = RamlParser::Parser.parse_file('spec/examples/raml/requestbodies.raml')
31
+ gen2 = RamlParser::SnippetGenerator.new(raml2)
32
+ res2 = gen2.javascript_vanilla(raml2.resources[0], 'post')
33
+ expect(res2).to include 'Accept'
34
+ expect(res2).to include 'application/json'
35
+ expect(res2).to include '"foo": "bar"'
36
+
37
+ raml3 = RamlParser::Parser.parse_file('spec/examples/raml/required.raml')
38
+ gen3 = RamlParser::SnippetGenerator.new(raml3)
39
+ res3 = gen3.javascript_vanilla(raml3.resources[4], 'get')
40
+ expect(res3).to include 'GET'
41
+ expect(res3).to include 'http://localhost:3000/i?l=value'
42
+ end
43
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: raml_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Hoffmeister
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-05 00:00:00.000000000 Z
11
+ date: 2015-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -82,6 +82,7 @@ files:
82
82
  - Rakefile
83
83
  - lib/raml_parser.rb
84
84
  - lib/raml_parser/model.rb
85
+ - lib/raml_parser/snippet_generator.rb
85
86
  - lib/raml_parser/version.rb
86
87
  - lib/raml_parser/yaml_helper.rb
87
88
  - raml_parser.gemspec
@@ -148,6 +149,7 @@ files:
148
149
  - spec/examples/yaml/include2.yml
149
150
  - spec/examples/yaml/simple.yml
150
151
  - spec/examples/yaml/traversing.yml
152
+ - spec/lib/raml_parser/snippet_generator_spec.rb
151
153
  - spec/lib/raml_parser/yaml_helper_spec.rb
152
154
  - spec/lib/raml_parser_spec.rb
153
155
  - spec/spec_helper.rb
@@ -171,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
173
  version: '0'
172
174
  requirements: []
173
175
  rubyforge_project:
174
- rubygems_version: 2.0.14
176
+ rubygems_version: 2.4.6
175
177
  signing_key:
176
178
  specification_version: 4
177
179
  summary: A parser for the RAML API modeling language.
@@ -239,6 +241,7 @@ test_files:
239
241
  - spec/examples/yaml/include2.yml
240
242
  - spec/examples/yaml/simple.yml
241
243
  - spec/examples/yaml/traversing.yml
244
+ - spec/lib/raml_parser/snippet_generator_spec.rb
242
245
  - spec/lib/raml_parser/yaml_helper_spec.rb
243
246
  - spec/lib/raml_parser_spec.rb
244
247
  - spec/spec_helper.rb