lib/hackpad/cli/pad.rb in hackpad-cli-0.0.6 vs lib/hackpad/cli/pad.rb in hackpad-cli-0.0.7

- old
+ new

@@ -8,11 +8,11 @@ class UnknownFormat < StandardError end class Pad - attr_reader :id, :content, :guest_policy, :moderated + attr_reader :id, :content, :guest_policy, :moderated, :cached_at def initialize(id) @id = id end @@ -29,22 +29,37 @@ end def load(ext, refresh=false) raise UnknownFormat unless FORMATS.include? ext raise UndefinedPad unless @id - if refresh or !Store.exists? ext, id - @content = Api.read id, ext - Store.save self, ext - options = Api.read_options id - @guest_policy = options['guestPolicy'] - @moderated = !!options['isModerated'] - Store.save_meta @id, options + if refresh or !Store.exists? ext, @id + load_from_api ext else - @content = Store.read id, ext - options = Store.read_options id - @guest_policy = options['guestPolicy'] - @moderated = !!options['isModerated'] + load_from_cache ext end + end + + def load_from_api(ext) + @content = Api.read @id, ext + Store.save self, ext + options = Api.read_options @id + @guest_policy = options['options']['guestPolicy'] + @moderated = !!options['options']['isModerated'] + options['cached_at'] = Time.now + @cached_at = options['cached_at'] + Store.save_meta @id, options + end + + def load_from_cache(ext) + @content = Store.read @id, ext + options = Store.read_options @id + @guest_policy = options['options']['guestPolicy'] + @moderated = !!options['options']['isModerated'] + @cached_at = options['cached_at'] + end + + def is_cached? + Store.exists? 'meta', @id end end end end