veritable 0.1.5.35 → 0.1.6.36

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.txt CHANGED
@@ -1,3 +1,6 @@
1
+ veritable-ruby 0.1.6 - August 24, 2012
2
+ * Initial support for group operations
3
+
1
4
  veritable-ruby 0.1.5 - July 18, 2012
2
5
  * Updated for new analysis.similar_to return format
3
6
 
data/lib/veritable/api.rb CHANGED
@@ -594,6 +594,45 @@ module Veritable
594
594
  end
595
595
  end
596
596
 
597
+ # Get a grouping for a particular column.
598
+ # If no grouping is currently running, this will create it.
599
+ #
600
+ # ==== Arguments
601
+ # * +column_id+ -- the name of the column along which to group rows
602
+ #
603
+ # ==== Returns
604
+ # A Grouping instance for the column id
605
+ #
606
+ # See also: https://dev.priorknowledge.com/docs/client/ruby
607
+ def grouping(column_id)
608
+ return (groupings([column_id]).to_a)[0]
609
+ end
610
+
611
+ # Gets groupings for a list of columns.
612
+ # If corresponding groupings are not currently running, this will create them.
613
+ #
614
+ # ==== Arguments
615
+ # * +column_ids+ -- an array of column names to create groupings for
616
+ #
617
+ # ==== Returns
618
+ # An Enumerator over Grouping instances
619
+ #
620
+ # See also: https://dev.priorknowledge.com/docs/client/ruby
621
+ def groupings(column_ids)
622
+ update if running?
623
+ if succeeded?
624
+ doc = post(link('group'), {:columns => column_ids}.update(@opts))
625
+ return doc['groupings'].to_a.map {|g| Grouping.new(@opts, g)}
626
+ elsif running?
627
+ raise VeritableError.new("Grouping -- Analysis with id #{_id} is still running and not yet ready to calculate groupings.")
628
+ elsif failed?
629
+ raise VeritableError.new("Grouping -- Analysis with id #{_id} has failed and cannot calculate groupings.")
630
+ else
631
+ raise VeritableError.new("Grouping -- Shouldn't be here -- please let us know at support@priorknowledge.com.")
632
+ end
633
+ end
634
+
635
+
597
636
  # Returns a string representation of the analysis resource
598
637
  def inspect; to_s; end
599
638
 
@@ -1026,4 +1065,167 @@ module Veritable
1026
1065
  end
1027
1066
  end
1028
1067
  end
