Class: HCUtil::Copier

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

Instance Method Summary collapse

Methods inherited from OpBase

#get_request, #post_request

Constructor Details

#initialize(room_name = 'Client Services', options = {}) ⇒ Copier

Returns a new instance of Copier.



9
10
11
12
13
# File 'lib/hcutil/copier.rb', line 9

def initialize(room_name = 'Client Services', options = {})
  super(options)
  @room_name = room_name
  @num_items = @options[:num_items] ||25
end

Instance Method Details

#copyObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/hcutil/copier.rb', line 84

def copy
  room_id = get_room_id
  if room_id == 0
    raise(Errors::CopierError, "Room with name '#{@room_name}' could not be found")
  end

  chat_date = @options[:date]
  $stderr.puts("Getting history for #{chat_date.to_s}") if @verbose

  param_arg = {
    :accept => :json,
    :params => {
      :auth_token => @auth.auth_token,
      :date => chat_date,
      'max-results' => @num_items
    }
  }

  uri = "https://api.hipchat.com/v2/room/#{room_id}/history"
  json = nil
  get_request(uri, param_arg) do |response, request, result|
    if result.is_a? Net::HTTPSuccess
      json = JSON.parse(response.body)
    else
      raise(Errors::RESTError.new(result, uri, response))
    end
  end

  if @debug
    chat_json_file = "messages-#{room_id}-#{chat_date.strftime('%Y-%m-%d')}.json"
    File.open(chat_json_file, 'w') { |f| f.write(json.inspect) }
    $stderr.puts "Chat JSON saved to #{chat_json_file}"
  end

  items = json['items']
  items.each do |item|
    date = Time.parse(item['date']).strftime('%Y-%m-%d %H:%M:%S')
    from = item['from']
    if from.is_a?(Hash)
      name = from['name']
    else
      name = from
    end
    message = item['message']
    puts "#{date} #{name}: #{message}"
  end
end

#get_room_id(start_index = 0) ⇒ Object



15
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hcutil/copier.rb', line 15

def get_room_id(start_index=0)

  uri = 'https://api.hipchat.com/v2/room'

  # First, try to get the room directly by name
  if start_index == 0
    param_arg = {
      :accept => :json,
      :params => {
        'auth_token' => @auth.auth_token
      }
    }
    uri_direct = uri + URI.escape("/#{@room_name}")
    get_request(uri_direct, param_arg) do |response, request, result|
      if result.is_a? Net::HTTPSuccess
        json = JSON.parse(response.body)
        if json['id']
          room_id = json['id']
          $stderr.puts("Room '#{@room_name}' has ID #{room_id}") if @verbose
          return room_id
        end
      end
    end
  end

  param_arg = {
    :accept => :json,
    :params => {
      'auth_token' => @auth.auth_token,
      'start-index' => start_index,
      'max-results' => 100
    }
  }

  room_id = 0

  get_request(uri, param_arg) do |response, request, result|
    if result.is_a? Net::HTTPSuccess
      json = JSON.parse(response.body)
      if json['items']
        items = json['items']
        items.each do |item|
          if item['name'] == @room_name
            room_id = item['id']
            $stderr.puts("Room '#{@room_name}' has ID #{room_id}") if @verbose
            break
          end
        end
        links = json['links']
        if room_id == 0 and not links.nil?
          next_uri = links['next']
          unless next_uri.nil_or_empty?
            query = URI.parse(next_uri).query
            query_params = CGI.parse(query)
            start_index = query_params['start-index'].first
            $stderr.puts("finding '#{@room_name}' with start_index #{start_index}") if @verbose
            return get_room_id(start_index)
          end
        end
      else
        raise(Errors::CopierError, "Room with name '#{@room_name}' could not be found - could not parse JSON")
      end
    else
      raise(Errors::RESTError.new(result, uri, response))
    end
  end
  return room_id
end