Class: Phase::Keys::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/phase/kit/keys/key.rb

Constant Summary collapse

PATTERN_KEY_LINE =
/ssh-rsa/
PATTERN_ATTRS_LINE =
/\A### phase-key (.*)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Key

Returns a new instance of Key.



28
29
30
31
32
33
34
# File 'lib/phase/kit/keys/key.rb', line 28

def initialize(attrs = {})
  self.email      = attrs.fetch(:email)
  self.key        = attrs.fetch(:key, nil)
  self.status     = attrs.fetch(:status, "active")
  self.created_at = attrs.fetch(:created_at, Time.now)
  self.updated_at = attrs.fetch(:updated_at, Time.now)
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



4
5
6
# File 'lib/phase/kit/keys/key.rb', line 4

def created_at
  @created_at
end

#emailObject

Returns the value of attribute email.



4
5
6
# File 'lib/phase/kit/keys/key.rb', line 4

def email
  @email
end

#keyObject

Returns the value of attribute key.



4
5
6
# File 'lib/phase/kit/keys/key.rb', line 4

def key
  @key
end

#statusObject

Returns the value of attribute status.



4
5
6
# File 'lib/phase/kit/keys/key.rb', line 4

def status
  @status
end

#updated_atObject

Returns the value of attribute updated_at.



4
5
6
# File 'lib/phase/kit/keys/key.rb', line 4

def updated_at
  @updated_at
end

Class Method Details

.parse(attrs_line, key_line) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/phase/kit/keys/key.rb', line 9

def self.parse(attrs_line, key_line)
  if attrs_line.match(PATTERN_ATTRS_LINE)
    attrs = JSON.parse($1)

    new_key = new(
      email:      attrs["email"],
      status:     attrs["status"],
      created_at: attrs["created_at"],
      updated_at: attrs["updated_at"]
    )

    if key_line.match(PATTERN_KEY_LINE)
      new_key.key = key_line.chomp
    end

    new_key
  end
end

Instance Method Details

#activateObject



69
70
71
72
# File 'lib/phase/kit/keys/key.rb', line 69

def activate
  self.status = "active"
  self.key = uncomment(self.key)
end

#active?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/phase/kit/keys/key.rb', line 65

def active?
  self.status == "active"
end

#attributesObject



48
49
50
51
52
53
54
55
# File 'lib/phase/kit/keys/key.rb', line 48

def attributes
  {
    email:      self.email,
    status:     self.status,
    created_at: self.created_at,
    updated_at: self.updated_at
  }
end

#comment(key) ⇒ Object



79
# File 'lib/phase/kit/keys/key.rb', line 79

def comment(key); "# #{key}"; end

#deactivateObject



74
75
76
77
# File 'lib/phase/kit/keys/key.rb', line 74

def deactivate
  self.status = "inactive"
  self.key = comment(self.key)
end

#escape(text) ⇒ Object



86
87
88
# File 'lib/phase/kit/keys/key.rb', line 86

def escape(text)
  text.gsub("'", "\'")
end

#key_fragmentObject



44
45
46
# File 'lib/phase/kit/keys/key.rb', line 44

def key_fragment
  "..." + uncomment(self.key).split[1][-20..-1] if self.key
end

#to_sObject



82
83
84
# File 'lib/phase/kit/keys/key.rb', line 82

def to_s
  escape( ["", "### phase-key #{ ::JSON.dump(attributes) }", self.key, ""].join("\n") )
end

#touchObject



57
58
59
# File 'lib/phase/kit/keys/key.rb', line 57

def touch
  self.updated_at = Time.now
end

#uncomment(key) ⇒ Object



80
# File 'lib/phase/kit/keys/key.rb', line 80

def uncomment(key); self.key.sub(/\A# /, ""); end

#valid?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/phase/kit/keys/key.rb', line 61

def valid?
  !!self.email && !!self.key
end