openwferu-extras 0.9.16 → 0.9.17

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  #
2
2
  #--
3
- # Copyright (c) 2007, Tomaso Tosolini, John Mettraux, OpenWFE.org
3
+ # Copyright (c) 2007-2008, Tomaso Tosolini, John Mettraux, OpenWFE.org
4
4
  # All rights reserved.
5
5
  #
6
6
  # Redistribution and use in source and binary forms, with or without
@@ -38,11 +38,10 @@
38
38
  # John Mettraux
39
39
  #
40
40
 
41
- require 'rubygems'
41
+ #require 'rubygems'
42
42
 
43
43
  #require_gem 'activerecord'
44
- gem 'activerecord'
45
- require 'active_record'
44
+ gem 'activerecord'; require 'active_record'
46
45
 
47
46
  require 'openwfe/omixins'
48
47
  require 'openwfe/expool/errorjournal'
@@ -105,7 +104,7 @@ module OpenWFE::Extras
105
104
  # (no synchronization needed it seems)
106
105
  #
107
106
  class DbErrorJournal < OpenWFE::ErrorJournal
108
- include FeiMixin
107
+ include OpenWFE::FeiMixin
109
108
 
110
109
  def initialize (service_name, application_context)
111
110
 
@@ -123,7 +122,7 @@ module OpenWFE::Extras
123
122
  #
124
123
  def get_error_log (wfid)
125
124
 
126
- wfid = to_wfid wfid
125
+ wfid = extract_wfid wfid, true
127
126
  errors = ProcessError.find_all_by_wfid wfid, :order => "id asc"
128
127
  errors.collect { |e| e.owfe_error }
129
128
  end
@@ -1,6 +1,6 @@
1
1
  #
2
2
  #--
3
- # Copyright (c) 2007, Tomaso Tosolini, John Mettraux OpenWFE.org
3
+ # Copyright (c) 2007-2008, Tomaso Tosolini, John Mettraux OpenWFE.org
4
4
  # All rights reserved.
5
5
  #
6
6
  # Redistribution and use in source and binary forms, with or without
@@ -38,15 +38,13 @@
38
38
  # John Mettraux
39
39
  #
40
40
 
41
- require 'rubygems'
41
+ #require 'rubygems'
42
42
 
43
43
  #require_gem 'activerecord'
44
- gem 'activerecord'
45
- require 'active_record'
44
+ gem 'activerecord'; require 'active_record'
46
45
 
47
46
  require 'monitor'
48
47
 
49
-
50
48
  require 'openwfe/service'
51
49
  require 'openwfe/rudefinitions'
52
50
  require 'openwfe/expool/expstorage'
@@ -190,57 +188,137 @@ module OpenWFE::Extras
190
188
  end
191
189
 
192
190
  #
193
- # Given an expression class (Schedulable or ParticipantExpression for
194
- # example), calls the passed block for each expression of that class.
191
+ # Gather expressions matching certain parameters.
195
192
  #
196
- def each_of_kind (kind, &block)
193
+ def find_expressions (options={})
194
+
195
+ conditions = determine_conditions options
196
+ # note : this call modifies the options hash...
197
+
198
+ #
199
+ # maximize usage of SQL querying
200
+
201
+ exps = Expression.find :all, :conditions => conditions
197
202
 
198
- classes = get_expression_map.get_expression_classes(kind)
199
- classes = classes.collect { |c| c.name }
203
+ #
204
+ # do the rest of the filtering
200
205
 
201
- exps = Expression.find_all_by_exp_class(classes)
206
+ exps = exps.collect do |exp|
207
+ as_owfe_expression exp
208
+ end
202
209
 
203
- exp_each exps, &block
210
+ exps.find_all do |fexp|
211
+ does_match? options, fexp
212
+ end
204
213
  end
205
214
 
206
215
  #
207
- # Calls the block for each expression stored. If a wfid prefix is
208
- # passed, only the expressions whose workflow instance id begins
209
- # with it will be iterated over.
216
+ # Fetches the root of a process instance.
210
217
  #
211
- def each (wfid_prefix=nil, &block)
218
+ def fetch_root (wfid)
212
219
 
213
220
  params = {}
214
221
 
215
- params[:conditions] = [ "wfid LIKE ?", "#{wfid_prefix}%" ] \
216
- if wfid_prefix
222
+ params[:conditions] = [
223
+ "wfid = ? AND exp_class = ?", wfid, DefineExpression.to_s
224
+ ]
217
225
 
