Class: Roust

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Queue, Ticket, User
Defined in:
lib/roust.rb,
lib/roust/user.rb,
lib/roust/queue.rb,
lib/roust/ticket.rb,
lib/roust/version.rb

Defined Under Namespace

Modules: Queue, Ticket, User

Constant Summary collapse

VERSION =
'1.8.9'

Instance Method Summary collapse

Methods included from User

#user_create, #user_show, #user_update

Methods included from Queue

#queue_show

Methods included from Ticket

#ticket_comment, #ticket_create, #ticket_history, #ticket_links_add, #ticket_links_remove, #ticket_links_show, #ticket_search, #ticket_show, #ticket_update

Constructor Details

#initialize(credentials, headers = {}) ⇒ Roust

Returns a new instance of Roust.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/roust.rb', line 15

def initialize(credentials, headers = {})
  @server   = credentials[:server]
  @username = credentials[:username]
  @password = credentials[:password]

  if @server =~ /REST\/1\.0/
    raise ArgumentError, 'The supplied :server has REST in the URL. You only need to specify the base, e.g. http://rt.example.org/'
  end

  headers['User-Agent'] = 'Roust' unless headers.key?('User-Agent')
  self.class.headers.merge!(headers)
  authenticate!
end

Instance Method Details

#authenticate!Object

Raises:



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
# File 'lib/roust.rb', line 29

def authenticate!
  # - There is no way to authenticate against the API. The only way to log
  #   in is to fill out the same HTML form humans fill in, cache the cookies
  #   returned, and send them on every subsequent request.
  # - RT does not provide *any* indication that the authentication request
  #   has succeeded or failed. RT will always return a HTTP 200.

  self.class.base_uri(@server)

  response = self.class.post(
    '/index.html',
    :body => {
      :user => @username,
      :pass => @password
    }
  )

  cookie = response.headers['set-cookie']
  self.class.headers['Cookie'] = cookie if cookie

  # Switch the base uri over to the actual REST API base uri.
  self.class.base_uri "#{@server}/REST/1.0"

  # - The easiest way to programatically check if an authentication request
  #   succeeded is by doing a request for a ticket, and seeing if the API
  #   responds with some specific text ("401 Credentials required") that
  #   indicates authentication has previously failed.
  # - The authenticated? method will return false if an Unauthenticated
  #   exception bubbles up from response handling. We (dirtily) rethrow the
  #   exception.
  raise Unauthenticated unless authenticated?
end

#authenticated?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/roust.rb', line 62

def authenticated?
  return true if show('1')
rescue Unauthenticated
  return false
end