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

- old
+ new

@@ -1,7 +1,7 @@ -require_relative "store" -require_relative "api" +require_relative 'store' +require_relative 'api' module Hackpad module Cli class UndefinedPad < StandardError end @@ -26,42 +26,41 @@ def lines @content.lines.count if @content end - def load(ext, refresh=false) - raise UnknownFormat unless FORMATS.include? ext - raise UndefinedPad unless @id - if refresh or !Store.exists? ext, @id - load_from_api ext + def load(ext, refresh = false, save = true) + fail UnknownFormat unless FORMATS.include? ext + fail UndefinedPad unless @id + if refresh || !Store.exists?(ext, @id) + load_from_api ext, save else load_from_cache ext end end - def load_from_api(ext) + def load_from_api(ext, dosave = true) @content = Api.read @id, ext - Store.save self, ext + dosave && Store.save(self, ext) options = Api.read_options @id @guest_policy = options['options']['guestPolicy'] - @moderated = !!options['options']['isModerated'] + @moderated = options['options']['isModerated'] options['cached_at'] = Time.now @cached_at = options['cached_at'] - Store.save_meta @id, options + dosave && Store.save_options(@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'] + @moderated = options['options']['isModerated'] @cached_at = options['cached_at'] end - def is_cached? + def cached? Store.exists? 'meta', @id end end end end -