Class: Exos::Commands::Keys::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/exos/commands/keys.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Key

Returns a new instance of Key.



181
182
183
184
185
186
187
# File 'lib/exos/commands/keys.rb', line 181

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.



160
161
162
# File 'lib/exos/commands/keys.rb', line 160

def created_at
  @created_at
end

#emailObject

Returns the value of attribute email.



160
161
162
# File 'lib/exos/commands/keys.rb', line 160

def email
  @email
end

#keyObject

Returns the value of attribute key.



160
161
162
# File 'lib/exos/commands/keys.rb', line 160

def key
  @key
end

#statusObject

Returns the value of attribute status.



160
161
162
# File 'lib/exos/commands/keys.rb', line 160

def status
  @status
end

#updated_atObject

Returns the value of attribute updated_at.



160
161
162
# File 'lib/exos/commands/keys.rb', line 160

def updated_at
  @updated_at
end

Class Method Details

.parse(attrs_line, key_line) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/exos/commands/keys.rb', line 162

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



222
223
224
225
# File 'lib/exos/commands/keys.rb', line 222

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

#active?Boolean

Returns:

  • (Boolean)


218
219
220
# File 'lib/exos/commands/keys.rb', line 218

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

#attributesObject



201
202
203
204
205
206
207
208
# File 'lib/exos/commands/keys.rb', line 201

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

#comment(key) ⇒ Object



232
# File 'lib/exos/commands/keys.rb', line 232

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

#deactivateObject



227
228
229
230
# File 'lib/exos/commands/keys.rb', line 227

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

#escape(text) ⇒ Object



239
240
241
# File 'lib/exos/commands/keys.rb', line 239

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

#key_fragmentObject



197
198
199
# File 'lib/exos/commands/keys.rb', line 197

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

#to_sObject



235
236
237
# File 'lib/exos/commands/keys.rb', line 235

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

#touchObject



210
211
212
# File 'lib/exos/commands/keys.rb', line 210

def touch
  self.updated_at = Time.now
end

#uncomment(key) ⇒ Object



233
# File 'lib/exos/commands/keys.rb', line 233

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

#valid?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/exos/commands/keys.rb', line 214

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