|
| 1 | +#!env -S ruby -W2 -w -v -E UTF-8 -T0 |
| 2 | + |
| 3 | +# Spectrum - a ruby HTTP client program paired with GeekSpec DSL |
| 4 | + |
| 5 | +# GeekApk - https://github.com/duangsuse/GeekApk |
| 6 | +# |
| 7 | +# Write your API specifications with GeekSpec markup and load them with Spectrum! |
| 8 | + |
| 9 | +# Http client wrapper |
| 10 | +require 'faraday' |
| 11 | + |
| 12 | +# Json library |
| 13 | +require 'json' |
| 14 | + |
| 15 | +# Program version |
| 16 | +VERSION = "0.1.0" |
| 17 | + |
| 18 | +# Node template |
| 19 | +PARSER_CODE = ENV['PARSER_CODE'] || './geekspec_parser.js' |
| 20 | +NODE_FD = '/proc/self/fd/0' |
| 21 | +NODE_INPUT = "fs.readFileSync('#{NODE_FD}')" |
| 22 | + |
| 23 | +PARSER = <<EoCMD |
| 24 | +node -e "console.log(JSON.stringify(require('#{PARSER_CODE}').parse(#{NODE_INPUT}.toString())))" |
| 25 | +EoCMD |
| 26 | + |
| 27 | +######################################################################### |
| 28 | +# Copyright 2019 duangsuse |
| 29 | +# |
| 30 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 31 | +# you may not use this file except in compliance with the License. |
| 32 | +# You may obtain a copy of the License at |
| 33 | +# |
| 34 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 35 | +# |
| 36 | +# Unless required by applicable law or agreed to in writing, software |
| 37 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 38 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 39 | +# See the License for the specific language governing permissions and |
| 40 | +# limitations under the License. |
| 41 | +######################################################################### |
| 42 | + |
| 43 | +def right_away(spec) |
| 44 | + puts spec |
| 45 | +end |
| 46 | + |
| 47 | +# synchronous, bit buggy, not robust |
| 48 | +def translate_extra_node(code) |
| 49 | +# %x[#{"cat<<CODE\n" + code + "\nCODE\n|" + PARSER}] |
| 50 | +# %x[#{"echo " + code + '|' + PARSER}] |
| 51 | + f = File.new("Code_#{rand(0..100)}", 'w+') |
| 52 | + |
| 53 | + begin |
| 54 | + f.write(code) |
| 55 | + f.flush |
| 56 | + |
| 57 | + result = %x(#{PARSER.gsub(NODE_FD, f.path)}) |
| 58 | + ensure |
| 59 | + File.delete(f) |
| 60 | + end |
| 61 | + |
| 62 | + return result |
| 63 | +end |
| 64 | + |
| 65 | +# CLI launcher |
| 66 | +def start(args = ARGV) |
| 67 | + puts "Spectrum v#{VERSION}: usage: #{$0} <spec json file>" |
| 68 | + |
| 69 | + return unless ARGV.size == 1 |
| 70 | + |
| 71 | + code = File.read(ARGV.first) |
| 72 | + |
| 73 | + unless ARGV.first.end_with?('.json') or ENV['JSON'] |
| 74 | + translated = translate_extra_node(code.gsub(/#.*$/, '')) |
| 75 | + require 'pry' |
| 76 | + translated.pry |
| 77 | + json = translated.yield_self { |c| JSON.parse(c) } |
| 78 | + |
| 79 | + return right_away(json) |
| 80 | + end |
| 81 | + |
| 82 | + right_away(JSON.parse(code)) |
| 83 | +end |
| 84 | + |
| 85 | +# invokes main function if this script is running not as a library |
| 86 | +start if $0 == __FILE__ |
0 commit comments