Class: FeedInto::Single

Inherits:
Object
  • Object
show all
Includes:
General
Defined in:
lib/feed_into.rb

Direct Known Subclasses

Group

Constant Summary collapse

SINGLE =
{
  format: {
    title: {
      symbol: {
        video: '👾',
        custom: '⚙️ ',
        web: '🤖'
      },
      separator: '|',
      more: '...',
      length: 100,
      str: '{{sym}} {{cmd_name__upcase}} ({{channel_name__upcase}}) {{separator}} {{title_item__titleize}}'
    },
    download: {
      agent: ''
    }
  },
  validation: {
    allows: [
      :format__title__symbol__video,
      :format__title__symbol__custom,
      :format__title__symbol__web,
      :format__title__separator,
      :format__title__more,
      :format__title__length,
      :format__title__str,
      :format__download__agent,
    ],
    wildcards: [
      :options__s3
    ]
  },
  channels: [],
  options: {}
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from General

#crl_general

Constructor Details

#initialize(modules: nil, options: {}, silent: false) ⇒ Single

Returns a new instance of Single.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/feed_into.rb', line 61

def initialize( modules: nil, options: {}, silent: false )
  mdl = modules.class.eql? String
  chn = options.keys.include? :channels
  mode = :not_found

  if !mdl and !chn
    puts 'No Channel found.'
  else
    mode = nil

    if chn
      mode = :options
      transfer = Marshal.load( Marshal.dump( options[:channels] ) )
      options.delete( :channels )
    end

    mdl ? mode = :folder : ''

    @single = Marshal.load( Marshal.dump( SINGLE ) )
    if options_update( options, @single, true )
      @single[:channels].concat( crl_general_channels() )

      chn ? @single[:channels].concat( transfer ) : ''
      mdl ? @single[:channels].concat( load_modules( modules, silent ) ) : ''

      @single = options_update( options, @single, false )
      @settings = @single
    else
    end
  end
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



57
58
59
# File 'lib/feed_into.rb', line 57

def settings
  @settings
end

Instance Method Details

#analyse(item: {}, trust_item: false) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/feed_into.rb', line 94

def analyse( item: {}, trust_item: false )
  def modul( type, channel, allow_methods, cmd, response, data, obj )
    messages = []
    if !channel[ type ].nil?
      error = nil
      execute = true
      name = nil
      case channel[ type ]
        when :self
          name = ( 'crl_' + channel[:name].to_s ).to_sym
          !allow_methods.include?( name ) ? execute = false : ''
        when :general
          name = :crl_general
      else
        name = :crl_general
        type = ( type.to_s + '_' + channel[ type ].to_s ).to_sym
      end

      if execute
        data, messages = self
          .method( name )
          .call( type, cmd, channel, response, data, obj )
      else
        messages = [ "Modul: #{name} not found." ]
      end
    else
    end

    return data, messages
  end


  def formats( type, cmd, channel, response, data, obj )
    channel[ type ].each do | format_ |
      data = self
        .method( 'crl_general' )
        .call( format_, cmd, channel, response, data, obj )[ 0 ]
    end
    return data
  end


  def set_status( messages )
    s = 'Download: Status '
    status = nil

    if messages.class.eql? Array
      tmp = messages
        .find { | a | a.start_with?( s ) }
        .to_s
        .gsub( s, '')
        .to_i
      tmp.nil? ? status = 0 : status = tmp
    else
    end

    return status
  end


  result = {
    cmd: nil,
    result: nil,
    messages: nil,
    time: nil,
    success: false,
    status: nil
  }

  obj = Marshal.load( Marshal.dump( @single ) )

  begin
    start = Time.now.to_f
    messages = []
    status = nil
    cmd = {}
    data = nil

    if trust_item
      cmd, m0 = item[:cmd], item[:messages]
    else
      cmd, m0 = cmd( item, obj )
    end

    messages.concat( m0 )
    if cmd[:valid]
      channel = obj[:channels].find { | c | c[:name].eql? cmd[:channel] }
      allow_channels = obj[:channels].map { | a | ( 'crl_' + a[:name].to_s ).to_sym }
      allow_methods = self.methods.select { | a | allow_channels.include?( a.to_sym ) }

      response, m1 = modul( :download, channel, allow_methods, cmd, response, nil, obj )
      messages.concat( m1 )

      data, m2 = modul( :mining, channel, allow_methods, cmd, response, nil, obj )
      messages.concat( m2 )

      data = formats( :pre, cmd, channel, response, data, obj )

      data, m3 = modul( :transform, channel, allow_methods, cmd, response, data, obj )
      messages.concat( m3 )

      data = formats( :post, cmd, channel, response, data, obj )
      result[:success] = true
    end

    result[:cmd] = cmd
    result[:result] = data 
    result[:messages] = messages
    result[:time] = Time.now.to_f - start

    status = set_status( messages )
    result[:status] = status

  rescue => e
    messages.push( "Begin/Rescue: #{e}, #{messages}, #{cmd}" )

    result[:cmd] = cmd
    result[:result] = data 
    result[:messages] = messages
    result[:time] = Time.now.to_f - start

    status = set_status( messages )
    result[:status] = status
  end

  return result
end

#cmd(cmd, obj) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/feed_into.rb', line 223

def cmd( cmd, obj )
  def validate( cmd )
    check = {
      validation: {
        struct: false,
        url: false,
        channel: false,
      },
      struct: {
        name: String,
        url: String,
        category: Symbol,
        channel: Symbol
      }
    }

    messages = []

    check[:validation][:struct] = check[:struct]
      .map { | k, v | cmd[ k ].class.eql? v } 
      .all?

    !check[:validation][:struct] ? messages.push( 'Structure of cmd is not valid.' ) : ''

    if cmd[:url] =~ URI::regexp
      check[:validation][:url] = true
    else
      messages.push( "'#{cmd[:url]}' is not a valid URL.")
    end

    if !cmd[:channel].eql? :not_found
      check[:validation][:channel] = true
    else
      messages.push( 'Channel not found' )
    end

    cmd[:valid] = check[:validation].map { | k, v | v }.all?

    return cmd, messages
  end


  if [ Hash, ActiveSupport::HashWithIndifferentAccess ].include? cmd.class
    !cmd.key?( :name ) ? cmd[:name] = 'Unknown' : ''
    !cmd.key?( :category ) ? cmd[:category] = :unknown : ''
    cmd[:category].class.eql?( String ) ? cmd[:category] = cmd[:category].to_s.downcase.to_sym : ''
  else
    if cmd.class.eql? String
      cmd = {
        name: 'Unknown',
        url: cmd,
        category: :unknown,
        #channel: :not_found
      }
    else
      cmd = {
        name: 'Invalid',
        url: cmd.to_s,
        category: :invalid,
        channel: :not_found
      }
    end
  end

  f = obj[:channels].find do | channel | 
    r = channel[:regexs]
      .map { | ps | ps.map { | p | cmd[:url].match?( p ) }.all? }
      .include?( true )
  end

  !f.nil? ? cmd[:channel] = f[:name] : cmd[:channel] = :not_found

  valid, messages = validate( cmd )

  return valid, messages
end

#formats(type, cmd, channel, response, data, obj) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/feed_into.rb', line 126

def formats( type, cmd, channel, response, data, obj )
  channel[ type ].each do | format_ |
    data = self
      .method( 'crl_general' )
      .call( format_, cmd, channel, response, data, obj )[ 0 ]
  end
  return data
end

#modul(type, channel, allow_methods, cmd, response, data, obj) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/feed_into.rb', line 95

def modul( type, channel, allow_methods, cmd, response, data, obj )
  messages = []
  if !channel[ type ].nil?
    error = nil
    execute = true
    name = nil
    case channel[ type ]
      when :self
        name = ( 'crl_' + channel[:name].to_s ).to_sym
        !allow_methods.include?( name ) ? execute = false : ''
      when :general
        name = :crl_general
    else
      name = :crl_general
      type = ( type.to_s + '_' + channel[ type ].to_s ).to_sym
    end

    if execute
      data, messages = self
        .method( name )
        .call( type, cmd, channel, response, data, obj )
    else
      messages = [ "Modul: #{name} not found." ]
    end
  else
  end

  return data, messages
end

#set_status(messages) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/feed_into.rb', line 136

def set_status( messages )
  s = 'Download: Status '
  status = nil

  if messages.class.eql? Array
    tmp = messages
      .find { | a | a.start_with?( s ) }
      .to_s
      .gsub( s, '')
      .to_i
    tmp.nil? ? status = 0 : status = tmp
  else
  end

  return status
end

#str_difference(a, b) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
# File 'lib/feed_into.rb', line 343

def str_difference( a, b )
  a = a.to_s.downcase.split( '_' ).join( '' )
  b = b.to_s.downcase.split( '_' ).join( '' )
  longer = [ a.size, b.size ].max
  same = a
    .each_char
    .zip( b.each_char )
    .select { | a, b | a == b }
    .size
  ( longer - same ) / a.size.to_f
end

#validate(cmd) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/feed_into.rb', line 224

def validate( cmd )
  check = {
    validation: {
      struct: false,
      url: false,
      channel: false,
    },
    struct: {
      name: String,
      url: String,
      category: Symbol,
      channel: Symbol
    }
  }

  messages = []

  check[:validation][:struct] = check[:struct]
    .map { | k, v | cmd[ k ].class.eql? v } 
    .all?

  !check[:validation][:struct] ? messages.push( 'Structure of cmd is not valid.' ) : ''

  if cmd[:url] =~ URI::regexp
    check[:validation][:url] = true
  else
    messages.push( "'#{cmd[:url]}' is not a valid URL.")
  end

  if !cmd[:channel].eql? :not_found
    check[:validation][:channel] = true
  else
    messages.push( 'Channel not found' )
  end

  cmd[:valid] = check[:validation].map { | k, v | v }.all?

  return cmd, messages
end