Module: Pray::Session
- Defined in:
- lib/pray/session.rb
Class Method Summary collapse
- .current_signer(root) ⇒ Object
- .current_signer_fingerprint(root) ⇒ Object
- .load_latest(root) ⇒ Object
- .load_sessions(path) ⇒ Object
- .persist(root, session) ⇒ Object
- .session_file_path(root) ⇒ Object
- .session_from_hash(entry) ⇒ Object
- .session_to_hash(session) ⇒ Object
Class Method Details
.current_signer(root) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/pray/session.rb', line 46 def current_signer(root) session = load_latest(root) return session.email if session && !session.email.to_s.strip.empty? "local" end |
.current_signer_fingerprint(root) ⇒ Object
53 54 55 56 57 |
# File 'lib/pray/session.rb', line 53 def current_signer_fingerprint(root) session = load_latest(root) fingerprint = session&.signer_fingerprint.to_s.strip fingerprint.empty? ? nil : fingerprint end |
.load_latest(root) ⇒ Object
41 42 43 44 |
# File 'lib/pray/session.rb', line 41 def load_latest(root) sessions = load_sessions(session_file_path(root)) sessions.reverse.find { |session| !session.email.to_s.strip.empty? } end |
.load_sessions(path) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/pray/session.rb', line 59 def load_sessions(path) return [] unless File.file?(path) data = JSON.parse(File.read(path)) entries = (data.is_a?(Hash) && data.key?("sessions")) ? data["sessions"] : [data] Array(entries).map { |entry| session_from_hash(entry) } rescue JSON::ParserError => error raise Error.parse("session file", error.) end |
.persist(root, session) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/pray/session.rb', line 19 def persist(root, session) path = session_file_path(root) FileUtils.mkdir_p(File.dirname(path)) sessions = load_sessions(path) existing = sessions.find { |entry| entry.server_url == session.server_url } if existing existing.email = session.email existing.token = session.token existing.kind = session.kind existing.signer_fingerprint = session.signer_fingerprint else sessions << session end document = if sessions.length == 1 session_to_hash(sessions.first) else {"sessions" => sessions.map { |entry| session_to_hash(entry) }} end File.write(path, JSON.pretty_generate(document)) session end |
.session_file_path(root) ⇒ Object
15 16 17 |
# File 'lib/pray/session.rb', line 15 def session_file_path(root) File.join(root, ".pray", "session.json") end |
.session_from_hash(entry) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/pray/session.rb', line 80 def session_from_hash(entry) SessionFile.new( server_url: entry["server_url"], email: entry["email"], token: entry["token"], kind: entry["kind"], signer_fingerprint: entry["signer_fingerprint"] ) end |
.session_to_hash(session) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/pray/session.rb', line 69 def session_to_hash(session) hash = { "server_url" => session.server_url, "email" => session.email, "token" => session.token, "kind" => session.kind } hash["signer_fingerprint"] = session.signer_fingerprint if session.signer_fingerprint hash end |