class Loadrunner::Client
Send simulated GitHub events to any webhook server
Attributes
encoding[RW]
host[RW]
host_path[RW]
payload[RW]
secret_token[RW]
Public Class Methods
new(opts={})
click to toggle source
# File lib/loadrunner/client.rb, line 11 def initialize(opts={}) @secret_token = opts[:secret_token] @encoding = opts[:encoding] || :json base_url = opts[:base_url] || 'http://localhost:3000' base_url = "http://#{base_url}" unless base_url =~ /^http/ url_parts = URI.parse base_url @host_path = url_parts.path url_parts.path = '' @host = url_parts.to_s self.class.base_uri host end
Public Instance Methods
send_event(event=:push, opts={})
click to toggle source
Send a simulated event using a shorthand syntax. opts can contain any of these:
-
repo
: repository name -
ref
: ref ID (for exampleref/heads/branchname
) -
branch
: branch name -
tag
: tag name
# File lib/loadrunner/client.rb, line 33 def send_event(event=:push, opts={}) payload = build_payload opts send_payload event, payload end
send_payload(event, payload)
click to toggle source
Send a simulated event. Payload can be a hash or a JSON string.
# File lib/loadrunner/client.rb, line 39 def send_payload(event, payload) @payload = payload.is_a?(String) ? payload : payload.to_json @payload = URI.encode_www_form(payload: @payload) if encoding == :form self.class.post host_path, body: @payload, headers: headers(event) end
Private Instance Methods
build_payload(opts={})
click to toggle source
# File lib/loadrunner/client.rb, line 60 def build_payload(opts={}) {}.tap do |result| result[:ref] = ref_from_opts opts result[:repository] = repo_from_opts opts end end
content_type()
click to toggle source
# File lib/loadrunner/client.rb, line 86 def content_type { json: 'application/json', form: 'application/x-www-form-urlencoded', } end
headers(event=:push)
click to toggle source
# File lib/loadrunner/client.rb, line 47 def headers(event=:push) {}.tap do |header| header['X-GitHub-Event'] = event.to_s if event header['X-Hub-Signature'] = signature if secret_token header['Content-Type'] = content_type[encoding] end end
ref_from_opts(opts)
click to toggle source
# File lib/loadrunner/client.rb, line 67 def ref_from_opts(opts) if opts[:ref] opts[:ref] elsif opts[:branch] "refs/heads/#{opts[:branch]}" elsif opts[:tag] "refs/tags/#{opts[:tag]}" end end
repo_from_opts(opts)
click to toggle source
# File lib/loadrunner/client.rb, line 77 def repo_from_opts(opts) if opts[:repo] =~ /.+\/.+/ _owner, name = opts[:repo].split '/' { name: name, full_name: opts[:repo] } else { name: opts[:repo] } end end
signature()
click to toggle source
# File lib/loadrunner/client.rb, line 55 def signature return nil unless secret_token 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), secret_token, payload) end