Module: Stubify

Extended by:
Commander::Methods
Defined in:
lib/stubify.rb,
lib/stubify/io.rb,
lib/stubify/server.rb,
lib/stubify/version.rb

Defined Under Namespace

Classes: IO, Server

Constant Summary collapse

VERSION =
'0.1.2'
DESCRIPTION =
'Create stub environments the easy way'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/stubify.rb', line 9

def options
  @options
end

Class Method Details

.parse_whitelist(file_path) ⇒ Object



51
52
53
54
55
# File 'lib/stubify.rb', line 51

def parse_whitelist(file_path)
  require 'yaml'
  return [] unless !file_path.nil? && Pathname.new(file_path).file?
  return YAML.load_file(file_path)
end

.runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/stubify.rb', line 11

def run
  program :name, 'stubify'
  program :version, Stubify::VERSION
  program :description, Stubify::DESCRIPTION
  program :help, 'Author', 'Carlos Vidal <[email protected]>'

  command :server do |c|
    c.syntax = 'server [options]'
    c.description = 'Runs a local environment that will forward the requests received to the host provided. Responses will be cached'
    c.option '--host STRING', String, 'Host the requests will be redirected to. i.e. https://easy-peasy.io'
    c.option '--port STRING', String, 'Port the local environment will listen to. Default is 4567'
    c.option '--directory DIR', String, 'Path where fixtures will be stored. i.e. fixtures/'
    c.option '--whitelist DIR', String, 'Path to the .yaml file with the whitelisted paths'
    c.option '--verbose', 'Increases the amount of information logged'
    c.action do |args, options|
      # Ensure host is set
      if options.host.nil?
        raise 'Host must be provided. Run help command for more details'
      end

      # Default options
      options.default \
        port: '4567',
        directory: 'fixtures',
        verbose: false,
        whitelisted: parse_whitelist(options.whitelist)

      # Set env variables
      ENV['PORT'] = options.port

      # Set options global attr
      Stubify.options = options

      # Launch Sinatra app
      require 'stubify/server'
    end
  end
  run!
end