Class: YourMembership::Session
- Defined in:
- lib/your_membership/session.rb
Overview
It is important to note that the Auth Namespace has been consumed by Sessions in the SDK as sessions and authentication are inextricably linked.
YourMembership Session Object
Session objects encapsulate the creation, storage, authentication, maintenance, and destruction of sessions in the YourMembership.com API.
Sessions can be generic (unauthenticated), authenticated, or abandoned.
-
*Generic sessions* are used extensively whenever the scope of a specific user is not necessary.
-
*Authenticated sessions* are used when the called method requires the scope of a specific user.
-
*Abandoned sessions* are no longer usable and are essentially the same as logging out.
Instance Attribute Summary collapse
-
#call_id ⇒ Integer
readonly
Auto Increments ad returns the call_id for the session as required by the YourMembership.com API.
-
#session_id ⇒ String
readonly
The unique session identifier provided by the API.
-
#user_id ⇒ String, Nil
readonly
The user id of the user bound to the session, if one exists.
Class Method Summary collapse
Instance Method Summary collapse
-
#abandon ⇒ Boolean
Destroys an API session, thus preventing any further calls against it.
-
#authenticate(user_name, password) ⇒ Hash
Authenticates a member’s username and password and binds them to the current API session.
-
#authenticated? ⇒ Boolean
Indicates whether the session is bound to a user.
-
#createToken(options = {}) ⇒ Hash
Creates an AuthToken that is bound to the current session.
-
#get_authenticated_user ⇒ String, Nil
Get the ID of the currently authenticated user bound to this session.
-
#initialize(session_id = nil, call_id = 1, user_id = nil) ⇒ Session
constructor
Generates an empty session.
-
#ping ⇒ Boolean
When called at intervals of less than 20 minutes, this method acts as an API session keep-alive.
-
#to_s ⇒ String
Returns the session_id.
-
#valid? ⇒ Boolean
Convenience method for ping.
Methods inherited from Base
build_XML_request, new_call_id, post, response_to_array, response_to_array_of_hashes, response_valid?, response_ym_error?
Constructor Details
#initialize(session_id = nil, call_id = 1, user_id = nil) ⇒ Session
Generates an empty session
27 28 29 30 31 |
# File 'lib/your_membership/session.rb', line 27 def initialize(session_id = nil, call_id = 1, user_id = nil) @session_id = session_id @call_id = call_id @user_id = user_id end |
Instance Attribute Details
#call_id ⇒ Integer (readonly)
Returns Auto Increments ad returns the call_id for the session as required by the YourMembership.com API.
49 50 51 |
# File 'lib/your_membership/session.rb', line 49 def call_id @call_id end |
#session_id ⇒ String (readonly)
The unique session identifier provided by the API
23 24 25 |
# File 'lib/your_membership/session.rb', line 23 def session_id @session_id end |
#user_id ⇒ String, Nil (readonly)
The user id of the user bound to the session, if one exists.
23 24 25 |
# File 'lib/your_membership/session.rb', line 23 def user_id @user_id end |
Class Method Details
.create(user_name = nil, password = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/your_membership/session.rb', line 37 def self.create(user_name = nil, password = nil) response = post('/', :body => build_XML_request('Session.Create')) if response_valid? response session = new response['YourMembership_Response']['Session.Create']['SessionID'] end session.authenticate user_name, password if user_name session end |
Instance Method Details
#abandon ⇒ Boolean
Destroys an API session, thus preventing any further calls against it.
63 64 65 66 |
# File 'lib/your_membership/session.rb', line 63 def abandon response = self.class.post('/', :body => self.class.build_XML_request('Session.Abandon', self)) self.class.response_valid? response end |
#authenticate(user_name, password) ⇒ Hash
Authenticates a member’s username and password and binds them to the current API session.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/your_membership/session.rb', line 94 def authenticate(user_name, password) = {} [:Username] = user_name [:Password] = password response = self.class.post('/', :body => self.class.build_XML_request('Auth.Authenticate', self, )) self.class.response_valid? response if response['YourMembership_Response']['Auth.Authenticate'] get_authenticated_user else false end end |
#authenticated? ⇒ Boolean
Indicates whether the session is bound to a user.
138 139 140 |
# File 'lib/your_membership/session.rb', line 138 def authenticated? valid? && !get_authenticated_user.nil? end |
#createToken(options = {}) ⇒ Hash
Creates an AuthToken that is bound to the current session. The returned token must be supplied to the Sign-In form during member authentication in order to bind a member to their API session. The sign-in URL, which will include the new AuthToken in its query-string, is returned by this method as GoToUrl. Tokens expire after a short period of time, so it is suggested that the user be immediately redirected the returned GoToUrl after creating an authentication token.
127 128 129 130 131 132 133 134 135 |
# File 'lib/your_membership/session.rb', line 127 def createToken( = {}) # rubocop:disable Style/MethodName # Options inlclude: :RetUrl(String), :Username(String), # :Password(String), :Persist(Boolean) response = self.class.post('/', :body => self.class.build_XML_request('Auth.CreateToken', self, )) self.class.response_valid? response response['YourMembership_Response']['Auth.CreateToken'] end |
#get_authenticated_user ⇒ String, Nil
Get the ID of the currently authenticated user bound to this session.
144 145 146 |
# File 'lib/your_membership/session.rb', line 144 def get_authenticated_user # rubocop:disable Style/AccessorMethodName @user_id = YourMembership::Member.isAuthenticated(self) end |
#ping ⇒ Boolean
When called at intervals of less than 20 minutes, this method acts as an API session keep-alive.
73 74 75 76 77 |
# File 'lib/your_membership/session.rb', line 73 def ping response = self.class.post('/', :body => self.class.build_XML_request('Session.Ping', self)) self.class.response_valid? response response['YourMembership_Response']['Session.Ping'] == '1' end |
#to_s ⇒ String
Returns the session_id
54 55 56 |
# File 'lib/your_membership/session.rb', line 54 def to_s @session_id end |
#valid? ⇒ Boolean
Convenience method for ping.
80 81 82 |
# File 'lib/your_membership/session.rb', line 80 def valid? ping end |