hcutil 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3618c39717485726f3dd3e5ef4a72b96f99acf4
4
- data.tar.gz: db70565f9a28b15923f7142e29b846621927ee7f
3
+ metadata.gz: bf3a6882a040189533cd1a350eb8fc306cce1218
4
+ data.tar.gz: 8289619d46f7cd79cf2ef67c0d9aa68adb88823a
5
5
  SHA512:
6
- metadata.gz: e7ff2825adbb74364a3ddc8c1323ac7916c4ee8638dfd8c6abf178302232c2cd339959be6c54e61b06a887b799212d5969d270a2b74f0c895445477a80360bab
7
- data.tar.gz: 8424f71db151157a418918739e40f101b03487d4f6319c758b8b13d6439c9aa642a0db560131a0846938fe0fd6476b4ab28576e88fe416b16e7cab731044066d
6
+ metadata.gz: 963143ee74e502eba3e0dc746c4a4a8d0f4373bf3352b5253fd59f160a7921ba210ebfa3e75bc8bc38b86e31cd9a8fa159182456ceff7f9c865c152a859481ce
7
+ data.tar.gz: 4be63ce8ec8198671b38a14e563edfcb1540d5442ec8b9685255efce8cd1a771bff201b0acc9c1d8a59ffadc0d649af531a7190107fe32cf071bdc60b4997a39
@@ -1,10 +1,4 @@
1
1
  module HCUtil
2
- class AuthError < StandardError
3
- def initialize(msg = 'AuthError')
4
- super
5
- end
6
- end
7
-
8
2
  class Auth
9
3
 
10
4
  attr_reader :auth_token
@@ -12,7 +6,7 @@ module HCUtil
12
6
  def initialize(options = {})
13
7
  home_env = ENV['HOME']
14
8
  if home_env.nil_or_empty? or not Dir.exist?(ENV['HOME'])
15
- raise(AuthError, 'HOME env var not set or dir does not exist')
9
+ raise(Errors::AuthError, 'HOME env var not set or dir does not exist')
16
10
  end
17
11
 
18
12
  auth_file = File.join(ENV['HOME'], '.hcapi')
@@ -20,7 +14,7 @@ module HCUtil
20
14
  @auth_token = File.read(auth_file).gsub("\n", ' ').squeeze(' ')
21
15
  $stderr.puts("Auth token: #{auth_token}") if options[:verbose]
22
16
  else
23
- raise(AuthError, 'missing auth token file ~/.hcapi')
17
+ raise(Errors::AuthError, 'missing auth token file ~/.hcapi')
24
18
  end
25
19
  end
26
20
  end
@@ -3,8 +3,9 @@
3
3
 
4
4
  require 'thor'
5
5
  require 'hcutil/version'
6
+ require 'hcutil/errors'
6
7
  require 'hcutil/copier'
7
- require 'hcutil/pinger'
8
+ require 'hcutil/paster'
8
9
 
9
10
  module HCUtil
10
11
  class CLI < Thor
@@ -24,11 +25,6 @@ module HCUtil
24
25
  end
25
26
 
26
27
  desc('copy ROOM_NAME', 'copy chat messages from ROOM_NAME')
