Class: Lolcommits::LolHipchat

Inherits:
Plugin
  • Object
show all
Defined in:
lib/lolcommits/plugins/lol_hipchat.rb

Instance Attribute Summary

Attributes inherited from Plugin

#options, #runner

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

#configuration, #debug, #enabled?, #execute_postcapture, #execute_precapture, #initialize, #log_error, #parse_user_input, #puts, #run_precapture, #valid_configuration?

Constructor Details

This class inherits a constructor from Lolcommits::Plugin

Class Method Details

.nameObject



117
118
119
# File 'lib/lolcommits/plugins/lol_hipchat.rb', line 117

def self.name
  'hipchat'
end

.runner_orderObject



121
122
123
# File 'lib/lolcommits/plugins/lol_hipchat.rb', line 121

def self.runner_order
  :postcapture
end

Instance Method Details

#api_urlObject



78
79
80
# File 'lib/lolcommits/plugins/lol_hipchat.rb', line 78

def api_url
  URI(format('http://%{api_team}.hipchat.com/v2/room/%{api_room}/share/file?auth_token=%{api_token}', symbolized_configuration))
end

#configure_auth_optionsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lolcommits/plugins/lol_hipchat.rb', line 10

def configure_auth_options
  puts '-' * 50
  puts ' Lolcommits HipChat Plugin Configuration'
  puts '-' * 50

  puts '1.) I need your Team Name '
  puts 'teamname as in teamname.hipchat.com, without .hipchat.com'
  print 'Your Teamname: '
  teamname = STDIN.gets.to_s.strip
  puts "2.) We need a Authentication Token, get yours at https://#{teamname}.hipchat.com/account/api"
  puts 'make sure to select scope "Send Message"'
  print 'Your auth_token: '
  token = STDIN.gets.to_s.strip
  puts '3.) Which Room should be we post to?'
  puts 'can be a id or name'
  print 'Your Room: '
  room = STDIN.gets.to_s.strip

  {
    'api_token' => token,
    'api_team' => teamname,
    'api_room' => room
  }
end

#configure_options!Object



4
5
6
7
8
# File 'lib/lolcommits/plugins/lol_hipchat.rb', line 4

def configure_options!
  options = super
  options.merge! configure_auth_options if options['enabled']
  options
end

#configured?Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
# File 'lib/lolcommits/plugins/lol_hipchat.rb', line 110

def configured?
  super &&
    configuration['api_token'] &&
    configuration['api_team'] &&
    configuration['api_room']
end

#messageObject



86
87
88
# File 'lib/lolcommits/plugins/lol_hipchat.rb', line 86

def message
  "commited some #{random_adjective} #{random_object} to #{runner.vcs_info.repo}@#{runner.sha} (#{runner.vcs_info.branch}) "
end

#message_jsonObject



59
60
61
# File 'lib/lolcommits/plugins/lol_hipchat.rb', line 59

def message_json
  { :message => message }.to_json.force_encoding('utf-8')
end

#message_partObject



49
50
51
52
53
54
55
56
57
# File 'lib/lolcommits/plugins/lol_hipchat.rb', line 49

def message_part
  [
    'Content-Type: application/json; charset=UTF-8',
    'Content-Disposition: attachment; name="metadata"',
    '',
    message_json,
    ''
  ].join "\r\n"
end

#pictureObject



74
75
76
# File 'lib/lolcommits/plugins/lol_hipchat.rb', line 74

def picture
  @picture ||= File.new(runner.main_image)
end

#picture_partObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/lolcommits/plugins/lol_hipchat.rb', line 63

def picture_part
  mime_type = MIME::Types.type_for(picture.path)[0] || MIME::Types['application/octet-stream'][0]
  [
    format('Content-Type: %s', mime_type.simplified),
    format('Content-Disposition: attachment; name="file"; filename="%s"', picture.path),
    '',
    "#{picture.read} ",
    ''
  ].join "\r\n"
end

#random_adjectiveObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/lolcommits/plugins/lol_hipchat.rb', line 96

def random_adjective
  adjectives = %w(adaptable adventurous affable affectionate agreeable ambitious amiable amicable amusing brave \
                  bright broad-minded calm careful charming communicative compassionate conscientious considerate \
                  convivial courageous courteous creative decisive determined diligent diplomatic discreet dynamic \
                  easygoing emotional energetic enthusiastic exuberant fair-minded faithful fearless forceful \
                  frank friendly funny generous gentle good gregarious hard-working helpful honest humorous \
                  imaginative impartial independent intellectual intelligent intuitive inventive kind loving loyal \
                  modest neat nice optimistic passionate patient persistent pioneering philosophical placid plucky \
                  polite powerful practical pro-active quick-witted quiet rational reliable reserved resourceful \
                  romantic self-confident self-disciplined sensible sensitive shy sincere sociable straightforward \
                  sympathetic thoughtful tidy tough unassuming understanding versatile warmhearted willing witty)
  adjectives.sample
end

#random_objectObject



90
91
92
93
94
# File 'lib/lolcommits/plugins/lol_hipchat.rb', line 90

def random_object
  objects = %w(screws bolts exceptions errors cookies)

  objects.sample
end

#run_postcaptureObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lolcommits/plugins/lol_hipchat.rb', line 35

def run_postcapture
  return unless valid_configuration?

  http = Net::HTTP.new(api_url.host, api_url.port)
  # http.set_debug_output $stderr # nice for debugging, never ever release with it
  http.start do |connection|
    header = { 'Content-Type' => 'multipart/related; boundary=0123456789ABLEWASIEREISAWELBA9876543210' }
    data = [message_part, picture_part].map do |part|
      "--0123456789ABLEWASIEREISAWELBA9876543210\r\n#{part}"
    end.join('') << '--0123456789ABLEWASIEREISAWELBA9876543210--'
    connection.post("#{api_url.path}?#{api_url.query}", data, header)
  end
end

#symbolized_configurationObject



82
83
84
# File 'lib/lolcommits/plugins/lol_hipchat.rb', line 82

def symbolized_configuration
  @symbolized_configuration ||= configuration.each_with_object({}) { |(k, v), obj| obj[k.to_sym] = v }
end