hcutil 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0a64ad7f82b4fb5af8e88458092803beea4c84a5
4
+ data.tar.gz: 48b55eb4b072998fd8ef5c118033a85481a4e06c
5
+ SHA512:
6
+ metadata.gz: 3d65e247187b8f5b518c69786fe51cfbfefa2140383311c0e08458ff99f0886ce6f4dfc1d1882daaf1acf7b19c7ca49a947988bae7ec01ca12480f776e719496
7
+ data.tar.gz: 249393fa25dcb5d14e280a6dfba0ed552796ac314b9f2ad36f11378b810a874d4c90fa72ff95388dfb14878503e2682cbe241d523c4ee973f3a1725167537a59
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org>
data/bin/hcutil ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # vim: ft=ruby
3
+
4
+ require 'rubygems'
5
+ require 'hcutil'
6
+
7
+ HCUtil::CLI.start(ARGV)
@@ -0,0 +1,29 @@
1
+ module HCUtil
2
+ class AuthError < StandardError
3
+ def initialize(data)
4
+ @data = data
5
+ end
6
+ end
7
+
8
+ class Auth
9
+
10
+ attr_reader :auth_token
11
+
12
+ def initialize(options = {})
13
+ if ENV['HOME'].nil? or
14
+ ENV['HOME'] == '' or
15
+ (not Dir.exist?(ENV['HOME']))
16
+ raise AuthError.new('[error]: HOME env var not set or dir does not exist')
17
+ end
18
+
19
+ auth_file = File.join(ENV['HOME'], '.hcapi')
20
+ if File.exists?(auth_file)
21
+ @auth_token = File.read(auth_file).gsub("\n", ' ').squeeze(' ')
22
+ $stderr.puts("Auth token: #{auth_token}") if options[:verbose]
23
+ else
24
+ raise AuthError.new('[error]: missing auth token file ~/.hcapi')
25
+ end
26
+ end
27
+ end
28
+ end
29
+
data/lib/hcutil/cli.rb ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env ruby
2
+ # vim:ft=ruby:sw=2:ts=2
3
+
4
+ require 'thor'
5
+ require 'hcutil/version'
6
+ require 'hcutil/copier'
7
+
8
+ module HCUtil
9
+ class CLI < Thor
10
+
11
+ class_option(:room,
12
+ :desc => 'HipChat room with which to work',
13
+ :aliases => '-r',
14
+ :type => :string,
15
+ :default => 'Client Services')
16
+
17
+ class_option(:verbose,
18
+ :desc => 'Verbose output',
19
+ :type => :boolean,
20
+ :aliases => '-v')
21
+
22
+ class_option(:debug,
23
+ :desc => 'Debug output',
24
+ :type => :boolean)
25
+
26
+ desc('version', 'show version')
27
+ def version()
28
+ puts("version: #{HCUtil::VERSION}")
29
+ end
30
+
31
+ desc('copy ROOM_NAME', 'copy chat messages from ROOM_NAME')
32
+ option(:date,
33
+ :desc => 'Date from which to copy messages',
34
+ :type => :string,
35
+ :aliases => '-d',
36
+ :default => Time.now.to_s)
37
+ option(:num_items,
38
+ :desc => 'Number of chat messages to copy',
39
+ :aliases => '-n',
40
+ :type => :numeric,
41
+ :default => 25)
42
+ def copy(room_name)
43
+ copier = HCUtil::Copier.new(room_name, options)
44
+ copier.copy()
45
+ end
46
+ end
47
+ end
48
+
@@ -0,0 +1,84 @@
1
+ require 'json'
2
+ require 'rest_client'
3
+ require 'time'
4
+
5
+ require 'hcutil/auth'
6
+
7
+ module HCUtil
8
+
9
+ class CopierError < StandardError
10
+ end
11
+
12
+ class Copier
13
+ def initialize(room_name = 'Client Services', options = {})
14
+ @room_name = room_name
15
+ @options = options
16
+
17
+ @debug = @options[:debug]
18
+ @verbose = @options[:verbose] || @options[:debug]
19
+ @num_items = @options[:num_items] ||25
20
+
21
+ RestClient.log = 'stdout' if @debug
22
+ @auth = Auth.new(@options)
23
+ end
24
+
25
+ def copy
26
+ room_id = 0
27
+
28
+ param_arg = {
29
+ :accept => :json,
30
+ :params => {
31
+ :auth_token => @auth.auth_token
32
+ }
33
+ }
34
+ RestClient.get('https://api.hipchat.com/v2/room', param_arg) do |response, request, result|
35
+ json = JSON.parse(response.body)
36
+ items = json['items']
37
+ items.each do |item|
38
+ if item['name'] == @room_name
39
+ room_id = item['id']
40
+ $stderr.puts("Room '#{room_name}' has ID #{room_id}") if @verbose
41
+ break
42
+ end
43
+ end
44
+ end
45
+
46
+ chat_date = @options[:date]
47
+ $stderr.puts("Getting history for #{chat_date.to_s}") if @verbose
48
+
49
+ param_arg = {
50
+ :accept => :json,
51
+ :params => {
52
+ :auth_token => @auth.auth_token,
53
+ :date => chat_date,
54
+ 'max-results' => @num_items
55
+ }
56
+ }
57
+
58
+ json = nil
59
+ RestClient.get("https://api.hipchat.com/v2/room/#{room_id}/history", param_arg) do |response, request, result|
60
+ json = JSON.parse(response.body)
61
+ end
62
+
63
+ if @debug
64
+ chat_json_file = "messages-#{room_id}-#{chat_date.strftime('%Y-%m-%d')}.json"
65
+ File.open(chat_json_file, 'w') { |f| f.write(json.inspect) }
66
+ $stderr.puts "Chat JSON saved to #{chat_json_file}"
67
+ end
68
+
69
+ items = json['items']
70
+ items.each do |item|
71
+ date = Time.parse(item['date']).strftime('%Y-%m-%d %H:%M:%S')
72
+ from = item['from']
73
+ if from.is_a?(Hash)
74
+ name = from['name']
75
+ else
76
+ name = from
77
+ end
78
+ message = item['message']
79
+ puts "#{date} #{name}: #{message}"
80
+ end
81
+ end
82
+ end
83
+ end
84
+
@@ -0,0 +1,3 @@
1
+ module HCUtil
2
+ VERSION = '0.0.1'.freeze
3
+ end
data/lib/hcutil.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'hcutil/version'
2
+ require 'hcutil/cli'
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hcutil
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Luke Bakken
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.19'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.19'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest_client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ description:
56
+ email:
57
+ - luke@bowbak.org
58
+ executables:
59
+ - hcutil
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - LICENSE
64
+ - bin/hcutil
65
+ - lib/hcutil.rb
66
+ - lib/hcutil/auth.rb
67
+ - lib/hcutil/cli.rb
68
+ - lib/hcutil/copier.rb
69
+ - lib/hcutil/version.rb
70
+ homepage: http://github.com/lukebakken/hipchat-util
71
+ licenses: []
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project: hipchat-util
89
+ rubygems_version: 2.2.1
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Utility for working with HipChat from the command line
93
+ test_files: []