218
226
  exps = Expression.find(:all, params)
219
227
 
220
- exp_each exps, &block
221
- end
228
+ e = exps.sort { |fe1, fe2| fe1.fei.expid <=> fe2.fei.expid }[0]
229
+ #
230
+ # find the one with the smallest expid
222
231
 
223
- alias :real_each :each
232
+ as_owfe_expression e
233
+ end
224
234
 
225
235
  protected
226
236
 
227
237
  #
228
- # Used by each() and each_of_kind().
238
+ # Grabs the options to build a conditions array for use by
239
+ # find().
240
+ #
241
+ # Note : this method, modifies the options hash (it removes
242
+ # the args it needs).
229
243
  #
230
- def exp_each (expressions, &block)
244
+ def determine_conditions (options)
245
+
246
+ wfid = options.delete :wfid
247
+ wfid_prefix = options.delete :wfid_prefix
248
+ #parent_wfid = options.delete :parent_wfid
249
+
250
+ query = []
251
+ conditions = []
252
+
253
+ if wfid
254
+ query << "wfid = ?"
255
+ conditions << wfid
256
+ elsif wfid_prefix
257
+ query << "wfid LIKE ?"
258
+ conditions << "#{wfid_prefix}%"
259
+ end
260
+
261
+ add_class_conditions options, query, conditions
262
+
263
+ conditions = conditions.flatten
231
264
 
232
- expressions.each do |exp|
233
- fexp = as_owfe_expression(exp)
234
- block.call fexp.fei, fexp
265
+ if conditions.size < 1
266
+ nil
267
+ else
268
+ conditions.insert 0, query.join(" AND ")
235
269
  end
236
270
  end
237
271
 
272
+ #
273
+ # Used by determine_conditions().
274
+ #
275
+ def add_class_conditions (options, query, conditions)
276
+
277
+ ic = options.delete :include_classes
278
+ ic = Array(ic)
279
+
280
+ ec = options.delete :exclude_classes
281
+ ec = Array(ec)
282
+
283
+ acc ic, query, conditions, "OR"
284
+ acc ec, query, conditions, "AND"
285
+ end
286
+
287
+ def acc (classes, query, conditions, join)
288
+
289
+ return if classes.size < 1
290
+
291
+ classes = classes.collect do |kind|
292
+ get_expression_map.get_expression_classes kind
293
+ end
294
+ classes = classes.flatten
295
+
296
+ quer = []
297
+ cond = []
298
+ classes.each do |cl|
299
+
300
+ quer << if join == "AND"
301
+ "exp_class != ?"
302
+ else
303
+ "exp_class = ?"
304
+ end
305
+
306
+ cond << cl.to_s
307
+ end
308
+ quer = quer.join " #{join} "
309
+
310
+ query << "(#{quer})"
311
+ conditions << cond
312
+ end
313
+
238
314
  #
239
315
  # Extracts the OpenWFE FlowExpression instance from the
240
316
  # active record and makes sure its application_context is set.
241
317
  #
242
318
  def as_owfe_expression (record)
243
319
 
320
+ return nil unless record
321
+
244
322
  fe = record.svalue
245
323
  fe.application_context = @application_context
246
324
  fe
@@ -1,6 +1,6 @@
1
1
  #
2
2
  #--
3
- # Copyright (c) 2007, John Mettraux, OpenWFE.org
3
+ # Copyright (c) 2007-2008, John Mettraux, OpenWFE.org
4
4
  # All rights reserved.
5
5
  #
6
6
  # Redistribution and use in source and binary forms, with or without
@@ -42,15 +42,12 @@ require 'base64'
42
42
  require 'monitor'
43
43
 
44
44
  require 'openwfe/service'
45
- require 'openwfe/util/scheduler'
46
45
  require 'openwfe/listeners/listener'
47
46
 
48
- require 'openwfe/extras/util/sqs'
47
+ #require 'rubygems'
48
+ require 'rufus/sqs' # gem 'rufus-sqs'
49
49
 
50
50
 
51
- #
52
- # some base listener implementations
53
- #
54
51
  module OpenWFE
55
52
  module Extras
56
53
 
@@ -69,9 +66,10 @@ module Extras
69
66
  # # every 2 minutes and 30 seconds
