Class: Cnvrg::Auth
- Inherits:
-
Object
- Object
- Cnvrg::Auth
- Defined in:
- lib/cnvrg/auth.rb
Instance Method Summary collapse
- #ask(message) ⇒ Object
- #ask_password(message) ⇒ Object
- #get_email ⇒ Object
- #get_token ⇒ Object
-
#is_logged_in? ⇒ Boolean
include Thor::Actions.
- #sign_in(email, password, token: nil) ⇒ Object
Instance Method Details
#ask(message) ⇒ Object
36 37 38 |
# File 'lib/cnvrg/auth.rb', line 36 def ask() HighLine.new.ask() end |
#ask_password(message) ⇒ Object
41 42 43 44 45 |
# File 'lib/cnvrg/auth.rb', line 41 def ask_password() HighLine.new.ask() do |q| q.echo = false end end |
#get_email ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/cnvrg/auth.rb', line 14 def get_email n = Netrc.read email, token = n[Cnvrg::Helpers.netrc_domain] if self.is_logged_in? return email else return nil end end |
#get_token ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/cnvrg/auth.rb', line 25 def get_token n = Netrc.read email, token = n[Cnvrg::Helpers.netrc_domain] if self.is_logged_in? return token else return nil end end |
#is_logged_in? ⇒ Boolean
include Thor::Actions
6 7 8 9 10 11 12 |
# File 'lib/cnvrg/auth.rb', line 6 def is_logged_in? n = Netrc.read return false if n[Cnvrg::Helpers.netrc_domain].nil? email, token = n[Cnvrg::Helpers.netrc_domain] not (email.empty? or token.empty?) end |
#sign_in(email, password, token: nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cnvrg/auth.rb', line 47 def sign_in(email, password, token: nil) url = Cnvrg::API.endpoint_uri() url = URI.parse(url+ "/users/sign_in") http = Net::HTTP.new(url.host, url.port) if url.scheme == 'https' http.use_ssl = true if !Helpers.is_verify_ssl http.verify_mode = OpenSSL::SSL::VERIFY_NONE end end req = Net::HTTP::Post.new(url.request_uri) req.add_field("EMAIL", email) req.add_field("PASSWORD", password) if token.present? req.add_field("Authorization", "CAPI #{token}") end response = http.request(req) result = JSON.parse(response.body) if Cnvrg::CLI.is_response_success(result) return result["result"] end end |