Class: Signalwire::Relay::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/signalwire/relay/task.rb

Constant Summary collapse

DEFAULT_HOST =
"relay.signalwire.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project:, token:, host: nil) ⇒ Task

Returns a new instance of Task.



10
11
12
13
14
# File 'lib/signalwire/relay/task.rb', line 10

def initialize(project:, token:, host: nil )
  @project = project
  @token = token
  @host = host || DEFAULT_HOST
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/signalwire/relay/task.rb', line 8

def host
  @host
end

Instance Method Details

#deliver(context:, message:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/signalwire/relay/task.rb', line 16

def deliver(context:, message:)
  message = JSON.generate({
    context: context,
    message: message
  })
  conn = Faraday.new(
    url: normalize_host(@host),
    headers: {'Content-Type' => 'application/json'}
  )
  conn.basic_auth(@project, @token)

  resp = conn.post('/api/relay/rest/tasks') do |req|
    req.body = message
  end
  return resp.status == 204
end

#normalize_host(passed_host) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/signalwire/relay/task.rb', line 33

def normalize_host(passed_host)
  uri = URI.parse(passed_host)
  if uri.scheme.nil? && uri.host.nil?
    unless uri.path.nil?
      uri.scheme = 'https'
      uri.host = uri.path
      uri.path = ''
    end
  end
  uri.to_s
end