1068
+
1069
+ # Represents the resources associated with a grouping resource.
1070
+ # Lets you get the status of a grouping and get information about discovered groups.
1071
+ #
1072
+ # ==== Attributes
1073
+ # * +column_id+ -- the column id of this grouping
1074
+ # * +state+ -- the state of the grouping operation, one of <tt>["running", "succeeded", "failed"]</tt>
1075
+ # * +running?+ -- +true+ if +state+ is <tt>"running"</tt>
1076
+ # * +succeeded?+ -- ++true+ if +state+ is <tt>"succeeded"</tt>
1077
+ # * +failed?+ -- +true+ if +state+ is <tt>"failed"</tt>
1078
+ #
1079
+ # ==== Methods
1080
+ # * +update+ -- refreshes the local representation of the grouping resource
1081
+ # * +wait+ -- blocks until the grouping succeeds or fails
1082
+ # * +groups+ -- gets an iterator over all groups in the grouping
1083
+ # * +rows+ -- get rows and confidence information for a particular group
1084
+ # * +row+ -- get group information for a particular row
1085
+ #
1086
+ # See also: https://dev.priorknowledge.com/docs/client/ruby
1087
+ class Grouping
1088
+ include VeritableResource
1089
+
1090
+ # The column id of this grouping
1091
+ def column_id; @doc['column_name']; end
1092
+
1093
+ # The state of the analysis
1094
+ #
1095
+ # One of <tt>["running", "succeeded", "failed"]</tt>
1096
+ def state; @doc['state']; end
1097
+
1098
+ # +true+ if +state+ is <tt>"running"</tt>, otherwise +false+
1099
+ def running?; state == 'running'; end
1100
+
1101
+ # +true+ if +state+ is <tt>"succeeded"</tt>, otherwise +false+
1102
+ def succeeded?; state == 'succeeded'; end
1103
+
1104
+ # +true+ if +state+ is <tt>"failed"</tt>, otherwise +false+
1105
+ def failed?; state == 'failed'; end
1106
+
1107
+ # Refreshes the local representation of the grouping
1108
+ #
1109
+ # ==== Returns
1110
+ # +nil+ on success
1111
+ #
1112
+ # See also: https://dev.priorknowledge.com/docs/client/ruby
1113
+ def update; @doc = get(link('self')); nil; end
1114
+
1115
+ # Blocks until the grouping succeeds or fails
1116
+ #
1117
+ # ==== Arguments
1118
+ # * +max_time+ -- the maximum time to wait, in seconds. Default is +nil+, in which case the method will wait indefinitely.
1119
+ # * +poll+ -- the number of seconds to wait between polling the API server. Default is +2+.
1120
+ #
1121
+ # ==== Returns
1122
+ # +nil+ on success.
1123
+ #
1124
+ # See also: https://dev.priorknowledge.com/docs/client/ruby
1125
+ def wait(max_time=nil, poll=2)
1126
+ elapsed = 0
1127
+ while running?
1128
+ sleep poll
1129
+ if not max_time.nil?
1130
+ elapsed += poll
1131
+ if elapsed > max_time
1132
+ raise VeritableError.new("Wait for grouping -- Maximum time of #{max_time} second exceeded.")
1133
+ end
1134
+ end
1135
+ update
1136
+ end
1137
+ end
1138
+
1139
+
1140
+ # Gets all groups in the grouping
1141
+ #
1142
+ # ==== Arguments
1143
+ # * +opts+ A Hash optionally containing the keys
1144
+ # - <tt>"start"</tt> -- the group id from which the cursor should begin returning results. Defaults to +nil+, in which case the cursor will return result starting with the lexicographically first group id.
1145
+ # - <tt>"limit"</tt> -- the total number of results to return (must be a Fixnum). Defaults to +nil+, in which case the number of results returned will not be limited.
1146
+ #
1147
+ # ==== Returns
1148
+ # A Veritable::Cursor. The cursor will return group ids in lexicographic order.
1149
+ #
1150
+ # See also: https://dev.priorknowledge.com/docs/client/ruby
1151
+ def groups(opts={'start' => nil, 'limit' => nil})
1152
+ update if running?
1153
+ if succeeded?
1154
+ return Cursor.new({'collection' => link('groups'),
1155
+ 'start' => opts['start'],
1156
+ 'limit' => opts['limit']}.update(@opts)) { |g| g['group_id'] }
1157
+ elsif running?
1158
+ raise VeritableError.new("Grouping on column #{column_id} is still running and not yet ready to return groups.")
1159
+ elsif failed?
1160
+ raise VeritableError.new("Grouping on column #{column_id} has failed and cannot return groups.")
1161
+ else
1162
+ raise VeritableError.new("Grouping -- Shouldn't be here -- please let us know at support@priorknowledge.com.")
1163
+ end
1164
+ end
1165
+
1166
+ # Get rows and confidence information for a particular group.
1167
+ #
1168
+ # ==== Arguments
1169
+ # * +opts+ A Hash optionally containing the keys
1170
+ # - <tt>"group_id"</tt> -- The id of the group of interest. If nil (default), returns all rows in the table
1171
+ # - <tt>"return_data"</tt> -- If true, return row data values along with group assignment and confidence info (default: True).
1172
+ # - <tt>"start"</tt> -- the integer index from which the cursor should begin returning results. Defaults to +nil+, in which case the cursor will return result starting with the first row.
1173
+ # - <tt>"limit"</tt> -- the total number of results to return (must be a Fixnum). Defaults to +nil+, in which case the number of rows returned will not be limited.
1174
+ #
1175
+ # ==== Returns
1176
+ # A Veritable::Cursor. The cursor will return rows from the group.
1177
+ #
1178
+ # See also: https://dev.priorknowledge.com/docs/client/ruby
1179
+ def rows(opts={'group_id' => nil, 'return_data' => true, 'start' => nil, 'limit' => nil})
1180
+ update if running?
1181
+ if succeeded?
1182
+ if not opts['group_id'].nil?
1183
+ collection = link('groups') + '/' + opts['group_id'].to_s
1184
+ else
1185
+ collection = link('rows')
1186
+ end
1187
+ return Cursor.new({'collection' => collection,
1188
+ 'start' => opts['start'],
1189
+ 'limit' => opts['limit'],
1190
+ 'extra_args' => {:return_data => opts['return_data']}}.update(@opts))
1191
+ elsif running?
1192
+ raise VeritableError.new("Grouping on column #{column_id} is still running and not yet ready to return groups.")
1193
+ elsif failed?
1194
+ raise VeritableError.new("Grouping on column #{column_id} has failed and cannot return groups.")
1195
+ else
1196
+ raise VeritableError.new("Grouping -- Shouldn't be here -- please let us know at support@priorknowledge.com.")
1197
+ end
1198
+ end
1199
+
1200
+
1201
+ # Get group information for a particular row.
1202
+ #
1203
+ # ==== Arguments
1204
+ # * +target_row+ The row hash of interest. The row hash must contain an _id field. All other fields will be ignored.
1205
+ # * +opts+ A Hash optionally containing the keys
1206
+ # - <tt>"return_data"</tt> -- If true, return row data values along with group assignment and confidence info (default: True).
1207
+ #
1208
+ # ==== Returns
1209
+ # A row data hash with group_id and confidence specified in the _group_id and _confidence keys respectively.
1210
+ #
1211
+ # See also: https://dev.priorknowledge.com/docs/client/ruby
1212
+ def row(target_row, opts={'return_data' => true})
1213
+ update if running?
1214
+ if succeeded?
1215
+ row_id = target_row['_id']
1216
+ res = get(link('rows') +'/'+row_id, params={:return_data => opts['return_data']})
1217
+ return res['row']
1218
+ elsif running?
1219
+ raise VeritableError.new("Grouping on column #{column_id} is still running and not yet ready to return groups.")
1220
+ elsif failed?
1221
+ raise VeritableError.new("Grouping on column #{column_id} has failed and cannot return groups.")
1222
+ else
1223
+ raise VeritableError.new("Grouping -- Shouldn't be here -- please let us know at support@priorknowledge.com.")
1224
+ end
1225
+ end
1226
+
1227
+ end
1228
+
1229
+
1230
+
1029
1231
  end
