Class: NoticeSys::HashtagQueryView

Inherits:
Object
  • Object
show all
Defined in:
lib/noticesys.rb

Instance Method Summary collapse

Constructor Details

#initialize(basepath, db_basepath, css_url, weblet, static_urlbase, urlbase, debug: false) ⇒ HashtagQueryView

Returns a new instance of HashtagQueryView.



402
403
404
405
406
407
408
409
# File 'lib/noticesys.rb', line 402

def initialize(basepath, db_basepath, css_url, weblet, static_urlbase,
               urlbase, debug: false)

  @basepath, @css_url, @static_urlbase = basepath,  css_url, static_urlbase
  @db_basepath, @w, @urlbase, @debug = db_basepath, weblet, urlbase, debug
  @card = CardView.new(@w)

end

Instance Method Details

#render(q, referer) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/noticesys.rb', line 411

def render(q, referer)
  #q = args.first
  dbindex_path = File.join(@db_basepath, '/hashtag/index.db')
  puts 'dbindex_path: ' + dbindex_path.inspect if @debug

  db = RecordxSqlite.new(dbindex_path, table: 'hashtags')


  a = db.find_all_by_tag q

  topics = a.map(&:topic).uniq.map do |topic|

    topicpath = File.join(@basepath, 'u', topic)
    topic_db_path = File.join(@db_basepath, 'u', topic)

    db_filepath = File.join(topic_db_path, "notices.db")
    puts 'db_filepath: ' + db_filepath.inspect if @debug
    db = RecordxSqlite.new(db_filepath, table: 'notices')

    dx = Dynarex.new(File.join(topicpath, 'feed.xml'))
    [topic, OpenStruct.new(title: dx.title, image: dx.image, db: db)]

  end.to_h


  rows = a.map do |x|

    r = topics[x.topic]
    rx = r.db.find x.noticeid
    link = "%s/%s/status/%s" % [@urlbase, x.topic, x.noticeid]
    OpenStruct.new(rx.to_h.merge({topic: x.topic, title: r.title,
                                  image: r.image, bio: r.bio, link: link}))

  end


  s = ''

  notices = rows.sort_by {|x| -(x.id.to_i)}.take(20).map do |rx|

    next unless rx.description

    card2 = ''
    rawcard = rx.to_h[:card]

    card2 = if rawcard and rawcard.length > 10 then

      card = JSON.parse(rawcard, symbolize_names: true)

      if card.is_a? Hash then
        @card.render(dx, rx, card)
      end

    else
      ''
    end

    t2 = Time.at rx.id.to_s[0..9].to_i
    relative_time = Unichron.new(t2).elapsed

    d = t2.strftime("%I:%M%p %b %d %Y")

    description = if rx.description.length > 1 then
      doc = Rexle.new "<node>%s</node>" % rx.description.gsub(/<\/?p>/,'')
      doc.root.xpath('img|div').each(&:delete)
      "<p>%s</p>" % doc.root.xml
    else
      ''
    end

    utitle, uimage, ubio = rx.title, rx.image, rx.bio
    @w.render :notice, binding

  end

  s += if notices.any? then
    notices.compact.join
  else
    "<p>No results for #{q}</p>"
  end

  ref = referer
  ref = '../' if ref =~ /(?:status|hashtag)\/\d+/

  svg = @w.render 'svg/backarrow', binding
  back = ref ? "<a href='#{ref}'>#{svg}</a>" : ''

  @w.render :search_pg, binding

end