Class: CardWall::Alerts::Gateway

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

Instance Method Summary collapse

Constructor Details

#initialize(username, password, cardwall_url = "https://www.cardwall.co.uk") ⇒ Gateway

Returns a new instance of Gateway.



8
9
10
11
12
# File 'lib/alerts/gateway.rb', line 8

def initialize(username, password, cardwall_url = "https://www.cardwall.co.uk")
  @username = username
  @password = password
  @base_url = cardwall_url
end

Instance Method Details

#post(project, title, body, file = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/alerts/gateway.rb', line 14

def post(project, title, body, file = nil)
  url = URI.parse(URI.escape("#{@base_url}/projects/#{project}/alerts"))
  puts "posting to: #{url}"

  req = Net::HTTP::Post::Multipart.new url.path, request_hash(title, body, file)
  req.basic_auth @username, @password

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true if url.port == 443
  result = http.start do |http|
    http.request(req)
  end
  
  return if result.is_a?(Net::HTTPSuccess)
  
  puts "Result: #{result.inspect}"
  puts "Could not post alert: #{result.body}"
  
  result.error!
rescue Net::HTTPServerException, Errno::ECONNREFUSED => e
  raise AlertPostError.new(e.to_s)
end

#request_hash(title, body, file) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/alerts/gateway.rb', line 37

def request_hash(title, body, file)
  request_hash = {"title" => title, "body" => body}
  
  if file
    s = StringIO.new("", "w+")
    gz = Zlib::GzipWriter.new(s)
    gz.write file[:file_body]
    gz.close
    
    request_hash["file"] = UploadIO.new(StringIO.new(s.string), "text/plain", file[:file_name])
  end
  
  return request_hash
end