70
67
  #
71
68
  class SqsListener < Service
69
+
72
70
  include MonitorMixin
73
71
  include WorkItemListener
74
- include Schedulable
72
+ include Rufus::Schedulable
75
73
 
76
74
  #
77
75
  # The name of the Amazon SQS whom this listener cares for
@@ -84,23 +82,22 @@ module Extras
84
82
 
85
83
  service_name = "#{self.class}::#{@queue_name}"
86
84
 
87
- super(service_name, application_context)
85
+ super service_name, application_context
88
86
 
89
87
  linfo { "new() queue is '#{@queue_name}'" }
90
88
  end
91
89
 
92
90
  #
93
- # Will 'find' files in the work directory (by default ./work/in/),
94
- # extract the workitem in them and feed it back to the engine.
91
+ # polls the SQS for incoming messages
95
92
  #
96
93
  def trigger (params)
97
94
  synchronize do
98
95
 
99
96
  ldebug { "trigger()" }
100
97
 
101
- qs = SQS::QueueService.new
98
+ qs = Rufus::SQS::QueueService.new
102
99
 
103
- qs.create_queue(@queue_name)
100
+ qs.create_queue @queue_name
104
101
  # just to be sure it is there
105
102
 
106
103
  while true
@@ -112,7 +109,7 @@ module Extras
112
109
 
113
110
  l.each do |msg|
114
111
 
115
- o = decode_object(msg)
112
+ o = decode_object msg
116
113
 
117
114
  handle_item o
118
115
 
@@ -137,9 +134,9 @@ module Extras
137
134
  #
138
135
  def decode_object (message)
139
136
 
140
- o = Base64.decode64(message.message_body)
141
- o = YAML.load(o)
142
- o = OpenWFE::workitem_from_h(o)
137
+ o = Base64.decode64 message.message_body
138
+ o = YAML.load o
139
+ o = OpenWFE::workitem_from_h o
143
140
  o
144
141
  end
145
142
  end
@@ -1,6 +1,6 @@
1
1
  #
2
2
  #--
3
- # Copyright (c) 2007, John Mettraux, OpenWFE.org
3
+ # Copyright (c) 2007-2008, John Mettraux, OpenWFE.org
4
4
  # All rights reserved.
5
5
  #
6
6
  # Redistribution and use in source and binary forms, with or without
@@ -47,23 +47,8 @@
47
47
 
48
48
  #require 'monitor'
49
49
 
50
- require 'rubygems'
51
-
52
- begin
53
- require 'atom/collection'
54
- rescue LoadError
55
- #
56
- # soft dependency on 'atom-tools'
57
- #
58
- puts
59
- puts
60
- puts "'atom/collection' is missing. You can install with :"
61
- puts
62
- puts " [sudo] gem install atom-tools"
63
- puts
64
- puts
65
- exit 1
66
- end
50
+ #require 'rubygems'
51
+ require 'atom/collection' # gem 'atoom-tools'
67
52
 
68
53
  require 'openwfe/service'
69
54
 
@@ -107,8 +92,8 @@ module OpenWFE::Extras
107
92
  #
108
93
  class ActivityFeedService
109
94
  #include MonitorMixin
110
- include ServiceMixin
111
- include OwfeServiceLocator
95
+ include OpenWFE::ServiceMixin
96
+ include OpenWFE::OwfeServiceLocator
112
97
 
113
98
 
114
99
  attr_accessor :max_item_count
@@ -140,6 +125,8 @@ module OpenWFE::Extras
140
125
  #
141
126
  def call (channel, *args)
142
127
 
128
+ ldebug "call() c '#{channel}' entries count : #{@entries.size}"
129
+
143
130
  e = Entry.new
144
131
 
145
132
  e.participant_name = channel
