class Loadrunner::CommandLine

Handles the command line interface

Public Instance Methods

event() click to toggle source
# File lib/loadrunner/command_line.rb, line 18
def event
  client = Client.new client_opts
  response = client.send_event args['EVENT'], payload_opts
  show response
end
payload() click to toggle source
# File lib/loadrunner/command_line.rb, line 24
def payload
  client = Client.new client_opts
  file = args['FILE']
  raise ArgumentError, "File not found: #{file}" unless File.exist? file

  json = File.read file
  response = client.send_payload args['EVENT'], json
  show response
end
server() click to toggle source
# File lib/loadrunner/command_line.rb, line 34
def server
  Server.prepare port: args['--port'], bind: args['--bind']
  Server.run!
end
status() click to toggle source
# File lib/loadrunner/command_line.rb, line 39
def status
  response = Status.update repo: args['REPO'], 
    sha: args['SHA'], 
    state: args['STATE'], 
    context: args['--context'],
    description: args['--desc'],
    url: args['--url']

  show response
end

Private Instance Methods

client_opts() click to toggle source
# File lib/loadrunner/command_line.rb, line 52
def client_opts
  {
    base_url: args['URL'], 
    secret_token: ENV['GITHUB_SECRET_TOKEN'],
    encoding: args['--form'] ? :form : :json
  }
end
json_generate(object) click to toggle source
# File lib/loadrunner/command_line.rb, line 86
def json_generate(object)
  JSON.pretty_generate(JSON.parse object.to_s)
rescue
  object.to_s
end
payload_opts() click to toggle source

Convert command line arguments to a hash suitable for consumption by Client#send. In essence, we are simply converting the input REF argument, which can come in several forms, to a valid git ref.

# File lib/loadrunner/command_line.rb, line 63
def payload_opts
  result = { repo: args['REPO'] }

  ref = args['REF'] || 'master'
  ref = "refs/tags/#{$1}" if ref =~ /^tag=(.+)/
  ref = "refs/heads/#{$1}" if ref =~ /^branch=(.+)/
  ref = "refs/heads/#{ref}" if ref !~ /^refs/

  result[:ref] = ref
  result
end
show(response) click to toggle source

Print the response json to stdout, and the response code to stderr.

# File lib/loadrunner/command_line.rb, line 76
def show(response)
  puts json_generate(response)

  if response.respond_to? :code
    code = response.code.to_s
    color = code =~ /^2\d\d/ ? :txtgrn : :txtred
    say "!#{color}!Response Code: #{code}"
  end
end