Class: Casual::Client
- Inherits:
-
Object
- Object
- Casual::Client
- Defined in:
- lib/casual.rb
Instance Attribute Summary collapse
-
#callback_url ⇒ Object
Returns the value of attribute callback_url.
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#https ⇒ Object
Returns the value of attribute https.
-
#path ⇒ Object
Returns the value of attribute path.
-
#port ⇒ Object
Returns the value of attribute port.
Instance Method Summary collapse
- #acquire_ticket(login_page) ⇒ Object
- #authenticate(username, password) ⇒ Object
- #authenticate_ticket(ticket) ⇒ Object
- #authorization_url ⇒ Object
- #connection ⇒ Object
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
- #no_slash_path ⇒ Object
- #protocol ⇒ Object
- #server_url ⇒ Object
- #user_login(ticket) ⇒ Object
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 |
# File 'lib/casual.rb', line 8 def initialize(config) @hostname = config[:hostname] @path = config[:path] @https = config[:https] @port = config[:port] || 443 @callback_url = config[:callback_url] end |
Instance Attribute Details
#callback_url ⇒ Object
Returns the value of attribute callback_url.
6 7 8 |
# File 'lib/casual.rb', line 6 def callback_url @callback_url end |
#hostname ⇒ Object
Returns the value of attribute hostname.
6 7 8 |
# File 'lib/casual.rb', line 6 def hostname @hostname end |
#https ⇒ Object
Returns the value of attribute https.
6 7 8 |
# File 'lib/casual.rb', line 6 def https @https end |
#path ⇒ Object
Returns the value of attribute path.
6 7 8 |
# File 'lib/casual.rb', line 6 def path @path end |
#port ⇒ Object
Returns the value of attribute port.
6 7 8 |
# File 'lib/casual.rb', line 6 def port @port end |
Instance Method Details
#acquire_ticket(login_page) ⇒ Object
37 38 39 40 |
# File 'lib/casual.rb', line 37 def acquire_ticket(login_page) ticket = Hpricot(login_page.body).search('input[@name=lt]').first ticket ? ticket['value'] : nil end |
#authenticate(username, password) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/casual.rb', line 20 def authenticate(username,password) login_page = connection.get("/#{no_slash_path}/login") headers = { 'Cookie' => login_page.response["set-cookie"] } ticket = acquire_ticket(login_page) params = "username=#{username}&password=#{password}<=#{ticket}" params << '&_eventId=submit&submit=LOGIN' status,response = connection.post("/#{no_slash_path}/login", params, headers) if response =~ /JA-SIG/ (response =~ /Log In Successful/) ? username : nil else status.code == '200' ? username : nil end end |
#authenticate_ticket(ticket) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/casual.rb', line 48 def authenticate_ticket(ticket) connection. get("/#{no_slash_path}/serviceValidate?service=#{callback_url}" + "&ticket=#{ticket}"). body end |
#authorization_url ⇒ Object
16 17 18 |
# File 'lib/casual.rb', line 16 def "#{server_url}/login?service=#{callback_url}" end |
#connection ⇒ Object
42 43 44 45 46 |
# File 'lib/casual.rb', line 42 def connection http = Net::HTTP.new(hostname, port) http.use_ssl = @https http end |
#no_slash_path ⇒ Object
69 70 71 |
# File 'lib/casual.rb', line 69 def no_slash_path path[0] == 47 ? path[1..path.size] : path end |
#protocol ⇒ Object
65 66 67 |
# File 'lib/casual.rb', line 65 def protocol https ? 'http' : 'https' end |
#server_url ⇒ Object
61 62 63 |
# File 'lib/casual.rb', line 61 def server_url "#{protocol}://#{hostname}:#{port}/#{no_slash_path}" end |
#user_login(ticket) ⇒ Object
55 56 57 58 59 |
# File 'lib/casual.rb', line 55 def user_login(ticket) user = Hpricot::XML(authenticate_ticket(ticket)). search('//cas:authenticationSuccess //cas:user').text user.strip != '' ? user : nil end |