Class: Xify::Base::RocketChat

Inherits:
Object
  • Object
show all
Defined in:
lib/xify/base/rocket_chat.rb

Direct Known Subclasses

Input::RocketChat, Output::RocketChat

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ RocketChat

Returns a new instance of RocketChat.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/xify/base/rocket_chat.rb', line 8

def initialize(config)
  @config = config

  uri = URI.parse config['uri']
  @http = Net::HTTP.new uri.host, uri.port
  @http.use_ssl = true

  working_dir = "#{ENV['HOME']}/.xify/RocketChat"
  Dir.mkdir working_dir rescue Errno::EEXIST
  @auth_file = "#{working_dir}/#{@config['user']}.json"
end

Instance Method Details

#request(method, path) {|req| ... } ⇒ Object

Yields:

  • (req)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/xify/base/rocket_chat.rb', line 20

def request(method, path, &block)
   unless @auth_data

  req = Object.const_get("Net::HTTP::#{method.capitalize}").new path,
    'X-User-Id' => @auth_data['userId'],
    'X-Auth-Token' => @auth_data['authToken']

  yield req if block_given?

  res = @http.request req

  case res
  when Net::HTTPUnauthorized
    relogin
    request method, path, &block
  when Net::HTTPSuccess
    # nothing
  else
    $stderr.puts res.body
    raise "Error on #{method.upcase} #{@config['uri']}#{path}: #{res.code} #{res.message}"
  end

  res
end