Class: HipchatS3

Inherits:
Object
  • Object
show all
Defined in:
lib/hipchat-s3.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HipchatS3

Returns a new instance of HipchatS3.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/hipchat-s3.rb', line 9

def initialize(options = {})

  s3_creds = options.fetch(:s3, {:access_key_id => 'your-key', :secret_access_key => 'your-secret', :bucket => 'bucket'}).symbolize_keys
  hipchat_creds = options.fetch(:hipchat, {:api_token => 'your-token'}).symbolize_keys

  @s3_bucket = s3_creds.delete(:bucket)

  @hipchat_client = HipChat::Client.new(hipchat_creds[:api_token])
  AWS::S3::Base.establish_connection!(s3_creds)

end

Instance Attribute Details

#hipchat_clientObject (readonly)

Returns the value of attribute hipchat_client.



6
7
8
# File 'lib/hipchat-s3.rb', line 6

def hipchat_client
  @hipchat_client
end

#s3_bucketObject

Returns the value of attribute s3_bucket.



7
8
9
# File 'lib/hipchat-s3.rb', line 7

def s3_bucket
  @s3_bucket
end

Instance Method Details

#create_compressed_upload(path, room, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hipchat-s3.rb', line 21

def create_compressed_upload(path, room, options={})
  options = {:username => 'fileuploader', :message => "File Uploaded", :color => 'yellow'}.merge(options)

  unless tar_exists?
    @hipchat_client[room].send(username, "You don't have tar installed on host", :notify => true, :color => color)
    return
  end

  file = tar_with_path(path)
  basename = "#{Time.now.strftime("%Y_%m_%d_%H_%M_%S")}/#{File.basename(file)}"

  AWS::S3::S3Object.store(basename, open(file), @s3_bucket, :access => :public_read)
  @hipchat_client[room].send(options[:username], "#{options[:message]} :: <a href=\"https://s3.amazonaws.com/#{@s3_bucket}/#{basename}\">#{basename}</a>", :notify => true, :color => options[:color])
end

#create_file_upload(file_path, room, options = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/hipchat-s3.rb', line 36

def create_file_upload(file_path, room, options={})
  options = {:username => 'fileuploader', :message => "File Uploaded", :color => 'yellow'}.merge(options)
  basename = "#{Time.now.strftime("%Y_%m_%d_%H_%M_%S")}/#{File.basename(file_path)}"

  AWS::S3::S3Object.store(basename, open(file_path), @s3_bucket, :access => :public_read)
  @hipchat_client[room].send(options[:username], "#{options[:message]} :: <a href=\"https://s3.amazonaws.com/#{@s3_bucket}/#{basename}\">#{basename}</a>", :notify => true, :color => options[:color])
end

#create_inline_image(image_path, room, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hipchat-s3.rb', line 44

def create_inline_image(image_path, room, options={})
  options = {:thumbnail_path => nil, :username => 'fileuploader', :message => "Image Uploaded", :color => 'yellow'}.merge(options)

  timestamp = Time.now.strftime("%Y_%m_%d_%H_%M_%S")
  basename = File.basename(image_path)

  AWS::S3::S3Object.store("#{timestamp}/#{basename}", open(image_path), @s3_bucket, :access => :public_read)

  uri = "https://s3.amazonaws.com/#{@s3_bucket}/#{timestamp}/#{basename}"
  display_uri = uri

  if options[:thumbnail_path]
    thumb_basename = File.basename(options[:thumbnail_path])
    AWS::S3::S3Object.store("#{timestamp}/#{thumb_basename}", open(options[:thumbnail_path]), @s3_bucket, :access => :public_read)
    display_uri = "https://s3.amazonaws.com/#{@s3_bucket}/#{timestamp}/#{thumb_basename}"
  end

  @hipchat_client[room].send(options[:username], "#{options[:message]} <br> <a href=\"#{uri}\"><img src=\"#{display_uri}\" /></a>", :notify => true, :color => options[:color])
end