Class: Passcard::Reader

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/passcard/reader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, enc_text) ⇒ Reader

Returns a new instance of Reader.



15
16
17
18
19
# File 'lib/passcard/reader.rb', line 15

def initialize(key, enc_text)
  secret, enc_text = key.sha512, enc_text
  @opts = Passcard.decrypt!(secret, enc_text)
  get_grid
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &b) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/passcard/reader.rb', line 58

def method_missing(m, *a, &b)
  if Passcard.outputters.include?(m)
    klass, method_name = Passcard.outputters[m]
    klass.new(self).send(method_name, *a, &b)
  elsif @grid.respond_to?(m)
    @grid.send(m, *a, &b)
  else
    super
  end
end

Instance Attribute Details

#gridObject (readonly)

Returns the value of attribute grid.



13
14
15
# File 'lib/passcard/reader.rb', line 13

def grid
  @grid
end

#optsObject (readonly)

Returns the value of attribute opts.



13
14
15
# File 'lib/passcard/reader.rb', line 13

def opts
  @opts
end

Class Method Details

.read_key_file(key, path) ⇒ Object



8
9
10
11
# File 'lib/passcard/reader.rb', line 8

def self.read_key_file(key, path)
  data = File.readlines(path)
  self.new(key, data[1...-1].join.strip)
end

Instance Method Details

#alpha_gridObject



32
33
34
35
36
# File 'lib/passcard/reader.rb', line 32

def alpha_grid
  ir = @opts["size"][0] - @opts["alpha"][0]
  ic = @opts["size"][1] - @opts["alpha"][1]
  @grid.slice([ir, ic], @opts["size"])
end

#get_gridObject



21
22
23
24
25
26
# File 'lib/passcard/reader.rb', line 21

def get_grid
  return @grid if @grid
  @grid = @opts.delete("grid")
  @grid = @grid.chars.each_slice(@opts["size"][1]).to_a
  @grid = Passcard::Grid.new(@grid)
end

#numeric_gridObject



28
29
30
# File 'lib/passcard/reader.rb', line 28

def numeric_grid
  @grid.slice([0, 0], @opts["numeric"])
end

#random_grid(rows = 10, cols = 10) ⇒ Object

Should be taken from all corners rather than the first:



39
40
41
42
43
44
45
46
# File 'lib/passcard/reader.rb', line 39

def random_grid(rows = 10, cols = 10)
  return Passcard::Grid.new([]) if rows * cols == 0
  return @grid if rows * cols >= @grid.length

  ir = rand(@opts["size"][0] - rows - 1).to_i
  ic = rand(@opts["size"][1] - cols - 1).to_i
  @grid.slice([ir, ic], [ir + rows, ic + cols])
end

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/passcard/reader.rb', line 69

def respond_to?(*args)
  @grid.respond_to?(*args) || super
end

#to_hashObject



48
49
50
# File 'lib/passcard/reader.rb', line 48

def to_hash
  @opts.merge("grid" => @grid.to_str)
end

#to_s(*a, &b) ⇒ Object



52
53
54
55
56
# File 'lib/passcard/reader.rb', line 52

def to_s(*a, &b)
  return @grid.to_s unless Passcard.outputters.has_key?(:to_s)
  klass, method = Passcard.outputters[:to_s]
  klass.new(self).send(method, *a, &b) || @grid.to_s
end