Class: Loadrunner::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/loadrunner/client.rb

Overview

Send simulated GitHub events to any webhook server

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Attribute Details

#encodingObject

Returns the value of attribute encoding.



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

def encoding
  @encoding
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#host_pathObject

Returns the value of attribute host_path.



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

def host_path
  @host_path
end

#payloadObject

Returns the value of attribute payload.



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

def payload
  @payload
end

#secret_tokenObject

Returns the value of attribute secret_token.



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

def secret_token
  @secret_token
end

Instance Method Details

#send_event(event = :push, opts = {}) ⇒ Object

Send a simulated event using a shorthand syntax. opts can contain any of these:

  • repo: repository name

  • ref: ref ID (for example ref/heads/branchname)

  • branch: branch name

  • tag: tag name



33
34
35
36
# 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) ⇒ Object

Send a simulated event. Payload can be a hash or a JSON string.



39
40
41
42
43
# 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