Sha256: 562e140b09b9abfbcf4158f34d60b428e4695168fed7c56d709939cd6ab09cf9

Contents?: true

Size: 1.81 KB

Versions: 5

Compression:

Stored size: 1.81 KB

Contents

def send_trackback(post, url, app_config)
  require 'uri'
  require 'net/http'
  begin
    uri = URI.parse(url)
    oururi = URI.parse(url_for(:controller => 'ping', :action => 'trackback', :only_path => false))
    if oururi.host == uri.host && oururi.port == uri.port
      # This is the same EJ instance, which may not be able to handle
      # simultaneous connections (webrick), so dump this ping right to the DB
      if /.*\/(\d{1,7})/ =~ url
        origpost       = Post.find($1) rescue nil
        return if origpost.nil?
        ping           = origpost.build_to_pings
        ping.title     = post.subject
        logger.info "LOGGING #{strip_html(post.rendered, false)[0..255]}"
        ping.excerpt   = strip_html(post.rendered, false)[0..255]
        ping.url       = url_for(:controller => 'journal', :action => 'view', :id => post.id, :only_path => false)
        ping.blog_name = post.user.title
        ping.save
      end
      return
    end
    qrystr = "title=#{URI.escape(post.subject)}"
    qrystr << "&excerpt=#{strip_html(post.rendered)[0..255]}"
    qrystr << "&url=#{@request.protocol}#{@request.host_with_port}/journal/view/#{post.id}"
    qrystr << "&blog_name=#{URI.escape(post.user.title)}"

    Net::HTTP.start(uri.host, uri.port) do |http|
      http.post("#{uri.path}?#{uri.query}", qrystr)
      # I don't really care if this fails or not right now.
    end
  rescue
  end
end

def strip_html(text, escape=true)
  attribute_key = /[\w:_-]+/
  attribute_value = /(?:[A-Za-z0-9]+|(?:'[^']*?'|"[^"]*?"))/
  attribute = /(?:#{attribute_key}(?:\s*=\s*#{attribute_value})?)/
  attributes = /(?:#{attribute}(?:\s+#{attribute})*)/
  tag_key = attribute_key
  tag = %r{<[!/?\[]?(?:#{tag_key}|--)(?:\s+#{attributes})?\s*(?:[!/?\]]+|--)?>}
  text.gsub(tag, '').gsub(/\s+/, ' ').strip
  escape ? CGI::escape(text) : text
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
EliteJournal-1.9.400 lib/trackback.rb
EliteJournal-1.9.401 lib/trackback.rb
EliteJournal-1.9.403 lib/trackback.rb
EliteJournal-1.9.480 lib/trackback.rb
EliteJournal-1.9.492 lib/trackback.rb