@@ -19,6 +19,9 @@ module Veritable
19
19
  # Wraps the HTTP GET logic
20
20
  def get(url, params=nil, headers={})
21
21
  if params and params.count > 0
22
+ params.keys.to_a.each {|k|
23
+ params.delete(k) if params[k].nil?
24
+ }
22
25
  query_string = Util.query_params(params)
23
26
  url += "?#{query_string}"
24
27
  end
@@ -19,10 +19,10 @@ module Veritable
19
19
  super(opts, doc)
20
20
 
21
21
  require_opts 'collection'
22
- default_opts({'per_page' => 100})
22
+ default_opts({'per_page' => 100, 'extra_args' => {}})
23
23
 
24
24
  collection_key = collection.split("/")[-1]
25
- @doc = get(collection, params={:count => per_page, :start => start})
25
+ @doc = get(collection, params={:count => per_page, :start => start}.update(@opts['extra_args']))
26
26
  @doc.has_key?(collection_key) ? @opts['key'] = collection_key : @opts['key'] = 'data'
27
27
  @opts['lazymap'] = lazymap if lazymap
28
28
  end
@@ -63,7 +63,7 @@ module Veritable
63
63
  elsif last_page?
64
64
  return 0
65
65
  else
66
- @doc = get(collection, params={:count => per_page, :start => start})
66
+ @doc = get(collection, params={:count => per_page, :start => start}.update(@opts['extra_args']))
67
67
  end
68
68
  return data.length
69
69
  end
@@ -356,8 +356,8 @@ module Veritable
356
356
 
357
357
  private
358
358
 
359
- COUNT_LIMIT = 100000
360
-
359
+ COUNT_LIMIT = 100000
360
+
361
361
  # Private helper function for form encoding
362
362
  def flatten_params(params, parent=nil)
363
363
  result = []
@@ -533,6 +533,9 @@ module Veritable
533
533
  if opts['convert_types'] # try converting to float
534
534
  begin
535
535
  rows[i][c] = Float(rows[i][c]) unless rows[i][c].is_a? Float
536
+ if rows[i][c].nan? or rows[i][c].infinite?
537
+ raise VeritableError.new("Float is NaN or Inf",{})
538
+ end
536
539
  rescue
537
540
  rows[i][c] = opts['remove_invalids'] ? nil : rows[i][c] # flag for removal
538
541
  end
@@ -540,8 +543,8 @@ module Veritable
540
543
  if rows[i][c].nil?
541
544
  rows[i].delete c
542
545
  else
543
- if not rows[i][c].is_a? Float
544
- raise VeritableError.new("Validate -- row #{i}, key #{c}, value #{rows[i][c]} is a #{rows[i][c].class}, not a float.", {'row' => i, 'col' => c})
546
+ if (not rows[i][c].is_a? Float) or rows[i][c].nan? or rows[i][c].infinite?
547
+ raise VeritableError.new("Validate -- row #{i}, key #{c}, value #{rows[i][c]} is Inf of NaN, not a valid float.", {'row' => i, 'col' => c})
545
548
  end
546
549
  end
547
550
  elsif coltype == 'boolean'
@@ -1,5 +1,5 @@
1
1
  module Veritable
2
2
 
3
3
  # The current version of veritable-ruby
4
- VERSION = "0.1.5.35"
4
+ VERSION = "0.1.6.36"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: veritable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5.35
4
+ version: 0.1.6.36
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-18 00:00:00.000000000 Z
12
+ date: 2012-09-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -189,7 +189,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
189
189
  version: '0'
190
190
  segments:
191
191
  - 0
192
- hash: -1517887521321432942
192
+ hash: 1549802306916139207
193
193
  required_rubygems_version: !ruby/object:Gem::Requirement
194
194
  none: false
195
195
  requirements:
@@ -198,7 +198,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
198
198
  version: '0'
199
199
  segments:
200
200
  - 0
201
- hash: -1517887521321432942
201
+ hash: 1549802306916139207
202
202
  requirements: []
203
203
  rubyforge_project:
204
204
  rubygems_version: 1.8.24