27
- option(:room,
28
- :desc => 'HipChat room with which to work',
29
- :aliases => '-r',
30
- :type => :string,
31
- :default => 'Client Services')
32
28
  option(:date,
33
29
  :desc => 'Date from which to copy messages',
34
30
  :type => :string,
@@ -43,17 +39,30 @@ module HCUtil
43
39
  begin
44
40
  copier = HCUtil::Copier.new(room_name, options)
45
41
  copier.copy()
46
- rescue AuthError, CopierError => e
42
+ rescue Errors::HCUtilError => e
47
43
  $stderr.puts("[error] #{e.message}")
48
44
  end
49
45
  end
50
46
 
51
- desc('ping TICKET_NUM RESPONSE_FILE SUMMARY', 'Ping Client Services HipChat room for ticket TICKET_NUM with text from RESPONSE_FILE. SUMMARY is optional.')
52
- def ping(ticket_num, response_file, summary=nil)
47
+ desc('paste FILE', 'Paste from file (- for stdin) into HipChat room')
48
+ option(:room,
49
+ :desc => 'HipChat room name or number into which to paste data',
50
+ :type => :string,
51
+ :aliases => '-r',
52
+ :default => 61640)
53
+ option(:ticket,
54
+ :desc => 'Ticket number, optional. If present will create message header',
55
+ :type => :numeric,
56
+ :aliases => '-t')
57
+ option(:summary,
58
+ :desc => 'Summary, optional. If present will create message header',
59
+ :type => :string,
60
+ :aliases => '-s')
61
+ def paste(file)
53
62
  begin
54
- pinger = HCUtil::Pinger.new(ticket_num, response_file, summary, options)
55
- pinger.ping()
56
- rescue AuthError, PingerError => e
63
+ paster = HCUtil::Paster.new(file, options)
64
+ paster.paste()
65
+ rescue Errors::HCUtilError => e
57
66
  $stderr.puts("[error] #{e.message}")
58
67
  end
59
68
  end
@@ -1,13 +1,8 @@
1
+ require 'hcutil/errors'
1
2
  require 'hcutil/op_base'
2
3
 
3
4
  module HCUtil
4
5
 
5
- class CopierError < StandardError
6
- def initialize(msg = 'CopierError')
7
- super
8
- end
9
- end
10
-
11
6
  class Copier < OpBase
12
7
  def initialize(room_name = 'Client Services', options = {})
13
8
  super(options)
@@ -24,7 +19,8 @@ module HCUtil
24
19
  :auth_token => @auth.auth_token
25
20
  }
26
21
  }
27
- RestClient.get('https://api.hipchat.com/v2/room', param_arg) do |response, request, result|
22
+ uri = 'https://api.hipchat.com/v2/room'
23
+ RestClient.get(uri, param_arg) do |response, request, result|
28
24
  if result.is_a? Net::HTTPSuccess
29
25
  json = JSON.parse(response.body)
30
26
  items = json['items']
@@ -36,10 +32,14 @@ module HCUtil
36
32
  end
37
33
  end
38
34
  else
39
- raise(CopierError, "REST error: #{result}|#{response}")
35
+ raise(Errors::RESTError.new(result, uri, response))
40
36
  end
41
37
  end
42
38
 
39
+ if room_id == 0
40
+ raise(Errors::CopierError, "Room with name '#{@room_name}' could not be found")
41
+ end
42
+
43
43
  chat_date = @options[:date]
44
44
  $stderr.puts("Getting history for #{chat_date.to_s}") if @verbose
45
45
 
@@ -52,12 +52,13 @@ module HCUtil
52
52
  }
53
53
  }
54
54
 
55
+ uri = "https://api.hipchat.com/v2/room/#{room_id}/history"
55
56
  json = nil
56
- RestClient.get("https://api.hipchat.com/v2/room/#{room_id}/history", param_arg) do |response, request, result|
57
+ RestClient.get(uri, param_arg) do |response, request, result|
57
58
  if result.is_a? Net::HTTPSuccess
58
59
  json = JSON.parse(response.body)
59
60
  else
60
- raise(CopierError, "REST error: #{result}|#{response}")
61
+ raise(Errors::RESTError.new(result, uri, response))
61
62
  end
62
63
  end
63
64
 