@@ -0,0 +1,485 @@
1
+
2
+ #
3
+ # This code was written by 37signals.com
4
+ #
5
+ # The original is at :
6
+ # http://developer.37signals.com/basecamp/basecamp.rb
7
+ #
8
+
9
+ require 'net/https'
10
+ require 'yaml'
11
+ require 'date'
12
+ require 'time'
13
+
14
+ #require 'rubygems'
15
+ gem 'xml-simple'; require 'xmlsimple'
16
+
17
+ # An interface to the Basecamp web-services API. Usage is straightforward:
18
+ #
19
+ # session = Basecamp.new('your.basecamp.com', 'username', 'password')
20
+ # puts "projects: #{session.projects.length}"
21
+ class Basecamp #:nodoc:
22
+
23
+ # A wrapper to encapsulate the data returned by Basecamp, for easier access.
24
+ class Record #:nodoc:
25
+ attr_reader :type
26
+
27
+ def initialize(type, hash)
28
+ @type = type
29
+ @hash = hash
30
+ end
31
+
32
+ def [](name)
33
+ name = dashify(name)
34
+ case @hash[name]
35
+ when Hash then
36
+ @hash[name] = (@hash[name].keys.length == 1 && Array === @hash[name].values.first) ?
37
+ @hash[name].values.first.map { |v| Record.new(@hash[name].keys.first, v) } :
38
+ Record.new(name, @hash[name])
39
+ else @hash[name]
40
+ end
41
+ end
42
+
43
+ def id
44
+ @hash["id"]
45
+ end
46
+
47
+ def attributes
48
+ @hash.keys
49
+ end
50
+
51
+ def respond_to?(sym)
52
+ super || @hash.has_key?(dashify(sym))
53
+ end
54
+
55
+ def method_missing(sym, *args)
56
+ if args.empty? && !block_given? && respond_to?(sym)
57
+ self[sym]
58
+ else
59
+ super
60
+ end
61
+ end
62
+
63
+ def to_s
64
+ "\#<Record(#{@type}) #{@hash.inspect[1..-2]}>"
65
+ end
66
+
67
+ def inspect
68
+ to_s
69
+ end
70
+
71
+ private
72
+
73
+ def dashify(name)
74
+ name.to_s.tr("_", "-")
75
+ end
76
+ end
77
+
78
+ # A wrapper to represent a file that should be uploaded. This is used so that
79
+ # the form/multi-part encoder knows when to encode a field as a file, versus
80
+ # when to encode it as a simple field.
81
+ class FileUpload #:nodoc:
82
+ attr_reader :filename, :content
83
+
84
+ def initialize(filename, content)
85
+ @filename = filename
86
+ @content = content
87
+ end
88
+ end
89
+
90
+ attr_accessor :use_xml
91
+
92
+ # Connects
93
+ def initialize(url, user_name, password, use_ssl = false)
94
+ @use_xml = false
95
+ @user_name, @password = user_name, password
96
+ connect!(url, use_ssl)
97
+ end
98
+
99
+ # Return the list of all accessible projects.
100
+ def projects
101
+ records "project", "/project/list"
102
+ end
103
+
104
+ # Returns the list of message categories for the given project
105
+ def message_categories(project_id)
106
+ records "post-category", "/projects/#{project_id}/post_categories"
107
+ end
108
+
109
+ # Returns the list of file categories for the given project
110
+ def file_categories(project_id)
111
+ records "attachment-category", "/projects/#{project_id}/attachment_categories"
112
+ end
113
+
114
+ # Return information for the company with the given id
115
+ def company(id)
116
+ record "/contacts/company/#{id}"
117
+ end
118
+
119
+ # Return an array of the people in the given company. If the project-id is
120
+ # given, only people who have access to the given project will be returned.
121
+ def people(company_id, project_id=nil)
122
+ url = project_id ? "/projects/#{project_id}" : ""
123
+ url << "/contacts/people/#{company_id}"
124
+ records "person", url
125
+ end
126
+
127
+ # Return information about the person with the given id
128
+ def person(id)
129
+ record "/contacts/person/#{id}"
130
+ end
131
+
132
+ # Return information about the message(s) with the given id(s). The API
133
+ # limits you to requesting 25 messages at a time, so if you need to get more
134
+ # than that, you'll need to do it in multiple requests.
135
+ def message(*ids)
136
+ result = records("post", "/msg/get/#{ids.join(",")}")
137
+ result.length == 1 ? result.first : result
138
+ end
139
+
140
+ # Returns a summary of all messages in the given project (and category, if
141
+ # specified). The summary is simply the title and category of the message,
142
+ # as well as the number of attachments (if any).
143
+ def message_list(project_id, category_id=nil)
144
+ url = "/projects/#{project_id}/msg"
145
+ url << "/cat/#{category_id}" if category_id
146
+ url << "/archive"
147
+
148
+ records "post", url
149
+ end
150
+
151
+ # Create a new message in the given project. The +message+ parameter should
152
+ # be a hash. The +email_to+ parameter must be an array of person-id's that
153
+ # should be notified of the post.
154
+ #
155
+ # If you want to add attachments to the message, the +attachments+ parameter
156
+ # should be an array of hashes, where each has has a :name key (optional),
157
+ # and a :file key (required). The :file key must refer to a Basecamp::FileUpload
158
+ # instance.
159
+ #
160
+ # msg = session.post_message(158141,
161
+ # { :title => "Requirements",
162
+ # :body => "Here are the requirements documents you asked for.",
163
+ # :category_id => 2301121 },
164
+ # [john.id, martha.id],
165
+ # [ { :name => "Primary Requirements",
166
+ # :file => Basecamp::FileUpload.new('primary.doc", File.read('primary.doc')) },
167
+ # { :file => Basecamp::FileUpload.new('other.doc', File.read('other.doc')) } ])
168
+ def post_message(project_id, message, notify=[], attachments=[])
169
+ prepare_attachments(attachments)
170
+ record "/projects/#{project_id}/msg/create",
171
+ :post => message,
172
+ :notify => notify,
173
+ :attachments => attachments
174
+ end
175
+
176
+ # Edit the message with the given id. The +message+ parameter should
177
+ # be a hash. The +email_to+ parameter must be an array of person-id's that
178
+ # should be notified of the post.
179
+ #
180
+ # The +attachments+ parameter, if used, should be the same as described for
181
+ # #post_message.
182
+ def update_message(id, message, notify=[], attachments=[])
183
+ prepare_attachments(attachments)
184
+ record "/msg/update/#{id}",
185
+ :post => message,
186
+ :notify => notify,
187
+ :attachments => attachments
188
+ end
189
+
190
+ # Deletes the message with the given id, and returns it.
191
+ def delete_message(id)
192
+ record "/msg/delete/#{id}"
193
+ end
194
+
195
+ # Return a list of the comments for the specified message.
196
+ def comments(post_id)
197
+ records "comment", "/msg/comments/#{post_id}"
198
+ end
199
+
200
+ # Retrieve a specific comment
201
+ def comment(id)
202
+ record "/msg/comment/#{id}"
203
+ end
204
+
205
+ # Add a new comment to a message. +comment+ must be a hash describing the
206
+ # comment. You can add attachments to the comment, too, by giving them in
207
+ # an array. See the #post_message method for a description of how to do that.
208
+ def create_comment(post_id, comment, attachments=[])
209
+ prepare_attachments(attachments)
210
+ record "/msg/create_comment", :comment => comment.merge(:post_id => post_id),
211
+ :attachments => attachments
212
+ end
213
+
214
+ # Update the given comment. Attachments follow the same format as #post_message.
215
+ def update_comment(id, comment, attachments=[])
216
+ prepare_attachments(attachments)
217
+ record "/msg/update_comment", :comment_id => id,
218
+ :comment => comment, :attachments => attachments
219
+ end
220
+
221
+ # Deletes (and returns) the given comment.
222
+ def delete_comment(id)
223
+ record "/msg/delete_comment/#{id}"
224
+ end
225
+
226
+ # =========================================================================
227
+ # TODO LISTS AND ITEMS
228
+ # =========================================================================
229
+
230
+ # Marks the given item completed.
231
+ def complete_item(id)
232
+ record "/todos/complete_item/#{id}"
233
+ end
234
+
235
+ # Marks the given item uncompleted.
236
+ def uncomplete_item(id)
237
+ record "/todos/uncomplete_item/#{id}"
238
+ end
239
+
240
+ # Creates a new to-do item.
241
+ def create_item(list_id, content, responsible_party=nil, notify=true)
242
+ record "/todos/create_item/#{list_id}",
243
+ :content => content, :responsible_party => responsible_party,
244
+ :notify => notify
245
+ end
246
+
247
+ # Creates a new list using the given hash of list metadata.
248
+ def create_list(project_id, list)
249
+ record "/projects/#{project_id}/todos/create_list", list
250
+ end
251
+
252
+ # Deletes the given item from it's parent list.
253
+ def delete_item(id)
254
+ record "/todos/delete_item/#{id}"
255
+ end
256
+
257
+ # Deletes the given list and all of its items.
258
+ def delete_list(id)
259
+ record "/todos/delete_list/#{id}"
260
+ end
261
+
262
+ # Retrieves the specified list, and all of its items.
263
+ def get_list(id)
264
+ record "/todos/list/#{id}"
265
+ end
266
+
267
+ # Return all lists for a project. If complete is true, only completed lists
268
+ # are returned. If complete is false, only uncompleted lists are returned.
269
+ def lists(project_id, complete=nil)
270
+ records "todo-list", "/projects/#{project_id}/todos/lists", :complete => complete
271
+ end
272
+
273
+ # Repositions an item to be at the given position in its list
274
+ def move_item(id, to)
275
+ record "/todos/move_item/#{id}", :to => to
276
+ end
277
+
278
+ # Repositions a list to be at the given position in its project
279
+ def move_list(id, to)
280
+ record "/todos/move_list/#{id}", :to => to
281
+ end
282
+
283
+ # Updates the given item
284
+ def update_item(id, content, responsible_party=nil, notify=true)
285
+ record "/todos/update_item/#{id}",
286
+ :item => { :content => content }, :responsible_party => responsible_party,
287
+ :notify => notify
288
+ end
289
+
290
+ # Updates the given list's metadata
291
+ def update_list(id, list)
292
+ record "/todos/update_list/#{id}", :list => list
293
+ end
294
+
295
+ # =========================================================================
296
+ # MILESTONES
297
+ # =========================================================================
298
+
299
+ # Complete the milestone with the given id
300
+ def complete_milestone(id)
301
+ record "/milestones/complete/#{id}"
302
+ end
303
+
304
+ # Create a new milestone for the given project. +data+ must be hash of the
305
+ # values to set, including +title+, +deadline+, +responsible_party+, and
306
+ # +notify+.
307
+ def create_milestone(project_id, data)
308
+ create_milestones(project_id, [data]).first
309
+ end
310
+
311
+ # As #create_milestone, but can create multiple milestones in a single
312
+ # request. The +milestones+ parameter must be an array of milestone values as
313
+ # descrbed in #create_milestone.
314
+ def create_milestones(project_id, milestones)
315
+ records "milestone", "/projects/#{project_id}/milestones/create", :milestone => milestones
316
+ end
317
+
318
+ # Destroys the milestone with the given id.
319
+ def delete_milestone(id)
320
+ record "/milestones/delete/#{id}"
321
+ end
322
+
323
+ # Returns a list of all milestones for the given project, optionally filtered
324
+ # by whether they are completed, late, or upcoming.
325
+ def milestones(project_id, find="all")
326
+ records "milestone", "/projects/#{project_id}/milestones/list", :find => find
327
+ end
328
+
329
+ # Uncomplete the milestone with the given id
330
+ def uncomplete_milestone(id)
331
+ record "/milestones/uncomplete/#{id}"
332
+ end
333
+
334
+ # Updates an existing milestone.
335
+ def update_milestone(id, data, move=false, move_off_weekends=false)
336
+ record "/milestones/update/#{id}", :milestone => data,
337
+ :move_upcoming_milestones => move,
338
+ :move_upcoming_milestones_off_weekends => move_off_weekends
339
+ end
340
+
341
+ # Make a raw web-service request to Basecamp. This will return a Hash of
342
+ # Arrays of the response, and may seem a little odd to the uninitiated.
343
+ def request(path, parameters = {}, second_try = false)
344
+ response = post(path, convert_body(parameters), "Content-Type" => content_type)
345
+
346
+ if response.code.to_i / 100 == 2
347
+ result = XmlSimple.xml_in(response.body, 'keeproot' => true,
348
+ 'contentkey' => '__content__', 'forcecontent' => true)
349
+ typecast_value(result)
350
+ elsif response.code == "302" && !second_try
351
+ connect!(@url, !@use_ssl)
352
+ request(path, parameters, true)
353
+ else
354
+ raise "#{response.message} (#{response.code})"
355
+ end
356
+ end
357
+
358
+ # A convenience method for wrapping the result of a query in a Record
359
+ # object. This assumes that the result is a singleton, not a collection.
360
+ def record(path, parameters={})
361
+ result = request(path, parameters)
362
+ (result && !result.empty?) ? Record.new(result.keys.first, result.values.first) : nil
363
+ end
364
+
365
+ # A convenience method for wrapping the result of a query in Record
366
+ # objects. This assumes that the result is a collection--any singleton
367
+ # result will be wrapped in an array.
368
+ def records(node, path, parameters={})
369
+ result = request(path, parameters).values.first or return []
370
+ result = result[node] or return []
371
+ result = [result] unless Array === result
372
+ result.map { |row| Record.new(node, row) }
373
+ end
374
+
375
+ private
376
+
377
+ def connect!(url, use_ssl)
378
+ @use_ssl = use_ssl
379
+ @url = url
380
+ @connection = Net::HTTP.new(url, use_ssl ? 443 : 80)
381
+ @connection.use_ssl = @use_ssl
382
+ @connection.verify_mode = OpenSSL::SSL::VERIFY_NONE if @use_ssl
383
+ end
384
+
385
+ def convert_body(body)
386
+ body = use_xml ? body.to_xml : body.to_yaml
387
+ end
388
+
389
+ def content_type
390
+ use_xml ? "application/xml" : "application/x-yaml"
391
+ end
392
+
393
+ def post(path, body, header={})
394
+ request = Net::HTTP::Post.new(path, header.merge('Accept' => 'application/xml'))
395
+ request.basic_auth(@user_name, @password)
396
+ @connection.request(request, body)
397
+ end
398
+
399
+ def store_file(contents)
400
+ response = post("/upload", contents, 'Content-Type' => 'application/octet-stream',
401
+ 'Accept' => 'application/xml')
402
+
403
+ if response.code == "200"
404
+ result = XmlSimple.xml_in(response.body, 'keeproot' => true, 'forcearray' => false)
405
+ return result["upload"]["id"]
406
+ else
407
+ raise "Could not store file: #{response.message} (#{response.code})"
408
+ end
409
+ end
410
+
411
+ def typecast_value(value)
412
+ case value
413
+ when Hash
414
+ if value.has_key?("__content__")
415
+ content = translate_entities(value["__content__"]).strip
416
+ case value["type"]
417
+ when "integer" then content.to_i
418
+ when "boolean" then content == "true"
419
+ when "datetime" then Time.parse(content)
420
+ when "date" then Date.parse(content)
421
+ else content
422
+ end
423
+ # a special case to work-around a bug in XmlSimple. When you have an empty
424
+ # tag that has an attribute, XmlSimple will not add the __content__ key
425
+ # to the returned hash. Thus, we check for the presense of the 'type'
426
+ # attribute to look for empty, typed tags, and simply return nil for
427
+ # their value.
428
+ elsif value.keys == %w(type)
429
+ nil
430
+ elsif value["nil"] == "true"
431
+ nil
432
+ # another special case, introduced by the latest rails, where an array
433
+ # type now exists. This is parsed by XmlSimple as a two-key hash, where
434
+ # one key is 'type' and the other is the actual array value.
435
+ elsif value.keys.length == 2 && value["type"] == "array"
436
+ value.delete("type")
437
+ typecast_value(value)
438
+ else
439
+ value.empty? ? nil : value.inject({}) do |h,(k,v)|
440
+ h[k] = typecast_value(v)
441
+ h
442
+ end
443
+ end
444
+ when Array
445
+ value.map! { |i| typecast_value(i) }
446
+ case value.length
447
+ when 0 then nil
448
+ when 1 then value.first
449
+ else value
450
+ end
451
+ else
452
+ raise "can't typecast #{value.inspect}"
453
+ end
454
+ end
455
+
456
+ def translate_entities(value)
457
+ value.gsub(/&lt;/, "<").
458
+ gsub(/&gt;/, ">").
459
+ gsub(/&quot;/, '"').
460
+ gsub(/&apos;/, "'").
461
+ gsub(/&amp;/, "&")
462
+ end
463
+
464
+ def prepare_attachments(list)
465
+ (list || []).each do |data|
466
+ upload = data[:file]
467
+ id = store_file(upload.content)
468
+ data[:file] = { :file => id,
469
+ :content_type => "application/octet-stream",
470
+ :original_filename => upload.filename }
471
+ end
472
+ end
473
+ end
474
+
475
+ # A minor hack to let Xml-Simple serialize symbolic keys in hashes
476
+ #class Symbol
477
+ # def [](*args)
478
+ # to_s[*args]
479
+ # end
480
+ #end
481
+ #class Hash
482
+ # def to_xml
483
+ # XmlSimple.xml_out({:request => self}, 'keeproot' => true, 'noattr' => true)
484
+ # end
485
+ #end