lib/ayadn/scroll.rb in ayadn-1.0.13 vs lib/ayadn/scroll.rb in ayadn-1.1.0

- old
+ new

@@ -5,48 +5,32 @@ def initialize(api, view) @api = api @view = view end - def global(options) - options = check_raw(options) - loop do - begin - stream = @api.get_global(options) - if Databases.has_new?(stream, 'global') - show(stream, options) - end - unless stream['meta']['max_id'].nil? - Databases.save_max_id(stream) - options = options_hash(stream) - end - sleep Settings.options[:scroll][:timer] - rescue Interrupt - canceled - rescue => e - raise e - end + def method_missing(meth, options) + case meth.to_s + when 'trending', 'photos', 'checkins', 'replies', 'global', 'unified' + scroll_it(meth.to_s, options) + else + super end end - def unified(options) + def scroll_it(target, options) options = check_raw(options) + orig_target = target loop do begin - stream = @api.get_unified(options) - if Databases.has_new?(stream, 'unified') - show(stream, options) - end - unless stream['meta']['max_id'].nil? #check if there isn't lost posts - Databases.save_max_id(stream) - options = options_hash(stream) - end - sleep Settings.options[:scroll][:timer] + stream = get(target, options) + target = "explore:#{target}" if explore?(target) + show_if_new(stream, options, target) + target = orig_target if target =~ /explore/ + options = save_then_return(stream, options) + pause rescue Interrupt canceled - rescue => e - raise e end end end def mentions(username, options) @@ -54,22 +38,15 @@ user = @api.get_user(username) id = user['data']['id'] loop do begin stream = @api.get_mentions(username, options) - if Databases.has_new?(stream, "mentions:#{id}") - show(stream, options) - end - unless stream['meta']['max_id'].nil? - Databases.save_max_id(stream) - options = options_hash(stream) - end - sleep Settings.options[:scroll][:timer] + show_if_new(stream, options, "mentions:#{id}") + options = save_then_return(stream, options) + pause rescue Interrupt canceled - rescue => e - raise e end end end def posts(username, options) @@ -77,152 +54,89 @@ user = @api.get_user(username) id = user['data']['id'] loop do begin stream = @api.get_posts(username, options) - if Databases.has_new?(stream, "posts:#{id}") - show(stream, options) - end - unless stream['meta']['max_id'].nil? - Databases.save_max_id(stream) - options = options_hash(stream) - end - sleep Settings.options[:scroll][:timer] + show_if_new(stream, options, "posts:#{id}") + options = save_then_return(stream, options) + pause rescue Interrupt canceled - rescue => e - raise e end end end def convo(post_id, options) options = check_raw(options) loop do begin stream = @api.get_convo(post_id, options) - if Databases.has_new?(stream, "replies:#{post_id}") - show(stream, options) - end - unless stream['meta']['max_id'].nil? - Databases.save_max_id(stream) - options = options_hash(stream) - end - sleep Settings.options[:scroll][:timer] + show_if_new(stream, options, "replies:#{post_id}") + options = save_then_return(stream, options) + pause rescue Interrupt canceled - rescue => e - raise e end end end - def conversations(options) + def messages(channel_id, options) options = check_raw(options) loop do begin - stream = @api.get_conversations(options) - if Databases.has_new?(stream, 'explore:replies') - show(stream, options) - end - unless stream['meta']['max_id'].nil? - Databases.save_max_id(stream) - options = options_hash(stream) - end - sleep Settings.options[:scroll][:timer] + stream = @api.get_messages(channel_id, options) + show_if_new(stream, options, "channel:#{channel_id}") + options = save_then_return(stream, options) + pause rescue Interrupt canceled - rescue => e - raise e end end end - def trending(options) - options = check_raw(options) - loop do - begin - stream = @api.get_trending(options) - if Databases.has_new?(stream, 'explore:trending') - show(stream, options) - end - unless stream['meta']['max_id'].nil? - Databases.save_max_id(stream) - options = options_hash(stream) - end - sleep Settings.options[:scroll][:timer] - rescue Interrupt - canceled - rescue => e - raise e - end + private + + def get(target, options) + case target + when 'global' + @api.get_global(options) + when 'unified' + @api.get_unified(options) + when 'trending' + @api.get_trending(options) + when 'photos' + @api.get_photos(options) + when 'checkins' + @api.get_checkins(options) + when 'replies' + @api.get_conversations(options) end end - def checkins(options) - options = check_raw(options) - loop do - begin - stream = @api.get_checkins(options) - if Databases.has_new?(stream, 'explore:checkins') - show(stream, options) - end - unless stream['meta']['max_id'].nil? - Databases.save_max_id(stream) - options = options_hash(stream) - end - sleep Settings.options[:scroll][:timer] - rescue Interrupt - canceled - rescue => e - raise e - end + def explore?(target) + case target + when 'trending', 'photos', 'checkins', 'replies' + true + else + false end end - def photos(options) - options = check_raw(options) - loop do - begin - stream = @api.get_photos(options) - if Databases.has_new?(stream, 'explore:photos') - show(stream, options) - end - unless stream['meta']['max_id'].nil? - Databases.save_max_id(stream) - options = options_hash(stream) - end - sleep Settings.options[:scroll][:timer] - rescue Interrupt - canceled - rescue => e - raise e - end - end + def pause + sleep Settings.options[:scroll][:timer] end - def messages(channel_id, options) - options = check_raw(options) - loop do - begin - stream = @api.get_messages(channel_id, options) - if Databases.has_new?(stream, "channel:#{channel_id}") - show(stream, options) - end - unless stream['meta']['max_id'].nil? - Databases.save_max_id(stream) - options = options_hash(stream) - end - sleep Settings.options[:scroll][:timer] - rescue Interrupt - canceled - rescue => e - raise e - end - end + def show_if_new(stream, options, target) + show(stream, options) if Databases.has_new?(stream, target) end - private + def save_then_return(stream, options) + unless stream['meta']['max_id'].nil? + Databases.save_max_id(stream) + return options_hash(stream) + end + options + end def check_raw(options) if options[:raw] {count: 200, since_id: nil, raw: true, scroll: true} else