Class: Octogate::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/octogate/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ Client

Returns a new instance of Client.



7
8
9
# File 'lib/octogate/client.rb', line 7

def initialize(event)
  @event = event
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



5
6
7
# File 'lib/octogate/client.rb', line 5

def event
  @event
end

Instance Method Details

#request(t) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/octogate/client.rb', line 28

def request(t)
  uri = URI(t.url)

  options = {url: t.url}
  options.merge!(ssl_options) if uri.scheme == "https"

  conn = build_connection(options, t.username, t.password)

  params = t.params.respond_to?(:call) ? t.params.call(event) : t.params

  case t.http_method
  when :get
    conn.get do |req|
      req.url uri.path
      params.each do |k, v|
        req.params[k] = v
      end
    end
  when :post
    if t.parameter_type == :json
      conn.post uri.path do |req|
        req.headers['Content-Type'] = 'application/json'
        req.body = Oj.dump(params)
      end
    else
      conn.post uri.path, params
    end
  end
end

#request_to_targetsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/octogate/client.rb', line 11

def request_to_targets
  Octogate.config.targets.each do |t|
    condition = event.default_condition
    case t.match
    when Proc
      condition = condition && instance_exec(event, &t.match)
    when nil
    else
      condition = condition && !!t.match
    end

    if condition
      request(t)
    end
  end
end