Class: Hobix::Facets::Trackbacks

Inherits:
BaseFacet show all
Defined in:
lib/hobix/facets/trackbacks.rb

Overview

The Trackbacks plugin adds support for the TrackBack specification (www.sixapart.com/pronet/docs/trackback_spec).

Add this require to your hobix.yaml:

requires:
- hobix/trackbacks

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseFacet

not_found, #protect

Methods inherited from BasePlugin

inherited, start

Constructor Details

#initialize(weblog, defaults = {}) ⇒ Trackbacks

Returns a new instance of Trackbacks.



34
35
36
# File 'lib/hobix/facets/trackbacks.rb', line 34

def initialize( weblog, defaults = {} )
    @weblog = weblog
end

Class Method Details

.trackback_classObject



32
# File 'lib/hobix/facets/trackbacks.rb', line 32

def self.trackback_class; Hobix::Trackback; end

.trackback_fieldsObject



31
# File 'lib/hobix/facets/trackbacks.rb', line 31

def self.trackback_fields; ['url','title', 'excerpt', 'blog_name']; end

Instance Method Details

#get(app) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hobix/facets/trackbacks.rb', line 37

def get app
    if app.respond_to? :action_uri
        action, entry_id = app.action_uri.split( '/', 2 )
        case action
        when "trackback"
            # Validate
            on_entry = @weblog.storage.load_entry( entry_id ) rescue nil
            return send_trackback_response( app, false, 'No such entry' ) if on_entry.nil?

            # Create a trackback comment
            trackback = Trackbacks.trackback_class.new do |t|
                Trackbacks.trackback_fields.each do |tf|
                  t.method( "#{tf}=" ).call( app._POST[tf].to_s )
                end
                return send_trackback_response( app, false, 'Missing URL field' ) if (t.url || '').empty?
                t.created = Time.now
                t.ipaddress = app.remote_addr
            end

            # Save the trackback, upgen
            @weblog.storage.append_to_attachment( entry_id, 'trackbacks', trackback )
            @weblog.regenerate :update

            # Send response
            send_trackback_response( app, true )
            return true
        end
    end
end

#send_trackback_response(app, ok = true, message = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/hobix/facets/trackbacks.rb', line 67

def send_trackback_response(app, ok = true, message = nil)
  app.content_type = 'text/xml'
  app.puts %{<?xml version="1.0" encoding="UTF-8"?>
    <response>
      <error>%d</error>
      %s
    </response>
  } % [ok ? 0 : 1, message ? %{<message>#{message}</message>} : '']
  true
end