Class: HCUtil::Paster

Inherits:
OpBase
  • Object
show all
Defined in:
lib/hcutil/paster.rb

Instance Method Summary collapse

Methods inherited from OpBase

#get_request, #post_request

Constructor Details

#initialize(thing, options = {}) ⇒ Paster

Returns a new instance of Paster.



8
9
10
11
12
13
14
# File 'lib/hcutil/paster.rb', line 8

def initialize(thing, options = {})
  super(options)
  @thing = thing
  @room = options[:room]
  @ticket = options[:ticket]
  @summary = options[:summary]
end

Instance Method Details

#pasteObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hcutil/paster.rb', line 16

def paste
  if @thing == '-'
    thing_contents = $stdin.read
  elsif File.exists?(@thing)
    thing_contents = File.read(@thing)
  else
    thing_contents = @thing
  end

  if @ticket
    ticket_text = "ticket ##{@ticket} - "
  end
  unless ticket_text.nil_or_empty? and @summary.nil_or_empty?
    paste_header = "#{ticket_text}#{@summary}\n"
  end
  message = <<"EOT"
#{paste_header}
#{thing_contents}
EOT
  post_data = {
    :message_format => 'text',
    :color => 'purple',
    :message => message
  }

  header_args = {
    :content_type => :json,
    :accept => :json,
    :authorization => "Bearer #{@auth.auth_token}"
  }

  room_esc = URI.escape(@room.to_s)
  uri = "https://api.hipchat.com/v2/room/#{room_esc}/notification"
  post_request(uri, post_data.to_json, header_args)
end