Commit 7041431

mo khan <mo@mokhan.ca>
2014-07-25 22:41:27
move save method to storage and place in different file.
1 parent bbff9ef
lib/tfa/add_command.rb
@@ -6,8 +6,7 @@ module TFA
 
     def run(arguments)
       name = arguments.first
-      secret = arguments.last
-      @storage.save(name, secret)
+      @storage.save(arguments.first, arguments.last)
       "Added #{name}"
     end
   end
lib/tfa/show_command.rb
@@ -10,37 +10,3 @@ module TFA
     end
   end
 end
-
-module TFA
-  class Storage
-    def initialize(storage)
-      @storage = storage
-    end
-
-    def all_secrets
-      open_readonly do |storage|
-        storage.roots.map { |key| { key => storage[key] } }
-      end
-    end
-
-    def secret_for(key)
-      open_readonly do |storage|
-        storage[key]
-      end
-    end
-
-    def save(key, value)
-      @storage.transaction do
-        @storage[key] = value
-      end
-    end
-
-    private
-
-    def open_readonly
-      @storage.transaction(true) do
-        yield @storage
-      end
-    end
-  end
-end
lib/tfa/storage.rb
@@ -0,0 +1,33 @@
+module TFA
+  class Storage
+    def initialize(storage)
+      @storage = storage
+    end
+
+    def all_secrets
+      open_readonly do |storage|
+        storage.roots.map { |key| { key => storage[key] } }
+      end
+    end
+
+    def secret_for(key)
+      open_readonly do |storage|
+        storage[key]
+      end
+    end
+
+    def save(key, value)
+      @storage.transaction do
+        @storage[key] = value
+      end
+    end
+
+    private
+
+    def open_readonly
+      @storage.transaction(true) do
+        yield @storage
+      end
+    end
+  end
+end
lib/tfa/totp_command.rb
@@ -1,20 +1,11 @@
 module TFA
   class TotpCommand
     def initialize(storage)
-      @storage = storage
+      @storage = Storage.new(storage)
     end
 
     def run(arguments)
-      name = arguments.first
-      ::ROTP::TOTP.new(secret_for(name)).now
-    end
-
-    private
-
-    def secret_for(name)
-      @storage.transaction(true) do
-        @storage[name]
-      end
+      ::ROTP::TOTP.new(@storage.secret_for(arguments.first)).now
     end
   end
 end
lib/tfa.rb
@@ -6,3 +6,4 @@ require "tfa/show_command"
 require "tfa/totp_command"
 require "tfa/usage_command"
 require "tfa/console"
+require "tfa/storage"