raml_parser 0.2.3 → 0.2.4

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: dd25d7244f2ee30c63c0af8ffdf113abf3a2f4bf
4
- data.tar.gz: 0050bfd6cbe239d7e9bd2aaf663895eb7c93a68b
3
+ metadata.gz: d074e36aa23c0cfd4d78e459a64d209d436d2f8c
4
+ data.tar.gz: 1de255edbdde7d2a9be2db0d878d26ea57f06d6f
5
5
  SHA512:
6
- metadata.gz: c9415cff318565dd2ee372e5e6194b754ecf473c5370b2f7dddc0ddaa3686e99020de86e316ce00d36c22e233fce809d1274b0ebfdc8916d7d34f963f1af444b
7
- data.tar.gz: 6a5d550de3bd9b9359a7fbd3a75457f5d1ded727d3430679df75c90f98b865b1bce80af7004eceec1e11944e1ee9fc4345873aa09a65701d98be3a089a2bf037
6
+ metadata.gz: cb1fab8fec64fbe91ddc5cec4c817f1a7287e18b664fadb2e695d3297e519eec410902875a8be4c2362e4dad4753923008b8f31982e1c1238979e8221ff16878
7
+ data.tar.gz: b540194a6fb42ab39b6e52639d09c460dfd5be4636bf0c2da639adec0c3006258710249bfe05369f44e8707b538384e81896d1249d762a5c535cea309403f310
@@ -22,7 +22,7 @@ module RamlParser
22
22
 
23
23
  query_parameters = map_nonempty(render_query_parameters(method.query_parameters.values)) { |s| '?' + s }
24
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") }
25
+ data = map_nonempty(method.bodies.values.first) { |b| (b.example || '').chop }
26
26
 
27
27
  result = "var xhr = new XMLHttpRequest();\n"
28
28
  result += "xhr.open('#{method.method.upcase}', '#{resource.absolute_uri + query_parameters}', true);\n"
@@ -36,6 +36,29 @@ module RamlParser
36
36
  result
37
37
  end
38
38
 
39
+ def ruby(resource, method_name)
40
+ method = resource.methods[method_name]
41
+ send_method = method.method.downcase == 'post' || method.method.downcase == 'put'
42
+ query_parameters = map_nonempty(render_query_parameters(method.query_parameters.values)) { |s| '?' + s } || ''
43
+ uri = "#{resource.absolute_uri}#{query_parameters}"
44
+
45
+ data = map_nonempty(method.bodies.values.first) { |b| b.example.chop }
46
+ headers = { 'Accept' => map_nonempty(method.bodies.values.first) { |b| b.media_type } }
47
+ headers = headers.delete_if {|key, value| value.nil? }
48
+
49
+ result = "require 'net/http'\n"
50
+ result += "require 'json'\n" if send_method
51
+ result += "uri = URI.parse('#{uri}')\n"
52
+ result += "headers = #{headers}\n" unless headers.empty?
53
+ result += "data = #{data}\n\n" if send_method
54
+ result += "req = Net::HTTP::#{method.method.capitalize}.new(uri.path"
55
+ result += headers.empty? ? ")\n" : ", initheader = headers)\n"
56
+ result += "req.body = data.to_json\n" if send_method
57
+ result += "response = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }\n"
58
+
59
+ result
60
+ end
61
+
39
62
  private
40
63
  def is_falsy(value)
41
64
  if value.is_a?(String)
@@ -1,3 +1,3 @@
1
1
  module RamlParser
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
@@ -40,4 +40,26 @@ RSpec.describe RamlParser::SnippetGenerator do
40
40
  expect(res3).to include 'GET'
41
41
  expect(res3).to include 'http://localhost:3000/i?l=value'
42
42
  end
43
+
44
+ it 'generate ruby snippets' do
45
+ raml1 = RamlParser::Parser.parse_file('spec/examples/raml/resources.raml')
46
+ gen1 = RamlParser::SnippetGenerator.new(raml1)
47
+ res1 = gen1.ruby(raml1.resources[0], 'get')
48
+ expect(res1).to include 'Net::HTTP::Get'
49
+ expect(res1).to include 'http://localhost:3000/first'
50
+
51
+ raml2 = RamlParser::Parser.parse_file('spec/examples/raml/requestbodies.raml')
52
+ gen2 = RamlParser::SnippetGenerator.new(raml2)
53
+ res2 = gen2.ruby(raml2.resources[0], 'post')
54
+ expect(res2).to include 'Net::HTTP::Post'
55
+ expect(res2).to include 'Accept'
56
+ expect(res2).to include 'application/json'
57
+ expect(res2).to include '"foo": "bar"'
58
+
59
+ raml3 = RamlParser::Parser.parse_file('spec/examples/raml/required.raml')
60
+ gen3 = RamlParser::SnippetGenerator.new(raml3)
61
+ res3 = gen3.ruby(raml3.resources[4], 'get')
62
+ expect(res3).to include 'Net::HTTP::Get'
63
+ expect(res3).to include 'http://localhost:3000/i?l=value'
64
+ end
43
65
  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.3
4
+ version: 0.2.4
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-05-27 00:00:00.000000000 Z
11
+ date: 2015-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler