Class: RubyLetmein
- Inherits:
-
Object
- Object
- RubyLetmein
- Defined in:
- lib/ruby_letmein.rb
Overview
Main class
Instance Attribute Summary collapse
-
#api_error ⇒ Object
Returns the value of attribute api_error.
-
#app_token ⇒ Object
readonly
Returns the value of attribute app_token.
-
#auth_token ⇒ Object
Returns the value of attribute auth_token.
-
#auth_token_expiry ⇒ Object
Returns the value of attribute auth_token_expiry.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #current_user ⇒ Object
-
#initialize(base_url, app_token) ⇒ RubyLetmein
constructor
A new instance of RubyLetmein.
- #refresh_current_user ⇒ Object
- #sign_in(email, password) ⇒ Object
- #sign_out ⇒ Object
- #signed_in? ⇒ Boolean
Constructor Details
#initialize(base_url, app_token) ⇒ RubyLetmein
Returns a new instance of RubyLetmein.
12 13 14 15 16 |
# File 'lib/ruby_letmein.rb', line 12 def initialize(base_url, app_token) URI.parse(base_url) @base_url = base_url @app_token = app_token end |
Instance Attribute Details
#api_error ⇒ Object
Returns the value of attribute api_error.
10 11 12 |
# File 'lib/ruby_letmein.rb', line 10 def api_error @api_error end |
#app_token ⇒ Object (readonly)
Returns the value of attribute app_token.
9 10 11 |
# File 'lib/ruby_letmein.rb', line 9 def app_token @app_token end |
#auth_token ⇒ Object
Returns the value of attribute auth_token.
10 11 12 |
# File 'lib/ruby_letmein.rb', line 10 def auth_token @auth_token end |
#auth_token_expiry ⇒ Object
Returns the value of attribute auth_token_expiry.
10 11 12 |
# File 'lib/ruby_letmein.rb', line 10 def auth_token_expiry @auth_token_expiry end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
9 10 11 |
# File 'lib/ruby_letmein.rb', line 9 def base_url @base_url end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
9 10 11 |
# File 'lib/ruby_letmein.rb', line 9 def uri @uri end |
Instance Method Details
#current_user ⇒ Object
60 61 62 |
# File 'lib/ruby_letmein.rb', line 60 def current_user @current_user ||= retrieve_current_user end |
#refresh_current_user ⇒ Object
64 65 66 67 |
# File 'lib/ruby_letmein.rb', line 64 def refresh_current_user @current_user = nil current_user end |
#sign_in(email, password) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ruby_letmein.rb', line 18 def sign_in(email, password) uri = URI.parse("#{base_url}/api/v1/app/session/sign_in") header = { 'Content-Type': 'application/json', AppToken: @app_token } http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.instance_of? URI::HTTPS request = Net::HTTP::Post.new(uri.request_uri, header) request.body = { email: email, password: password }.to_json response = http.request(request) return false unless success?(request, response) define_auth_token_and_expiry!(response) end |
#sign_out ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ruby_letmein.rb', line 33 def sign_out uri = URI.parse("#{base_url}/api/v1/app/session/sign_out") header = { 'Content-Type': 'application/json', AppToken: @app_token, Authorization: "Bearer #{@auth_token}" } http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.instance_of? URI::HTTPS request = Net::HTTP::Delete.new(uri.request_uri, header) response = http.request(request) return false unless success?(request, response) nullify_token_and_expiry true end |
#signed_in? ⇒ Boolean
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ruby_letmein.rb', line 48 def signed_in? uri = URI.parse("#{base_url}/api/v1/app/session/signed_in") header = { 'Content-Type': 'application/json', AppToken: @app_token, Authorization: "Bearer #{@auth_token}" } http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.instance_of? URI::HTTPS request = Net::HTTP::Get.new(uri.request_uri, header) response = http.request(request) success?(request, response) end |