ruote-extras 0.9.18

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,174 @@
1
+ #
2
+ #--
3
+ # Copyright (c) 2007-2008, John Mettraux, OpenWFE.org
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # . Redistributions of source code must retain the above copyright notice, this
10
+ # list of conditions and the following disclaimer.
11
+ #
12
+ # . Redistributions in binary form must reproduce the above copyright notice,
13
+ # this list of conditions and the following disclaimer in the documentation
14
+ # and/or other materials provided with the distribution.
15
+ #
16
+ # . Neither the name of the "OpenWFE" nor the names of its contributors may be
17
+ # used to endorse or promote products derived from this software without
18
+ # specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ # POSSIBILITY OF SUCH DAMAGE.
31
+ #++
32
+ #
33
+
34
+ #
35
+ # "made in Japan"
36
+ #
37
+ # John Mettraux at openwfe.org
38
+ #
39
+
40
+ #
41
+ # this participant requires atom-tools from
42
+ #
43
+ # http://code.necronomicorp.com/trac/atom-tools
44
+ #
45
+ # atom-tools' license is X11/MIT
46
+ #
47
+
48
+ require 'monitor'
49
+
50
+ #require 'rubygems'
51
+ require 'atom/collection' # gem 'atom-tools'
52
+
53
+ require 'openwfe/participants/participant'
54
+
55
+
56
+ module OpenWFE::Extras
57
+
58
+ #
59
+ # Stores the incoming workitem into an 'atom feed'
60
+ #
61
+ # An example :
62
+ #
63
+ # feed0 = AtomFeedParticipant.new(
64
+ # 20, # no more than 20 entries are kept
65
+ # """
66
+ # <p>
67
+ # <h1>${f:colour}</h1>
68
+ # </p>
69
+ # """) # the template for each entry
70
+ #
71
+ # The 'template' parameter may contain an instance of File instead of
72
+ # an instance of String.
73
+ #
74
+ # feed0 = AtomFeedParticipant.new(
75
+ # 20, File.new("path/to/my/atom/template.txt")
76
+ #
77
+ # The template can be passed as the second parameter (after the max entry
78
+ # count) or as a block :
79
+ #
80
+ # feed1 = AtomFeedParticipant.new(20) do |flow_expression, atom_participant, workitem|
81
+ # #
82
+ # # usually only the workitem parameter is used
83
+ # # but the other two allow for advanced tricks...
84
+ #
85
+ # atom_participant.content_type = "xml"
86
+ # # by default, it's "xhtml"
87
+ #
88
+ # s = "<task>"
89
+ # s << "<name>#{workitem.task_name}</name>"
90
+ # s << "<assignee>#{workitem.task_assignee}</assignee>"
91
+ # s << "<duedate>#{workitem.task_duedate}</duedate>"
92
+ # s << "</task>"
93
+ #
94
+ # # the block is supposed to 'return' a string which is the
95
+ # # effective template
96
+ # end
97
+ #
98
+ # This participant uses
99
+ # "atom-tools" from http://code.necronomicorp.com/trac/atom-tools
100
+ #
101
+ #
102
+ class AtomFeedParticipant
103
+ include MonitorMixin
104
+ include OpenWFE::LocalParticipant
105
+ include OpenWFE::TemplateMixin
106
+
107
+ #
108
+ # Made accessible so that blocks may set it.
109
+ #
110
+ attr_accessor :content_type
111
+
112
+ def initialize (max_item_count, template=nil, &block)
113
+
114
+ super() # very important as this class includes MonitorMixin
115
+
116
+ @template = template
117
+ @max_item_count = max_item_count
118
+ @block_template = block
119
+
120
+ @feed = Atom::Feed.new("http://localhost")
121
+ @content_type = "xhtml"
122
+ end
123
+
124
+ def consume (workitem)
125
+
126
+ e = Atom::Entry.new
127
+
128
+ e.id = \
129
+ "#{workitem.fei.workflow_instance_id}--" +
130
+ "#{workitem.fei.expression_id}"
131
+
132
+ e.title = workitem.atom_entry_title
133
+ e.content = render(workitem)
134
+
135
+ e.content["type"] = @content_type
136
+
137
+ @feed << e
138
+
139
+ @feed = @feed[0, @max_item_count] \
140
+ if @feed.entries.size > @max_item_count
141
+
142
+ publish workitem
143
+
144
+ reply_to_engine workitem
145
+ end
146
+
147
+ protected
148
+
149
+ #
150
+ # This base implementation simply calls the eval_template()
151
+ # method of the TemplateMixin.
152
+ # Feel free to override this render() method for custom
153
+ # representations.
154
+ #
155
+ def render (workitem)
156
+
157
+ eval_template workitem
158
+ end
159
+
160
+ #
161
+ # For the moment, just dumps the feed into a file.
162
+ #
163
+ def publish (workitem)
164
+ synchronize do
165
+ filename = "work/atom_#{workitem.participant_name}.xml"
166
+ f = File.open(filename, "w")
167
+ f << @feed.to_s
168
+ f.close()
169
+ end
170
+ end
171
+ end
172
+
173
+ end
174
+
@@ -0,0 +1,268 @@
1
+ #
2
+ #--
3
+ # Copyright (c) 2007-2008, John Mettraux, OpenWFE.org
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # . Redistributions of source code must retain the above copyright notice, this
10
+ # list of conditions and the following disclaimer.
11
+ #
12
+ # . Redistributions in binary form must reproduce the above copyright notice,
13
+ # this list of conditions and the following disclaimer in the documentation
14
+ # and/or other materials provided with the distribution.
15
+ #
16
+ # . Neither the name of the "OpenWFE" nor the names of its contributors may be
17
+ # used to endorse or promote products derived from this software without
18
+ # specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ # POSSIBILITY OF SUCH DAMAGE.
31
+ #++
32
+ #
33
+
34
+ #
35
+ # "made in Japan"
36
+ #
37
+ # John Mettraux at openwfe.org
38
+ #
39
+
40
+ #
41
+ # this participant requires atom-tools from
42
+ #
43
+ # http://code.necronomicorp.com/trac/atom-tools
44
+ #
45
+ # atom-tools' license is X11/MIT
46
+ #
47
+
48
+ require 'yaml'
49
+ require 'rexml/document'
50
+
51
+ require 'openwfe/participants/participants'
52
+
53
+ #require 'rubygems'
54
+ require 'atom/entry' # gem 'atom-tools'
55
+ require 'atom/collection'
56
+
57
+
58
+ module OpenWFE::Extras
59
+
60
+ #
61
+ # This participants posts (as in HTTP POST) a workitem
62
+ # to an AtomPub enabled resource.
63
+ #
64
+ # target_uri = "https://openwferu.wordpress.com/wp-app.php/posts"
65
+ #
66
+ # params = {}
67
+ # params[:username] = 'jmettraux'
68
+ # params[:password] = ENV['WORDPRESS_PASSWORD']
69
+ # params[:categories] = 'openwferu, test'
70
+ #
71
+ # engine.register_participant(
72
+ # "app", OpenWFE::Extras::AtomPubParticipant.new target_uri, params)
73
+ #
74
+ # This base implementation dumps workitem as YAML in the entry content.
75
+ #
76
+ # See BlogParticipant for a human-oriented blog posting participant.
77
+ #
78
+ class AtomPubParticipant
79
+ include OpenWFE::LocalParticipant
80
+
81
+ #
82
+ # The URI to post to
83
+ #
84
+ attr_accessor :target_uri
85
+
86
+ attr_accessor :author_name, :author_uri
87
+
88
+
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
106
+
107
+ #
108
+ # The incoming workitem will generate an atom entry that will
109
+ # get posted to the target URI.
110
+ #
111
+ # This consume() method returns the URI (as a String) where the
112
+ # just uploaded post can be edited.
113
+ #
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
136
+
137
+ protected
138
+
139
+ #
140
+ # This base implementation simply uses a YAML dump of the workitem
141
+ # as the post content (with a content type of 'html').
142
+ #
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
153
+
154
+ #
155
+ # This default implementation simply builds a single author
156
+ # out of the :author_name, :author_uri passed as initialization
157
+ # params.
158
+ #
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
167
+
168
+ #
169
+ # This base implementations simply adds the categories listed
170
+ # in the :categories initialization parameter.
171
+ # The target_uri is used as the scheme for the categories.
172
+ #
173
+ # You can override this method to add extra categories or to
174
+ # have completely different categories.
175
+ #
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
188
+
189
+ #
190
+ # Extracts the link of the newly created resource (newly posted blog
191
+ # entry), and returns it as a String.
192
+ #
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
205
+ end
206
+
207
+ #
208
+ # A participant that blogs.
209
+ #
210
+ # require 'openwfe/extras/participants/atompub_participants'
211
+ # include OpenWFE::Extras
212
+ #
213
+ # target_uri = "https://openwferu.wordpress.com/wp-app.php/posts"
214
+ #
215
+ # params = {}
216
+ # params[:username] = 'jeff'
217
+ # params[:password] = 'whatever'
218
+ #
219
+ # params[:categories] = 'openwferu, test'
220
+ #
221
+ # #params[:title_field] = "title"
222
+ # #
223
+ # # which workitem field will hold the post title ?
224
+ # # by default, it's "title"
225
+ #
226
+ # engine.register_participant "blogger", BlogParticipant.new(target_uri, params) do
227
+ # """
228
+ # paragraph 0
229
+ #
230
+ # paragraph 1 : ${f:message}
231
+ #
232
+ # paragraph 2
233
+ # """
234
+ # end
235
+ #
236
+ # This participant takes its template and the workitem it receives to
237
+ # publish a blog entry.
238
+ #
239
+ # The template can be specified as a block (as in the previous example)
240
+ # or via the :template parameter.
241
+ #
242
+ class BlogParticipant < AtomPubParticipant
243
+ include OpenWFE::TemplateMixin
244
+
245
+ def initialize (target_uri, params, &block)
246
+
247
+ super
248
+
249
+ @template = params[:template]
250
+ @block_template = block
251
+
252
+ @content_type = params[:content_type] || "html"
253
+
254
+ @title_field = params[:title_field] || "title"
255
+ end
256
+
257
+ protected
258
+
259
+ def render_content (entry, workitem)
260
+
261
+ entry.title = workitem.attributes[@title_field].to_s
262
+
263
+ entry.content = eval_template workitem
264
+ entry.content["type"] = @content_type
265
+ end
266
+ end
267
+ end
268
+
@@ -0,0 +1,87 @@
1
+ #
2
+ #--
3
+ # Copyright (c) 2008, John Mettraux, OpenWFE.org
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # . Redistributions of source code must retain the above copyright notice, this
10
+ # list of conditions and the following disclaimer.
11
+ #
12
+ # . Redistributions in binary form must reproduce the above copyright notice,
13
+ # this list of conditions and the following disclaimer in the documentation
14
+ # and/or other materials provided with the distribution.
15
+ #
16
+ # . Neither the name of the "OpenWFE" nor the names of its contributors may be
17
+ # used to endorse or promote products derived from this software without
18
+ # specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ # POSSIBILITY OF SUCH DAMAGE.
31
+ #++
32
+ #
33
+
34
+ #
35
+ # "made in Japan"
36
+ #
37
+ # John Mettraux at openwfe.org
38
+ #
39
+
40
+ require 'openwfe/participants/participants'
41
+ require 'openwfe/extras/misc/basecamp'
42
+
43
+
44
+ module OpenWFE
45
+ module Extras
46
+
47
+ class BasecampParticipant
48
+ include OpenWFE::LocalParticipant
49
+ include OpenWFE::TemplateMixin
50
+
51
+ def initialize (params, &block)
52
+
53
+ super()
54
+
55
+ @template = params[:template]
56
+ @block_template = block
57
+
58
+ @company_id = params[:company_id]
59
+ #@project_id = params[:project_id]
60
+
61
+ @responsible_party_id = params[:responsible_party_id]
62
+ @todo_list_id = params[:todo_list_id]
63
+
64
+ ssl = params[:ssl]
65
+ ssl = true if ssl == nil
66
+
67
+ @camp = Basecamp.new(
68
+ params[:host], params[:username], params[:password], ssl)
69
+ end
70
+
71
+ def consume (workitem)
72
+
73
+ text = workitem['todo_text'] || eval_template(wi)
74
+
75
+ resp = workitem['todo_responsible_party'] || @responsible_party_id
76
+ list = workitem['toto_list_id'] || @todo_list_id
77
+
78
+ todo = @camp.create_item list, text, resp
79
+
80
+ workitem['todo_id'] = todo.id
81
+
82
+ reply_to_engine workitem
83
+ end
84
+ end
85
+ end
86
+ end
87
+