Class: WindowsLiveAdmin

Inherits:
Object
  • Object
show all
Includes:
Templates
Defined in:
lib/windows_live_admin.rb

Overview

This creates an windows admin live account instance. The object instantiated will have various member/user/email manipulation methods.

Author:

  • Shivam Patel

Instance Method Summary collapse

Methods included from Templates

#create_member_xml, #delete_member_xml, #get_login_data_template_xml, #get_login_url_xml, #verify_auth_data_xml

Constructor Details

#initialize(username, password) ⇒ WindowsLiveAdmin

Constructor of the WindowsLiveAdmin class. Inits class variables @username and @password and makes HTTP calls to get login url and login template. Thereafter obtains auth_data by get_auth_data call



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

def initialize(username, password)
  @username = username
  @password = password

  begin
   = 
   = 
  @auth_data = get_auth_data(, )
  rescue
    @auth_data = nil   # nil auth_data signifies there was an error in the login process
  end
end

Instance Method Details

#create_member(member_name, password, reset_password, first_name, last_name, lcid = nil) ⇒ Array

This method will add a member to the specified domain and provision an email account The member name is a email id of the member. Note that the corresponding domain should be already manually added and configured in the windows admin live center by logging on to the web interface.

Parameters:

  • member_name (String)

    the complete email id of the member. eg. [email protected] Note that this domain should be already added and activated via the web interface at domains.live.com

  • password (String)

    the desired password for the user/member.

  • reset_password (Boolean)

    a true/false value whether we want member to be forced to change her password on first login.

  • first_name (String)

    first name of member

  • last_name (String)

    last name of member

  • lcid (Sting) (defaults to: nil)

    member’s locale ID. Can be blank or nil

Returns:

  • (Array)

    An [status, message] array where status is a true/false boolean indicating if create_member was successful or not. message contains relevant error message if the status is false.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/windows_live_admin.rb', line 100

def create_member(member_name, password, reset_password, first_name, last_name, lcid=nil)
  http, request = get_request_headers("http://domains.live.com/Service/ManageDomain/V1.0/CreateMember")

  request.body = create_member_xml(member_name, password, reset_password, first_name, last_name, lcid)
  #puts request.body
  response = http.request(request)
  # puts response.body

  error = is_error?(response.body) # if response is an error xml, error string describes the error

  if error # Response XML had in error node called ErrorDescription
    return [false, error]
  else                 # Absence of ErrorDescription node is taken as success
    return [true, "Member #{first_name} #{last_name} (#{member_name}) successfully created."]
  end

end

#delete_member(member_name) ⇒ Array

This method will delete a member given her member_name (email id)

Parameters:

  • member_name (String)

    email id of the member

Returns:

  • (Array)

    An [status, message] array. status is the true/false boolean indicating if delete_member was successful or not. message contains relevant error message if the status if false.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/windows_live_admin.rb', line 122

def delete_member(member_name)
  http, request = get_request_headers("http://domains.live.com/Service/ManageDomain/V1.0/DeleteMember")

  request.body = delete_member_xml(member_name)
  #puts request.body
  response = http.request(request)
  # puts response.body

  error = is_error?(response.body) # if response is an error, error is a string describing the error

  if error # Response XML had in error node called ErrorDescription
    return [false, error]
  else                 # Absence of ErrorDescription node is taken as success
    return [true, "Member #{member_name} successfully deleted."]
  end

end

#get_auth_data(login_url, login_template) ⇒ String

Fetches auth data by posting the login_template (got from call to get_login_template) to login_url (got from call to get_login_url)

Parameters:

  • login_url (String)

    The login_url obtained from a previous call to get_login_url

  • login_template (String)

    The login_template obtained from a previous call to get_login_template

Returns:

  • (String)

    The auth_data received from server.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/windows_live_admin.rb', line 57

def get_auth_data(, )
  uri = URI.parse()
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new()

  request.body = 
  response = http.request(request)
  @auth_data =  REXML::XPath.first(REXML::Document.new(response.body), "//Redirect").text
  @auth_data.gsub!(/&/, "&") # rexml converts & to &. Convert them back to &
end

#get_login_data_templateString

Fetches the login template from the server and replaces it with admin’s username and password

Returns:

  • (String)

    login template with admin username and password filled in.



41
42
43
44
45
46
47
48
49
50
# File 'lib/windows_live_admin.rb', line 41

def 
  http, request = get_request_headers("http://domains.live.com/Service/ManageDomain/V1.0/GetLoginDataTemplate")
  request.body = 
   #puts @request.inspect
  response = http.request(request)
  # puts response.body
  template =  REXML::XPath.first(REXML::Document.new(response.body), "//GetLoginDataTemplateResult").text
  template.gsub!(/%NAME%/, @username)
  template.gsub!(/%PASSWORD%/, @password)
end

#get_login_urlString

Fetches the login URl where the template needs to be posted to obtain the auth_data (token)

Returns:

  • (String)

    the login url obtained from the server



30
31
32
33
34
35
36
37
# File 'lib/windows_live_admin.rb', line 30

def 
  http, request = get_request_headers('http://domains.live.com/Service/ManageDomain/V1.0/GetLoginUrl')
  request.body = 
  #puts @request.body
  response = http.request(request)
   =  REXML::XPath.first(REXML::Document.new(response.body), "//GetLoginUrlResult").text

end

#verify_auth_dataString

Verifies if the auth_data obtained from the previous calls to get_auth_data is valid

Returns:

  • (String)

    returns true if auth_data is valid, nil otherwise



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/windows_live_admin.rb', line 71

def verify_auth_data
  http, request = get_request_headers("http://domains.live.com/Service/ManageDomain/V1.0/VerifyAuthData")
  request.body = verify_auth_data_xml

  # puts request.body
  response = http.request(request)
  # puts response.body
  begin
    authorized = REXML::XPath.first(REXML::Document.new(response.body), "//VerifyAuthDataResult").text
  end
  return authorized
end