@@ -0,0 +1,32 @@
1
+ module HCUtil
2
+ module Errors
3
+
4
+ class HCUtilError < StandardError; end
5
+
6
+ class AuthError < HCUtilError
7
+ def initialize(msg = 'AuthError')
8
+ super
9
+ end
10
+ end
11
+
12
+ class RESTError < HCUtilError
13
+ def initialize(result, uri, response)
14
+ super("#{result.code} #{result.msg}\n#{uri}\n#{response}")
15
+ end
16
+ end
17
+
18
+ class CopierError < HCUtilError
19
+ def initialize(msg = 'CopierError')
20
+ super
21
+ end
22
+ end
23
+
24
+ class PasterError < HCUtilError
25
+ def initialize(msg = 'PasterError')
26
+ super
27
+ end
28
+ end
29
+
30
+ end
31
+ end
32
+
@@ -0,0 +1,59 @@
1
+ require 'uri'
2
+ require 'hcutil/errors'
3
+ require 'hcutil/op_base'
4
+
5
+ module HCUtil
6
+
7
+ class Paster < OpBase
8
+ def initialize(file, options = {})
9
+ super(options)
10
+ if file == '-' or File.exists?(file)
11
+ @file = file
12
+ else
13
+ raise(Errors::PasterError, "Response file '#{file}' does not exist")
14
+ end
15
+ @room = options[:room]
16
+ @ticket = options[:ticket]
17
+ @summary = options[:summary]
18
+ end
19
+
20
+ def paste
21
+ if @file == '-'
22
+ file_contents = $stdin.read
23
+ else
24
+ file_contents = File.read(@file)
25
+ end
26
+
27
+ if @ticket
28
+ ticket_text = "ticket ##{@ticket} - "
29
+ end
30
+ unless ticket_text.nil_or_empty? and @summary.nil_or_empty?
31
+ paste_header = "#{ticket_text}#{@summary}\n"
32
+ end
33
+ message = <<"EOT"
34
+ #{paste_header}
35
+ #{file_contents}
36
+ EOT
37
+ post_data = {
38
+ :message_format => 'text',
39
+ :color => 'purple',
40
+ :message => message
41
+ }
42
+
43
+ header_args = {
44
+ :content_type => :json,
45
+ :accept => :json,
46
+ :authorization => "Bearer #{@auth.auth_token}"
47
+ }
48
+
49
+ room_esc = URI.escape(@room.to_s)
50
+ uri = "https://api.hipchat.com/v2/room/#{room_esc}/notification"
51
+ RestClient.post(uri, post_data.to_json, header_args) do |response, request, result|
52
+ unless result.is_a? Net::HTTPSuccess
53
+ raise(Errors::RESTError.new(result, uri, response))
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+
@@ -1,3 +1,3 @@
1
1
  module HCUtil
2
- VERSION = '0.0.3'.freeze
2
+ VERSION = '0.0.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hcutil
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luke Bakken
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-02 00:00:00.000000000 Z
11
+ date: 2014-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -66,9 +66,10 @@ files:
66
66
  - lib/hcutil/auth.rb
67
67
  - lib/hcutil/cli.rb
68
68
  - lib/hcutil/copier.rb
69
+ - lib/hcutil/errors.rb
69
70
  - lib/hcutil/extensions.rb
70
71
  - lib/hcutil/op_base.rb
71
- - lib/hcutil/pinger.rb
72
+ - lib/hcutil/paster.rb
72
73
  - lib/hcutil/version.rb
73
74
  homepage: http://github.com/lukebakken/hipchat-util
74
75
  licenses: []
@@ -1,53 +0,0 @@
1
- require 'hcutil/op_base'
2
-
3
- module HCUtil
4
-
5
- class PingerError < StandardError
6
- def initialize(msg = 'PingerError')
7
- super
8
- end
9
- end
10
-
11
- class Pinger < OpBase
12
- def initialize(ticket_num = nil, response_file = nil, summary = nil, options = {})
13
- super(options)
14
- if ticket_num =~ /^[0-9]{4,}$/
15
- @ticket_num = ticket_num
16
- else
17
- raise(PingerError, 'Ticket number must be 4 or more digits')
18
- end
19
- if File.exists?(response_file)
20
- @response_file = response_file
21
- else
22
- raise(PingerError, "Response file '#{response_file}' does not exist")
23
- end
24
- @summary = summary
25
- end
26
-
27
- def ping
28
- message = <<"EOT"
29
- ticket ##{@ticket_num} - #{@summary}
30
- #{File.read(@response_file)}
31
- EOT
32
- post_data = {
33
- :message_format => 'text',
34
- :color => 'purple',
35
- :message => message
36
- }
37
-
38
- header_args = {
39
- :content_type => :json,
40
- :accept => :json,
41
- :authorization => "Bearer #{@auth.auth_token}"
42
- }
43
-
44
- RestClient.post('https://api.hipchat.com/v2/room/61640/notification',
45
- post_data.to_json, header_args) do |response, request, result|
46
- unless result.is_a? Net::HTTPSuccess
47
- raise(PingerError, "REST error: #{result}|#{response}")
48
- end
49
- end
50
- end
51
- end
52
- end
53
-