class HelpDeskAPI::Client

Public Class Methods

new(username, password) click to toggle source
# File lib/helpdeskapi.rb, line 17
def initialize(username, password)
  HelpDeskAPI::Authentication.username = username
  HelpDeskAPI::Authentication.password = password
  HelpDeskAPI::Authentication.authenticity_token = nil
  HelpDeskAPI::Authentication.cookies = nil
  sign_in
end

Public Instance Methods

sign_in() click to toggle source

Authenicate user and set cookies. This will be called automatically on endpoint request.

# File lib/helpdeskapi.rb, line 27
def sign_in
  # Contact sign in page to set cookies.
  begin
    sign_in_res = RestClient.get(Endpoints::SIGN_IN)
  rescue RestClient::ExceptionWithResponse => error
    fail HelpDeskAPI::Exceptions.SignInError, "Error contacting #{Endpoints::SIGN_IN}: #{error}"
  end

  # Parse authenticity_token from sign in form.
  page = Nokogiri::HTML(sign_in_res)
  HelpDeskAPI::Authentication.authenticity_token = page.css('form').css('input')[1]['value']
  unless HelpDeskAPI::Authentication.authenticity_token
    fail HelpDeskAPI::Exceptions.AuthenticityTokenError, 'Error parsing authenticity_token: Token not found.'
  end
  # Parse sign_in HTML for csrf-token
  page.css('meta').each do |tag|
    HelpDeskAPI::Authentication.csrf_token = tag['content'] if tag['name'] == 'csrf-token'
  end
  unless HelpDeskAPI::Authentication.csrf_token
    fail HelpDeskAPI::Exceptions.CsrfTokenError, 'No csrf-token found'
  end

  # Set cookies for later requests
  HelpDeskAPI::Authentication.cookies = sign_in_res.cookies

  # Simulate sign in form submit button.
  body = {
    'authenticity_token': HelpDeskAPI::Authentication.authenticity_token,
    'user[email_address]': HelpDeskAPI::Authentication.username,
    'user[password]': HelpDeskAPI::Authentication.password
  }
  RestClient.post(Endpoints::SESSIONS, body, {:cookies => HelpDeskAPI::Authentication.cookies}) do |response, request, result, &block|
    # Response should be a 302 redirect from /sessions
    if Request::responseError?(response)
      fail HelpDeskAPI::Exceptions.SessionsError, "Error contacting #{Endpoints::SESSIONS}: #{error}"
    end
    # Update cookies just incase
    HelpDeskAPI::Authentication.cookies = response.cookies
  end
end