Class: FeedInto::Group
Constant Summary collapse
- GROUP =
{ meta: { timestamp: nil }, validation: { allows: [ :sleep__range, :sleep__codes, :sleep__varieties, :sleep__scores__ok__name, :sleep__scores__ok__value, :sleep__scores__user__name, :sleep__scores__user__value, :sleep__scores__server__name, :sleep__scores__server__value, :sleep__scores__other__name, :sleep__scores__other__value, :sleep__stages ], wildcards: [] }, sleep: { range: 15, codes: [ { status: 0, name: 'other', add: :user }, { status: 100, name: 'continue', add: :server }, { status: 101, name: 'switching_protocols', add: :server }, { status: 200, name: 'ok', add: :ok }, { status: 201, name: 'created', add: :ok }, { status: 202, name: 'accepted', add: :ok }, { status: 203, name: 'non_authoritative_information', add: :ok }, { status: 204, name: 'no_content', add: :ok }, { status: 205, name: 'reset_content', add: :ok }, { status: 206, name: 'partial_content', add: :ok }, { status: 207, name: 'multi_status', add: :ok }, { status: 208, name: 'already_reported', add: :ok }, { status: 226, name: 'im_used', add: :ok }, { status: 300, name: 'multiple_choices', add: :user }, { status: 301, name: 'moved_permanently', add: :user }, { status: 302, name: 'found', add: :user }, { status: 303, name: 'see_other', add: :user }, { status: 304, name: 'not_modified', add: :user }, { status: 305, name: 'use_proxy', add: :user }, { status: 306, name: 'switch_proxy', add: :user }, { status: 307, name: 'temporary_redirect', add: :user }, { status: 308, name: 'permanent_redirect', add: :user }, { status: 400, name: 'bad_request', add: :user }, { status: 401, name: 'unauthorized', add: :server }, { status: 402, name: 'payment_required', add: :server }, { status: 403, name: 'forbidden', add: :server }, { status: 404, name: 'not_found', add: :user }, { status: 405, name: 'method_not_allowed', add: :server }, { status: 406, name: 'not_acceptable', add: :server }, { status: 407, name: 'proxy_authentication_required', add: :server }, { status: 408, name: 'request_timeout', add: :user }, { status: 409, name: 'conflict', add: :server }, { status: 410, name: 'gone', add: :user }, { status: 411, name: 'length_required', add: :user }, { status: 412, name: 'precondition_failed', add: :user }, { status: 413, name: 'request_entity_too_large', add: :user }, { status: 414, name: 'request_uri_too_long', add: :user }, { status: 415, name: 'unsupported_media_type', add: :server }, { status: 416, name: 'requested_range_not_satisfiable', add: :user }, { status: 417, name: 'expectation_failed', add: :user }, { status: 418, name: 'im_a_teapot', add: :server }, { status: 421, name: 'misdirected_request', add: :server }, { status: 422, name: 'unprocessable_entity', add: :server }, { status: 426, name: 'upgrade_required', add: :server }, { status: 428, name: 'precondition_required', add: :server }, { status: 423, name: 'locked', add: :server }, { status: 424, name: 'failed_dependency', add: :server }, { status: 429, name: 'too_many_requests', add: :server }, { status: 431, name: 'request_header_fields_too_large', add: :user }, { status: 451, name: 'unavailable_for_legal_reasons', add: :server }, { status: 500, name: 'internal_server_error', add: :server }, { status: 501, name: 'not_implemented', add: :server }, { status: 502, name: 'bad_gateway', add: :server }, { status: 503, name: 'service_unavailable', add: :server }, { status: 504, name: 'gateway_timeout', add: :server }, { status: 505, name: 'http_version_not_supported', add: :server }, { status: 506, name: 'variant_also_negotiates', add: :server }, { status: 507, name: 'insufficient_storage', add: :server }, { status: 508, name: 'loop_detected', add: :server }, { status: 510, name: 'not_extended', add: :server }, { status: 511, name: 'network_authentication_required', add: :server } ], varieties: [ { variety: 1, sleep: 2 }, { variety: 2, sleep: 1 }, { variety: 3, sleep: 0.5 }, { variety: 4, sleep: 0.25 }, { variety: 5, sleep: 0.15 }, { variety: 6, sleep: 0.1 } ], scores: { ok: { name: 'Went through...', value: 0 }, user: { name: 'Wrong query, Data not found...', value: 1 }, server: { name: 'Wrong behavour, not patient enough...', value: 3 }, other: { name: 'Nil values and others errors...', value: 0 } }, stages: [ { name: 'Default', range: [ 0, 2 ], skip: false, sleep: 0 }, { name: 'Low', range: [ 3, 5 ], skip: false, sleep: 2 }, { name: 'High', range: [ 6, 8 ], skip: false, sleep: 5 }, { name: 'Stop', range: [ 9, 999 ], skip: true } ] }, options: {} }
Constants inherited from Single
Instance Attribute Summary
Attributes inherited from Single
Instance Method Summary collapse
- #analyse(items: nil, silent: false) ⇒ Object
- #c_log(silent, r, score) ⇒ Object
- #categories(analyse) ⇒ Object
- #channels(analyse) ⇒ Object
-
#initialize(modules: nil, group: {}, single: {}, silent: false) ⇒ Group
constructor
A new instance of Group.
- #merge ⇒ Object
- #overview(channels) ⇒ Object
- #shuffle(struct, groups) ⇒ Object
- #status(silent: false) ⇒ Object
- #struct(groups) ⇒ Object
- #to_h(type: nil) ⇒ Object
- #to_rss(key: Symbol, silent: false) ⇒ Object
- #to_rss_all(silent: false) ⇒ Object
- #valid?(item) ⇒ Boolean
Methods inherited from Single
#cmd, #formats, #modul, #set_status, #str_difference, #validate
Methods included from General
Constructor Details
#initialize(modules: nil, group: {}, single: {}, silent: false) ⇒ Group
Returns a new instance of Group.
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 |
# File 'lib/feed_into.rb', line 822 def initialize( modules: nil, group: {}, single: {}, silent: false ) mdl = modules.class.eql? String chn = single.keys.include? :channels mode = :not_found if !mdl and !chn puts 'No Channel found.' else if chn # mode = :options # transfer = Marshal.load( Marshal.dump( options[:channels] ) ) # options.delete( :channels ) end @group = Marshal.load( Marshal.dump( GROUP ) ) @group[:meta][:timestamp] = Time.now.utc.to_s if ( group, @group, true ) @single = Single.new( modules: modules, options: single, silent: silent ) @group = ( group, @group, false ) @analyse = nil @merge = nil else end end end |
Instance Method Details
#analyse(items: nil, silent: false) ⇒ Object
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 |
# File 'lib/feed_into.rb', line 850 def analyse( items: nil, silent: false ) def c_log( silent, r, score ) if !silent print "(#{score[:sleep]}) " if r[:success] if r[:result][:items].length > 0 r[:success] ? print( r[:result][:items][ 0 ][:title] ) : '' else print r[:result][:meta][:title] end else print r[:cmd][:name] r[:messages].each { | m | puts( "- #{m}" ) } end puts else end end cmds, = cmds( items ) if cmds[:valid] keys = cmds[:cmds] .map { | a | a[:cmd][:channel] } .to_set .to_a groups = keys.inject( {} ) do | hash, key | hash[ key ] = cmds[:cmds].select { | cmd | cmd[:cmd][:channel].eql?( key ) } hash end orders, struct = get_shuffle( groups ) results = struct.inject( {} ) do | item, k | item[ k[ 0 ] ] = {} item[ k[ 0 ] ][:responses] = k[ 1 ].clone item[ k[ 0 ] ][:status] = k[ 1 ].clone item end orders.each.with_index do | order, index | start = Time.now.to_f score = score( index, orders, results, @group, groups ) cmd = groups[ order[:category] ][ order[:index ] ] if score[:skip] !silent ? puts( ">> Skip: #{order}" ) : '' results[ order[:category] ][:responses][ order[:index] ] = { cmd: cmd[:cmd], skip: true } else sleep( score[:sleep] ) r = @single.analyse( item: cmd, trust_item: true ) !r[:status].class.eql? Integer ? tmp = 0 : tmp = r[:status] results[ order[:category] ][:status][ order[:index] ] = tmp #r[:status] results[ order[:category] ][:responses][ order[:index] ] = r results[ order[:category] ][:responses][ order[:index] ][:skip] = false results[ order[:category] ][:responses][ order[:index] ][:time] = Time.now.to_f - start c_log( silent, r, score ) end end else puts 'cmds not valid.' .each { | m | puts( "- #{m}" ) } end items = results .map { | k, v | v[:responses].map { | a | a } } .flatten categories = items .map { | item | item[:cmd][:category] } .to_set .to_a re_grouped = categories .inject( {} ) { | group, category | group[ category] = items.select { | a | a[:cmd][:category].eql? category } group } @analyse = re_grouped self end |
#c_log(silent, r, score) ⇒ Object
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 |
# File 'lib/feed_into.rb', line 851 def c_log( silent, r, score ) if !silent print "(#{score[:sleep]}) " if r[:success] if r[:result][:items].length > 0 r[:success] ? print( r[:result][:items][ 0 ][:title] ) : '' else print r[:result][:meta][:title] end else print r[:cmd][:name] r[:messages].each { | m | puts( "- #{m}" ) } end puts else end end |
#categories(analyse) ⇒ Object
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 |
# File 'lib/feed_into.rb', line 1106 def categories( analyse ) results = {} analyse.keys.each do | key | a = { status: { success: nil, error: nil, skip: nil, total: nil }, errors: nil, time: nil } a[:status][:success] = analyse[ key ] .reject { | a | a[:skip] } .select { | a | a[:success].eql? true } .length a[:status][:error] = analyse[ key ] .reject { | a | a[:skip] } .select { | a | a[:success].eql? false } .length a[:status][:skip] = analyse[ key ] .select { | a | a[:skip] } .length a[:status][:total] = [ :success, :error, :skip ] .map { | key | a[:status][ key ] } .sum a[:errors] = analyse[ key ] .reject { | a | a[:skip] } .select { | a | a[:success].eql? false } .map { | a | { name: a[:cmd][:name], url: a[:cmd][:url] } } a[:time] = analyse[ key ] .reject { | a | a[:skip] } .map { | a | a[:time] } .sum .round( 8 ) results[ key ] = a end results end |
#channels(analyse) ⇒ Object
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 |
# File 'lib/feed_into.rb', line 1155 def channels( analyse ) results = {} channels = analyse .map { | category | category[ 1 ] .map { | a | a[:cmd][:channel] } } .flatten .to_set .to_a channels.each do | channel | a = { status: { success: nil, error: nil, skip: nil, total: nil }, errors: nil, time: nil } cmds = analyse .map { | category | category[ 1 ] .select { | a | a[:cmd][:channel].eql? channel } } .flatten a[:status][:success] = cmds .reject { | a | a[:skip] } .select { | a | a[:success] } .length a[:status][:error] = cmds .reject { | a | a[:skip] } .select { | a | !a[:success] } .length a[:status][:skip] = cmds .select { | a | a[:skip] } .length a[:status][:total] = [ :success, :error, :skip ] .map { | key | a[:status][ key ] } .sum a[:time] = cmds .map { | a | a.keys.include?( :time ) ? a[:time] : 0 } .sum .round( 8 ) a[:errors] = cmds .reject { | a | a[:skip] } .select { | a | !a[:success] } .map { | a | { name: a[:cmd][:name], url: a[:cmd][:url] } } results[ channel ] = a end return results end |
#merge ⇒ Object
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 |
# File 'lib/feed_into.rb', line 942 def merge def valid?( item ) result = false if [ Hash, ActiveSupport::HashWithIndifferentAccess ].include? item.class one = [ :title, :time, :url ] .map { | key | item.keys.include? key } .all? if one if [ Hash, ActiveSupport::HashWithIndifferentAccess ].include? item[:time].class if item[:time].keys.include? :stamp result = true end end end end return result end = [] result = @analyse.inject( {} ) do | categories, d | all = d[ 1 ].inject( [] ) do | category, response | if response[:skip] else if response[:success] response[:result][:items].each do | item | if valid?( item ) itm = { title: item[:title], timestamp: item[:time][:stamp], url: item[:url] } category.push( itm ) else .push( '- One or more key(s) in Item are not available.' ) end end end end category end all = all.sort_by { | a | -a[:timestamp] } categories[ d[ 0 ].to_sym ] = all.clone categories end .each { | | puts( ) } @merge = result self end |
#overview(channels) ⇒ Object
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 |
# File 'lib/feed_into.rb', line 1221 def overview( channels ) results = { time: {}, all: {} } results[:time][:now] = Time.now.utc.to_s z = channels .keys .map { | key | channels[ key ][:time] } .sum .round( 0 ) results[:time][:analyse] = Time.at( z ).utc.strftime( "%Mm %Ss" ) [ :success, :error, :skip, :total ].each do | key | results[:all][ key ] = channels .map { | k, v | v[:status][ key ] } .sum end return results end |
#shuffle(struct, groups) ⇒ Object
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 |
# File 'lib/feed_into.rb', line 1322 def shuffle( struct, groups ) results = [] order = [] l = struct.map { | k, v | v.length }.sum for round in 0..l - 1 selections = struct .filter_map { | k, v | k if v.length > 0 } variety = selections.length if round == 0 current = selections[ 0 ] else case selections.length when 0 before = 0 when 1 before = 0 when 2 before = 1 when 3 before = 2 when 4 before = 3 else before = 4 end selections = selections .reject { | s | order.last( before ).include?( s ) } end current = selections[ rand( 0..selections.length - 1 ) ] r_index = rand( 0..struct[ current ].length - 1 ) index = struct[ current ][ r_index ].split( '--' ).last.to_i channel = groups[ current ][ index ][:cmd][:channel] result = { category: current, index: index, variety: variety, channel: channel } results.push( result ) struct[ current ].delete_at( r_index ) order.push( current ) end return results end |
#status(silent: false) ⇒ Object
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 |
# File 'lib/feed_into.rb', line 1105 def status( silent: false ) def categories( analyse ) results = {} analyse.keys.each do | key | a = { status: { success: nil, error: nil, skip: nil, total: nil }, errors: nil, time: nil } a[:status][:success] = analyse[ key ] .reject { | a | a[:skip] } .select { | a | a[:success].eql? true } .length a[:status][:error] = analyse[ key ] .reject { | a | a[:skip] } .select { | a | a[:success].eql? false } .length a[:status][:skip] = analyse[ key ] .select { | a | a[:skip] } .length a[:status][:total] = [ :success, :error, :skip ] .map { | key | a[:status][ key ] } .sum a[:errors] = analyse[ key ] .reject { | a | a[:skip] } .select { | a | a[:success].eql? false } .map { | a | { name: a[:cmd][:name], url: a[:cmd][:url] } } a[:time] = analyse[ key ] .reject { | a | a[:skip] } .map { | a | a[:time] } .sum .round( 8 ) results[ key ] = a end results end def channels( analyse ) results = {} channels = analyse .map { | category | category[ 1 ] .map { | a | a[:cmd][:channel] } } .flatten .to_set .to_a channels.each do | channel | a = { status: { success: nil, error: nil, skip: nil, total: nil }, errors: nil, time: nil } cmds = analyse .map { | category | category[ 1 ] .select { | a | a[:cmd][:channel].eql? channel } } .flatten a[:status][:success] = cmds .reject { | a | a[:skip] } .select { | a | a[:success] } .length a[:status][:error] = cmds .reject { | a | a[:skip] } .select { | a | !a[:success] } .length a[:status][:skip] = cmds .select { | a | a[:skip] } .length a[:status][:total] = [ :success, :error, :skip ] .map { | key | a[:status][ key ] } .sum a[:time] = cmds .map { | a | a.keys.include?( :time ) ? a[:time] : 0 } .sum .round( 8 ) a[:errors] = cmds .reject { | a | a[:skip] } .select { | a | !a[:success] } .map { | a | { name: a[:cmd][:name], url: a[:cmd][:url] } } results[ channel ] = a end return results end def overview( channels ) results = { time: {}, all: {} } results[:time][:now] = Time.now.utc.to_s z = channels .keys .map { | key | channels[ key ][:time] } .sum .round( 0 ) results[:time][:analyse] = Time.at( z ).utc.strftime( "%Mm %Ss" ) [ :success, :error, :skip, :total ].each do | key | results[:all][ key ] = channels .map { | k, v | v[:status][ key ] } .sum end return results end if @analyse.nil? !silent ? puts( 'Data is not analysed, use .analyse() before.' ) : '' else = { overview: {}, channels: {}, categories: {} } [:categories] = categories( @analyse ) [:channels] = channels( @analyse ) [:overview] = overview( [:channels] ) end return end |
#struct(groups) ⇒ Object
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 |
# File 'lib/feed_into.rb', line 1304 def struct( groups ) boilerplate = groups .inject( {} ) { | hash, k | hash[ k[ 0 ] ] = k[ 1 ].length; hash } struct = boilerplate.inject( {} ) do | hash, key | hash[ key[ 0 ] ] = key[ 1 ].times.map { | a | nil } hash end groups = boilerplate.inject( {} ) do | hash, key | hash[ key[ 0 ] ] = key[ 1 ].times.map { | a | "#{key[ 0 ]}--#{a}" } hash end return groups, struct end |
#to_h(type: nil) ⇒ Object
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 |
# File 'lib/feed_into.rb', line 1000 def to_h( type: nil ) if @analyse.nil? and @merge.nil? puts 'No Data found, please use .analyse() before.' return nil else if type.nil? if @merge.nil? return @analyse else return @merge end else case type when :merge return @merge when :analyse return @analyse end end end end |
#to_rss(key: Symbol, silent: false) ⇒ Object
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 |
# File 'lib/feed_into.rb', line 1023 def to_rss( key: Symbol, silent: false ) result = '' if @merge.nil? !silent ? puts( 'Data is not merged in groups, use .merge() before.' ) : '' else if @merge.keys.include? key builder = Nokogiri::XML::Builder.new( encoding: 'utf-8' ) builder.feed( xmlns: 'http://www.w3.org/2005/Atom', 'xmlns:dc': 'http://purl.org/dc/elements/1.1/' ) do | root | = { title: key.to_s, updated: Time.now.to_s, dcdate: Time.now.to_s } root. { | a | a.name { | aa | aa.text( '' ) } } root.id() { | a | a.text( '' ) } root.title() { | a | a.text( [:title] ) } root.updated() { | a | a.text( [:updated] ) } root['dc'].date { | a | a.text( [:dcdate] ) } @merge[ key ] .each { | entry | root.entry() { | a | d = Time.at( entry[:timestamp] ).to_datetime.to_s a.id() { | b | b.text( entry[:url] ) } a.link( rel: 'alternate', href: entry[:url] ) a.title() { | b | b.text( entry[:title] ) } a.updated() { | b | b.text( d ) } a['dc'].date { | b | b.text( d ) } } } end =begin rss = RSS::Maker.make( 'atom' ) do | maker | maker.channel.author = '' maker.channel.updated = Time.now.to_s maker.channel.about = '' maker.channel.title = key.to_s @merge[ key ].each do | entry | maker.items.new_item do | item | item.link = entry[:url] item.title = entry[:title] d = Time.at( entry[:timestamp] ).to_datetime.to_s item.updated = d end end end =end else !silent ? puts( 'Key does not exist.' ) : '' end result = builder.to_xml.to_s #rss.to_s.gsub( '<link href="', '<link rel="alternate" href="' ) end return result end |
#to_rss_all(silent: false) ⇒ Object
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 |
# File 'lib/feed_into.rb', line 1090 def to_rss_all( silent: false ) results = {} if @merge.nil? !silent ? puts( 'Data is not merged in groups, use .merge() before.' ) : '' else @merge.keys.each do | key | results[ key ] = to_rss( key: key, silent: false ) end end return results end |
#valid?(item) ⇒ Boolean
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 |
# File 'lib/feed_into.rb', line 943 def valid?( item ) result = false if [ Hash, ActiveSupport::HashWithIndifferentAccess ].include? item.class one = [ :title, :time, :url ] .map { | key | item.keys.include? key } .all? if one if [ Hash, ActiveSupport::HashWithIndifferentAccess ].include? item[:time].class if item[:time].keys.include? :stamp result = true end end end end return result end |