class NoticeSys::SearchQueryView

Public Class Methods

new(basepath, css_url, weblet, static_urlbase, urlbase) click to toggle source
# File lib/noticesys.rb, line 496
def initialize(basepath, css_url, weblet, static_urlbase, urlbase)
  
  @basepath, @css_url, @static_urlbase = basepath,  css_url, static_urlbase
  @w, @urlbase = weblet, urlbase
  @card = CardView.new(@w)
  
end

Public Instance Methods

render(q, referer) click to toggle source
# File lib/noticesys.rb, line 504
def render(q, referer)      
  

  users = Dir.glob(File.join(@basepath, 'u', '*')).map do |x|
    File.basename(x)
  end

  topics = users.map do |topic|  

    topicpath = File.join(@basepath, 'u', topic)
    filepath = File.join(topicpath, "notices.db")
    db = RecordxSqlite.new(filepath, table: 'notices')

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

  end.to_h

  #return topics.inspect
  rows = topics.flat_map do |topic, r|

    a = r.db.all.select {|x| x.description =~ /#{q}/i}
    a.map do |rx|
      link = "%s/%s/status/%s" % [@urlbase, topic, rx.noticeid]
      OpenStruct.new(rx.to_h.merge({topic: topic, title: r.title, image: r.image, link: link}))
    end

  end
  
  
  #return rows.inspect
  s = ''
  
  notices = rows.sort_by {|x| -(x.id.to_i)}.take(20).map do |rx|
  
    
    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   
    
    #rx.description
    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")
    
    #desc = rx.description.gsub(/<\/?p>/,'').gsub('&lt;','<').gsub('&gt;','>')
    
    topic = rx.topic
    title, image, bio = %i(title image bio).map do |x|
      topics[topic].method(x).call
    end
    
    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 = title, image, bio
    @w.render :notice, binding


  end
  #return notices.inspect
  
  s += if notices.any? then
    notices.join      
  else
    "<p>No results for #{q}</p>"
  end
  
  #ref =  @env["HTTP_REFERER"]
  ref = referer
  ref = '../' if ref =~ /(?:status\/\d+|search)/
  svg = @w.render 'svg/backarrow', binding
  back = ref ? "<a href='#{ref}'>#{svg}</a>" : ''      
  
  @w.render :search_pg, binding
  
  
end