Class | OpenWFE::Extras::AtomPubParticipant |
In: |
lib/openwfe/extras/participants/atompub_participants.rb
|
Parent: | Object |
This participants posts (as in HTTP POST) a workitem to an AtomPub enabled resource.
target_uri = "https://openwferu.wordpress.com/wp-app.php/posts" params = {} params[:username] = 'jmettraux' params[:password] = ENV['WORDPRESS_PASSWORD'] params[:categories] = 'openwferu, test' engine.register_participant( "app", OpenWFE::Extras::AtomPubParticipant.new target_uri, params)
This base implementation dumps workitem as YAML in the entry content.
See BlogParticipant for a human-oriented blog posting participant.
author_name | [RW] | |
author_uri | [RW] | |
target_uri | [RW] | The URI to post to |
# File lib/openwfe/extras/participants/atompub_participants.rb, line 89 89: def initialize (target_uri, params) 90: 91: @target_uri = target_uri 92: 93: @username = params[:username] 94: @password = params[:password] 95: 96: @author_name = \ 97: params[:author_name] || self.class.name 98: @author_uri = \ 99: params[:author_uri] || "http://openwferu.rubyforge.org" 100: 101: 102: @categories = params[:categories] || [] 103: @categories = @categories.split(",") if @categories.is_a?(String) 104: @categories = Array(@categories) 105: end
The incoming workitem will generate an atom entry that will get posted to the target URI.
This consume() method returns the URI (as a String) where the just uploaded post can be edited.
# File lib/openwfe/extras/participants/atompub_participants.rb, line 114 114: def consume (workitem) 115: 116: entry = Atom::Entry.new 117: entry.updated! # set updated time to now 118: 119: render_author entry, workitem 120: render_categories entry, workitem 121: render_content entry, workitem 122: 123: h = Atom::HTTP.new 124: h.user = @username 125: h.pass = @password 126: h.always_auth = :basic 127: 128: res = Atom::Collection.new(@target_uri, h).post!(entry) 129: 130: # initial implementation 131: # don't catch an error, let the process fail 132: 133: #res.read_body 134: extract_new_link res 135: end
Extracts the link of the newly created resource (newly posted blog entry), and returns it as a String.
# File lib/openwfe/extras/participants/atompub_participants.rb, line 193 193: def extract_new_link (response) 194: 195: doc = REXML::Document.new response.read_body 196: 197: #REXML::XPath.first(doc.root, "//link[@rel='edit']") 198: # 199: # doesn't work :( 200: 201: REXML::XPath.first(doc.root, "//link[2]").attribute('href') 202: # 203: # will break if the order changes :( 204: end
This default implementation simply builds a single author out of the :author_name, :author_uri passed as initialization params.
# File lib/openwfe/extras/participants/atompub_participants.rb, line 159 159: def render_author (entry, workitem) 160: 161: author = Atom::Author.new 162: author.name = author_name 163: author.uri = author_uri 164: 165: entry.authors << author 166: end
This base implementations simply adds the categories listed in the :categories initialization parameter. The target_uri is used as the scheme for the categories.
You can override this method to add extra categories or to have completely different categories.
# File lib/openwfe/extras/participants/atompub_participants.rb, line 176 176: def render_categories (entry, workitem) 177: 178: @categories.each do |s| 179: 180: c = Atom::Category.new 181: 182: c["scheme"] = @target_uri 183: c["term"] = s.strip 184: 185: entry.categories << c 186: end 187: end
This base implementation simply uses a YAML dump of the workitem as the post content (with a content type of ‘html’).
# File lib/openwfe/extras/participants/atompub_participants.rb, line 143 143: def render_content (entry, workitem) 144: 145: entry.title = \ 146: workitem.participant_name + " " + 147: workitem.fei.expression_id + " " + 148: workitem.fei.workflow_instance_id 149: 150: entry.content = workitem.to_yaml 151: entry.content["type"] = "html" 152: end