Class: TwistCapistrano::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(twist_secret) ⇒ Client

Returns a new instance of Client.



8
9
10
# File 'lib/twist_capistrano.rb', line 8

def initialize(twist_secret)
  @twist_secret = twist_secret
end

Instance Method Details

#send(message, twist_msg_id) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/twist_capistrano.rb', line 12

def send(message, twist_msg_id)  
  return if @twist_secret.nil? || @twist_secret.empty?
     
  uri = URI.parse("https://api.twist.io/api/v1/hook/capistrano?secret=#{@twist_secret}")      
  req = Net::HTTP::Post.new(uri.request_uri)
  
  req.body = {
      'message' => "#{message}",
      'parent_id' => twist_msg_id.to_s
  }.to_json
  
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  res = http.start {|http| http.request(req) }
  
  response = JSON.parse(res.body)
  response['id']
rescue
  puts "Sending message to Twist failed."
end