module Ayadn class Set < Thor desc "scroll ITEM VALUE", "Set the waiting time (in seconds, min 0.7) between two requests when scrolling" def scroll(*args) scroll_config = SetScroll.new if args[0] scroll_config.send(args[0], args[1]) else Status.new.error_missing_parameters exit end scroll_config.save end desc "marker ITEM TRUE/FALSE", "Set values for stream markers" map "markers" => :marker def marker(*args) marker_config = SetMarker.new unless args.length != 2 marker_config.send(args[0], args[1]) else Status.new.error_missing_parameters exit end marker_config.save end desc "channels ITEM TRUE/FALSE", "Set values for stream markers" map "channel" => :channels def channels(*args) channels_config = SetChannels.new unless args.length != 2 channels_config.send(args[0], args[1]) else Status.new.error_missing_parameters exit end channels_config.save end desc "blacklist ITEM TRUE/FALSE", "Set values for the blacklist" def blacklist(*args) blacklist_config = SetBlacklist.new unless args.length != 2 blacklist_config.send(args[0], args[1]) else Status.new.error_missing_parameters exit end blacklist_config.save end desc "nicerank ITEM VALUE", "Set NiceRank filter values" long_desc Descriptions.set_nicerank def nicerank *args nicerank_config = SetNiceRank.new if args[0] nicerank_config.send(args[0], args[1]) else Status.new.error_missing_parameters exit end nicerank_config.save end desc "timeline ITEM TRUE/FALSE", "Set ITEM to be activated or not" long_desc Descriptions.set_timeline def timeline(*args) timeline_config = SetTimeline.new if args[0] begin timeline_config.send(args[0], args[1]) rescue NoMethodError, ArgumentError Status.new.error_missing_parameters exit rescue => e raise e end else Status.new.error_missing_parameters exit end timeline_config.save end desc "count ITEM NUMBER", "Set ITEM to retrieve NUMBER of elements by default" long_desc Descriptions.set_counts map "counts" => :count def count(*args) counts_config = SetCounts.new if args[0] begin counts_config.send(args[0], args[1]) rescue NoMethodError, ArgumentError Status.new.error_missing_parameters exit rescue => e raise e end else Status.new.error_missing_parameters exit end counts_config.save end desc "edit", "Edit settings in $EDITOR" def edit Settings.load_config() sets = Settings.config.paths.config + "/config.yml" if sets.blank? Status.new.not_authorized exit end ed = `echo $EDITOR` if ed.blank? Status.new.info "No predefined EDITOR, opening in VIM" `vim #{sets}` exit end `$EDITOR #{sets}` end desc "color ITEM COLOR", "Set ITEM to COLOR" long_desc Descriptions.set_color map "colors" => :color map "colour" => :color map "colours" => :color def color(*args) color_config = SetColor.new if args[0] begin color_config.send(args[0], args[1]) rescue NoMethodError, ArgumentError Status.new.error_missing_parameters exit rescue => e raise e end else Status.new.error_missing_parameters exit end color_config.save end desc "backup ITEM TRUE/FALSE", "Set ITEM to be activated or not" long_desc Descriptions.set_backup def backup(*args) backup_config = SetBackup.new if args[0] begin backup_config.send(args[0], args[1]) rescue NoMethodError, ArgumentError Status.new.error_missing_parameters exit rescue => e raise e end else Status.new.error_missing_parameters exit end backup_config.save end desc "defaults", "Sets back the configuration to defaults values" long_desc Descriptions.set_defaults def defaults Settings.restore_defaults Status.new.done end desc "formats ITEM VALUE", "Set values for formatting fields" def formats(*args) formats_config = SetFormats.new if args[0] begin command = args.shift formats_config.send(command, args) rescue NoMethodError, ArgumentError Status.new.error_missing_parameters exit rescue => e raise e end else Status.new.error_missing_parameters exit end formats_config.save end desc "api URL", "Set an alternative base URL for the API calls." def api(*args) if args[0] begin SetAPI.new.setURL(args[0]) rescue NoMethodError, ArgumentError => e Status.new.error_missing_parameters exit rescue => e raise e end else Status.new.error_missing_parameters exit end end end class Validators def self.URL(str) require 'net/http' if(str.to_s.empty?) Status.new.error_missing_parameters exit end begin URI.parse(str) # if url.host.nil? || (url.scheme != 'http' && url.scheme != 'https') # ask: are you sure about this url? # end # url rescue URI::Error => e return nil end end def self.boolean(value) case value.downcase when "true", "1", "yes" true when "false", "0", "no" false else Status.new.error_missing_parameters exit end end def self.index_range(min, max, value) x = value.to_i if x >= min && x <= max x else Status.new.must_be_integer end end def self.width_range value if value >= 60 && value <= 90 value.round else 75 end end def self.threshold value value = value.to_f if value > 0 and value < 5 value else Status.new.threshold end end def self.timer(t) t = t.to_f.round t >= 1 ? t : 3 end def self.color(color) colors_list = %w{red green magenta cyan yellow blue white black} unless colors_list.include?(color.to_s) Status.new.error_missing_parameters Status.new.valid_colors(colors_list) exit else return color.to_sym end end end class SetBase attr_accessor :input, :output, :category def initialize Settings.load_config() Settings.get_token() Settings.init_config() Logs.create_logger() @status = Status.new end def save Settings.save_config() log() end def log @status.say do @status.say_cyan :updated, "'#{@input}' in '#{@category}'" @status.say_green :content, "'#{@output}'" end Logs.rec.info "new value for '#{@input}' in '#{@category}' => '#{@output}'" end end class SetAPI < SetBase def initialize super @category = 'API' @status = Status.new end def setURL(url) @input = url # @status.say_header "checking URL validity" url = Validators.URL(url) if url != nil @output = url.to_s @status.say_info "setting up configuration" File.write(Dir.home + "/ayadn/.api.yml", {root: @output}.to_yaml) log() else @status.say_red :canceled, "URL is invalid" exit end end end class SetFormats < SetBase def initialize super @category = 'formats' end def table(args) type = args.shift.downcase if type == 'width' value = args[0].to_i @input = 'table width' @output = Validators.width_range(value) Settings.options.formats.table.width = @output elsif type == 'borders' || type == 'border' value = args[0] @input = 'table borders' @output = Validators.boolean(value) Settings.options.formats.table.borders = @output else @status.error_missing_parameters exit end end def tables(args) table(args) end def list(args) type = args.shift.downcase value = args[0] if type == 'reverse' || type == 'reversed' @input = 'list reverse' @output = Validators.boolean(value) Settings.options.formats.list.reverse = @output else @status.error_missing_parameters exit end end def lists(args) list(args) end end class SetScroll < SetBase def initialize super @category = 'scroll' end def validate(t) Validators.timer(t) end def timer(t) @input = 'timer' @output = validate(t) Settings.options.scroll.timer = @output end def date(value) @input = 'date' @output = Validators.boolean(value) Settings.options.scroll.date = @output end def spinner(value) @input = 'spinner' @output = Validators.boolean(value) Settings.options.scroll.spinner = @output end end class SetNiceRank < SetBase def initialize super @category = 'nicerank' end def filter value @input = 'filter' @output = Validators.boolean(value) Settings.options.nicerank.filter = @output end def active value filter(value) end def unranked value @input = 'unranked' @output = Validators.boolean(value) Settings.options.nicerank.unranked = @output end def threshold value @input = 'threshold' @output = Validators.threshold(value) Settings.options.nicerank.threshold = @output end end class SetBackup < SetBase def initialize super @category = 'backup' end def validate(value) Validators.boolean(value) end def method_missing(meth, options) @input = meth.to_s @output = validate(options) case @input when 'posts' Settings.options.backup.posts = @output when 'messages' Settings.options.backup.messages = @output when 'lists' Settings.options.backup.lists = @output else super end end end class SetMarker < SetBase def initialize super @category = 'marker' end def validate(value) Validators.boolean(value) end def method_missing(meth, options) @input = meth.to_s @output = validate(options) case @input when 'messages' Settings.options.marker.messages = @output else super end end end class SetChannels < SetBase def initialize super @category = 'channels' end def validate(value) Validators.boolean(value) end def method_missing(meth, options) @input = meth.to_s @output = validate(options) case @input when 'links' Settings.options.channels.links = @output else super end end end class SetBlacklist < SetBase def initialize super @category = 'blacklist' end def validate(value) Validators.boolean(value) end def method_missing(meth, options) @input = meth.to_s @output = validate(options) case @input when 'active', 'activated' Settings.options.blacklist.active = @output else super end end end class SetCounts < SetBase def initialize super @category = 'counts' end def validate(value) Validators.index_range(1, 200, value) end def method_missing(meth, options) @input = meth.to_s.capitalize @output = validate(options.to_i) case meth.to_s when 'default' Settings.options.counts.default = @output when 'unified' Settings.options.counts.unified = @output when 'checkins' Settings.options.counts.checkins = @output when 'conversations' Settings.options.counts.conversations = @output when 'global' Settings.options.counts.global = @output when 'photos' Settings.options.counts.photos = @output when 'trending' Settings.options.counts.trending = @output when 'mentions' Settings.options.counts.mentions = @output when 'convo' Settings.options.counts.convo = @output when 'posts' Settings.options.counts.posts = @output when 'messages' Settings.options.counts.messages = @output when 'search' Settings.options.counts.search = @output when 'whoreposted' Settings.options.counts.whoreposted = @output when 'whostarred' Settings.options.counts.whostarred = @output when 'whatstarred' Settings.options.counts.whatstarred = @output when 'files' Settings.options.counts.files = @output else super end end end class SetTimeline < SetBase def initialize super @category = 'timeline' end def validate(value) Validators.boolean(value) end def method_missing(meth, options) @input = meth.to_s @output = validate(options) case @input when 'directed' Settings.options.timeline.directed = @output when 'source' Settings.options.timeline.source = @output when 'symbols' Settings.options.timeline.symbols = @output when 'name' Settings.options.timeline.name = @output when 'date' Settings.options.timeline.date = @output when 'debug' Settings.options.timeline.debug = @output when 'compact' Settings.options.timeline.compact = @output end end end class SetColor < SetBase def initialize super @category = 'colors' end def validate(color) Validators.color(color) end def method_missing(meth, options) @input = meth.to_s.capitalize @output = validate(options) case meth.to_s when 'id' Settings.options.colors.id = @output when 'index' Settings.options.colors.index = @output when 'username' Settings.options.colors.username = @output when 'name' Settings.options.colors.name = @output when 'date' Settings.options.colors.date = @output when 'link' Settings.options.colors.link = @output when 'dots' Settings.options.colors.dots = @output when 'mentions', 'mention' Settings.options.colors.mentions = @output when 'symbols', 'symbol' Settings.options.colors.symbols = @output when 'unread' Settings.options.colors.unread = @output when 'debug' Settings.options.colors.debug = @output when 'excerpt' Settings.options.colors.excerpt = @output when 'hashtag', 'tag', 'hashtags' Settings.options.colors.hashtag = @output when 'client', 'source' Settings.options.colors.source = @output else super end end end end