class Phabricator::ConduitClient

Public Class Methods

new() click to toggle source
# File lib/phabricator/conduit_client.rb, line 9
def initialize
  @host = Phabricator.host
  @credentials = {
    user: Phabricator.user,
    cert: Phabricator.cert
  }

  # The config is incomplete; try to get the credentials off the ~/.arcrc
  # file instead.
  if @host.nil? || @credentials.values.any? {|v| v.nil?}
    get_credentials_from_file
  end

  connect
end

Public Instance Methods

connect() click to toggle source
# File lib/phabricator/conduit_client.rb, line 25
def connect
  token = Time.now.to_i

  data = {
    client: 'phabricator-ruby',
    clientVersion: Phabricator::VERSION,
    user: @credentials[:user],
    host: @host,
    authToken: token,
    authSignature: Digest::SHA1.hexdigest("#{token}#{@credentials[:cert]}")
  }

  response = JSON.parse(post('conduit.connect', data, __conduit__: true))

  # TODO: Something something error handling

  @conduit = {
    connectionID: response['result']['connectionID'],
    sessionKey: response['result']['sessionKey']
  }
end
request(http_method, method, data={}) click to toggle source
# File lib/phabricator/conduit_client.rb, line 47
def request(http_method, method, data={})
  # TODO: validation on http_method
  self.send(http_method, method, data.merge(__conduit__: @conduit))
end

Private Instance Methods

get_credentials_from_file() click to toggle source
# File lib/phabricator/conduit_client.rb, line 54
def get_credentials_from_file
  filename = File.expand_path('~/.arcrc')

  if File.readable?(filename)
    settings = JSON.parse(File.read(filename))
    user_settings = settings['hosts'].first

    @host = settings['config']['phabricator.uri']
    @credentials = {
      user: user_settings[1]['user'],
      cert: user_settings[1]['cert']
    }
  else
    raise 'No credentials passed in, and ~/.arcrc does not exist or is \
      not readable.'
  end
end
post(method, data, opts={}) click to toggle source
# File lib/phabricator/conduit_client.rb, line 72
def post(method, data, opts={})
  RestClient.post("#{@host}/api/#{method}", {
    params: data.to_json,
    output: 'json'
  }.merge(opts))
end