class MyMedia::Base

Attributes

to_s[R]

Public Class Methods

new(media_type: 'blog', public_type: 'blog', ext: 'txt', config: nil, log: nil) click to toggle source
Calls superclass method MyMedia::Publisher::new
# File lib/mymedia.rb, line 89
def initialize(media_type: 'blog', public_type: 'blog', 
               ext: 'txt', config: nil, log: nil)

  super()      

  @schema = 'posts/post(title, url, raw_url)'      

  raise BaseException, "no config found" if config.nil?

  c = SimpleConfig.new(config).to_h
  
  @home = c[:home]
  @website = c[:website]    

  @dynamic_website = c[:dynamic_website]
  @www = c[:www]
  @domain = @website[/[^\.]+\.[^\.]+$/]

  @sps = c[:sps]
  
  @log = log


  @media_type = media_type
  @public_type = public_type ||= @media_type
  
  @xslt_schema = 'channel[title:title,description:desc]/' + \
                                            'item(title:title,link:url)'
  @ext = ext
  @rss = false
  
  Dir.chdir @home

end

Public Instance Methods

add_feed_item(raw_msg, record, options={}) click to toggle source
# File lib/mymedia.rb, line 124
def add_feed_item(raw_msg, record, options={})
  
  dynarex_filepath = File.join([@home, @public_type, 'dynarex.xml'])        
  id = Increment.update(File.join([@home, @public_type, 'counter.txt'])) 
  static_url = @static_baseurl + id
  record[:uri] = static_url
  
  publish_dynarex(dynarex_filepath, record, {id: id}.merge(options))                    
  publish_timeline(raw_msg, static_url)         
  publish_html(@home + '/index.html') 
end
auto_copy_publish(raw_msg='') click to toggle source
# File lib/mymedia.rb, line 136
def auto_copy_publish(raw_msg='')

  dir = DirToXML.new(@media_src, recursive: true)

  r = dir.last_modified

  filename = r.is_a?(Hash) ? r[:name] : File.join(r.map {|x| x[:name]})

  copy_publish( filename ,raw_msg)

end
basename(s1, s2) click to toggle source
# File lib/mymedia.rb, line 148
def basename(s1, s2)

  (s2.split('/') - s1.split('/')).join('/')

end
copy_publish(filename, raw_msg='') click to toggle source
# File lib/mymedia.rb, line 154
def copy_publish(filename, raw_msg='')
  file_publish(File.join(@media_src,filename), raw_msg)
end

Private Instance Methods

file_publish(src_path, raw_msg='') { |destination, raw_destination| ... } click to toggle source
# File lib/mymedia.rb, line 161
def file_publish(src_path, raw_msg='')

  #raise @logger.debug("source file '%s' not found" % src_path) unless File.exists? src_path
  ext = File.extname(src_path)
  @target_ext ||= ext
      
  public_path = "%s/%s/%shrs%s" % [@public_type, \
    Time.now.strftime('%Y/%b/%d').downcase, Time.now.strftime('%H%M'), 
                                  @target_ext]

  public_path2 = "%s/%s/%shrs%s%s" % [@public_type, \
    Time.now.strftime('%Y/%b/%d').downcase, Time.now.strftime('%H%M'), 
                                      Time.now.strftime('%S%2N'), @target_ext]
  
  raw_destination = "%s/%s/%s" % [@home, 'r', public_path]
  
  if File.exists? raw_destination then
    raw_destination = "%s/%s/%s" % [@home, 'r', public_path2]
    public_path = public_path2
  end

  destination = "%s/%s" % [@home, public_path]
  FileUtils.mkdir_p File.dirname(raw_destination)
  FileUtils.mkdir_p File.dirname(destination)

  raw_msg = raw_msg.join ' ' if raw_msg.is_a? Array

  raw_msg = src_path[/([^\/]+)\.\w+$/,1] + ' ' + raw_msg if raw_msg[/^#/]

  
  if block_given? then
    raw_msg, target_url = yield(destination, raw_destination) 
    static_url = target_url
  else
    FileUtils.cp src_path, destination
    FileUtils.cp src_path, raw_destination
  end

  raw_msg = raw_msg.join if raw_msg.is_a? Array   

  static_filename = if raw_msg.to_s.length > 0 then
     normalize(raw_msg) + File.extname(destination)
  else
    
    basename(@media_src, src_path)
    
  end      
  
  static_path = "%s/%s/%s" % [@public_type, \
    Time.now.strftime('%Y/%b/%d').downcase, static_filename]
  
  raw_static_destination = "%s/%s/%s" % [@home, 'r',static_path]

  static_destination = "%s/%s" % [@home, static_path]    

  #FileUtils.mkdir_p File.dirname(static_destination)
  FileUtils.cp destination, static_destination

  #jr010817 FileUtils.cp raw_destination, raw_static_destination

  # Make a static filename XML file copy?
  if File.extname(static_destination) == '.html' then

    xmlfilepath = destination.sub('.html','.xml')
    
    if File.exists?(xmlfilepath) then
      FileUtils.cp xmlfilepath, static_destination.sub('.html','.xml')
    end

  end
  
  target_url ||= "%s/%s" % [@website, public_path]
  static_url ||= "%s/%s" % [@website, static_path]

  msg = "%s %s" % [target_url, raw_msg ]
  
  sps_message = ['publish', @public_type, 
                target_url, static_url, raw_msg]

  send_message(msg: sps_message.join(' '))

  static_url
  
end
normalize(s) click to toggle source
# File lib/mymedia.rb, line 246
def normalize(s)

  r = s.downcase.gsub(/\s#\w+/,'').strip.gsub(/\W/,'-').gsub(/-{2,}/,'-').gsub(/^-|-$/,'')
  return s.scan(/#(\w+)/)[0..1].join('_').downcase if r.empty?
  return r        
end