Sha256: fb4737f7521103a87aa8d184c5be668effc5269648731c3e319e54b31152d035

Contents?: true

Size: 1.74 KB

Versions: 7

Compression:

Stored size: 1.74 KB

Contents

#!/usr/bin/env ruby

require 'slop'
require 'fakeit'
require 'rack/cors'

usage = nil
begin
  opts = Slop.parse do |o|
    o.string '--spec', 'spec file uri (required)', required: true
    o.integer '-p', '--port', 'custom port'
    o.bool '-q', '--quiet', 'mute request and response log'
    o.string '-l', '--log-file', 'redirect log to a file'
    o.bool '--permissive', 'log validation error as warning instead of denying request'
    o.bool '--use-example', 'use example provided in spec if exists'
    o.bool '--static', 'generate static response'
    o.array '--static-types', 'generate static value for specified types, e.g. --static-types integer,string'
    o.array '--static-properties', 'generate static value for specified properties, e.g. --static-properties id,uuid'
    o.separator ''
    o.separator 'other options:'
    o.on '-v', '--version' do
      puts Fakeit::VERSION
      exit
    end
    o.on '-h', '--help' do
      puts o
      exit
    end

    usage = o
  end
rescue Slop::Error => e
  puts e.message
  puts usage
  exit
end

server = Rack::Handler::WEBrick

trap(:INT) do
  if server.respond_to?(:shutdown)
    server.shutdown
  else
    exit
  end
end

options = Fakeit::App::Options.new(
  permissive: opts.permissive?,
  use_example: opts.use_example?,
  static: opts.static?,
  static_types: opts[:static_types],
  static_properties: opts[:static_properties]
)
app = Fakeit.build(opts[:spec], options)

$stderr.reopen(File.new(opts[:log_file], File::WRONLY | File::TRUNC | File::CREAT)) if opts[:log_file]
$stderr.sync = true

app.use Rack::Cors do
  allow do
    origins '*'
    resource '*', headers: :any, methods: :any
  end
end
app.use(Fakeit::Middleware::Recorder) unless opts.quiet?

server.run(app, Port: opts[:port], Host: '0.0.0.0')

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
fakeit-0.9.2 bin/fakeit
fakeit-0.9.1 bin/fakeit
fakeit-0.9.0 bin/fakeit
fakeit-0.8.1 bin/fakeit
fakeit-0.8.0 bin/fakeit
fakeit-0.7.1 bin/fakeit
fakeit-0.7.0 bin/fakeit