Class: Rmega::Session
Instance Attribute Summary collapse
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#master_key ⇒ Object
readonly
Returns the value of attribute master_key.
-
#request_id ⇒ Object
readonly
Returns the value of attribute request_id.
-
#sid ⇒ Object
readonly
Returns the value of attribute sid.
Instance Method Summary collapse
-
#initialize(email, password) ⇒ Session
constructor
A new instance of Session.
- #login(password) ⇒ Object
- #options ⇒ Object
- #random_request_id ⇒ Object
- #request(body) ⇒ Object
- #request_url ⇒ Object
- #storage ⇒ Object
Methods included from Loggable
Constructor Details
#initialize(email, password) ⇒ Session
Returns a new instance of Session.
16 17 18 19 20 21 |
# File 'lib/rmega/session.rb', line 16 def initialize(email, password) @email = email @request_id = random_request_id login(password) end |
Instance Attribute Details
#email ⇒ Object (readonly)
Returns the value of attribute email.
14 15 16 |
# File 'lib/rmega/session.rb', line 14 def email @email end |
#master_key ⇒ Object (readonly)
Returns the value of attribute master_key.
14 15 16 |
# File 'lib/rmega/session.rb', line 14 def master_key @master_key end |
#request_id ⇒ Object (readonly)
Returns the value of attribute request_id.
14 15 16 |
# File 'lib/rmega/session.rb', line 14 def request_id @request_id end |
#sid ⇒ Object (readonly)
Returns the value of attribute sid.
14 15 16 |
# File 'lib/rmega/session.rb', line 14 def sid @sid end |
Instance Method Details
#login(password) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rmega/session.rb', line 33 def login(password) uh = Crypto.stringhash Crypto.prepare_key_pw(password), email.downcase resp = request(a: 'us', user: email, uh: uh) # Decrypts the master key encrypted_key = Crypto.prepare_key_pw(password) @master_key = Crypto.decrypt_key(encrypted_key, Utils.base64_to_a32(resp['k'])) # Generates the session id @sid = Crypto.decrypt_sid(@master_key, resp['csid'], resp['privk']) end |
#options ⇒ Object
23 24 25 |
# File 'lib/rmega/session.rb', line 23 def Rmega. end |
#random_request_id ⇒ Object
45 46 47 |
# File 'lib/rmega/session.rb', line 45 def random_request_id rand(1E7..1E9).to_i end |
#request(body) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/rmega/session.rb', line 55 def request(body) @request_id += 1 logger.debug "POST #{request_url}\n#{body.inspect}" response = HTTPClient.new.post(request_url, [body].to_json, timeout: api_request_timeout) logger.debug "#{response.code}\n#{response.body}" resp = JSON.parse(response.body).first raise RequestError.new(resp) if RequestError.error_code?(resp) resp end |
#request_url ⇒ Object
49 50 51 52 53 |
# File 'lib/rmega/session.rb', line 49 def request_url "#{api_url}?id=#{@request_id}".tap do |url| url << "&sid=#{@sid}" if @sid end end |