veritable 0.1.0.79 → 0.1.0.80
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.txt +1 -1
- data/lib/veritable/api.rb +478 -39
- data/lib/veritable/connection.rb +20 -0
- data/lib/veritable/cursor.rb +35 -0
- data/lib/veritable/datatypes.rb +1 -0
- data/lib/veritable/errors.rb +38 -14
- data/lib/veritable/object.rb +8 -0
- data/lib/veritable/resource.rb +4 -0
- data/lib/veritable/util.rb +186 -27
- data/lib/veritable/version.rb +3 -1
- data/lib/veritable.rb +44 -0
- metadata +36 -4
data/lib/veritable/connection.rb
CHANGED
@@ -2,14 +2,21 @@ require 'veritable/object'
|
|
2
2
|
require 'multi_json'
|
3
3
|
|
4
4
|
module Veritable
|
5
|
+
|
6
|
+
# Encapsulates the HTTP logic for connecting to the Veritable API
|
7
|
+
#
|
8
|
+
# Users should not include this module.
|
5
9
|
module Connection
|
6
10
|
include VeritableObject
|
11
|
+
|
12
|
+
# Initalizes a new connection
|
7
13
|
def initialize(opts=nil, doc=nil)
|
8
14
|
super(opts, doc)
|
9
15
|
require_opts :api_key, :api_base_url
|
10
16
|
default_opts(:ssl_verify => true, :enable_gzip => true)
|
11
17
|
end
|
12
18
|
|
19
|
+
# Wraps the HTTP GET logic
|
13
20
|
def get(url, params=nil, headers={})
|
14
21
|
if params and params.count > 0
|
15
22
|
query_string = Util.query_params(params)
|
@@ -18,18 +25,23 @@ module Veritable
|
|
18
25
|
request(:get, url, nil, headers)
|
19
26
|
end
|
20
27
|
|
28
|
+
# Wraps the HTTP POST logic
|
21
29
|
def post(url, payload, headers={})
|
22
30
|
payload = MultiJson.encode(payload)
|
23
31
|
headers = headers.merge({:content_type => 'application/json'})
|
24
32
|
request(:post, url, payload, headers)
|
25
33
|
end
|
26
34
|
|
35
|
+
# Wraps the HTTP PUT logic
|
27
36
|
def put(url, payload, headers={})
|
28
37
|
payload = MultiJson.encode(payload)
|
29
38
|
headers = headers.merge({:content_type => 'application/json'})
|
30
39
|
request(:put, url, payload, headers)
|
31
40
|
end
|
32
41
|
|
42
|
+
# Wraps the HTTP DELETE logic
|
43
|
+
#
|
44
|
+
# Silently allows DELETE of nonexistent resources
|
33
45
|
def delete(url, headers={})
|
34
46
|
begin
|
35
47
|
request(:delete, url, nil, headers)
|
@@ -40,6 +52,7 @@ module Veritable
|
|
40
52
|
end
|
41
53
|
end
|
42
54
|
|
55
|
+
# Wraps the core HTTP request logic
|
43
56
|
def request(verb, url, payload=nil, headers={})
|
44
57
|
url = api_base_url + "/" + url
|
45
58
|
|
@@ -75,9 +88,16 @@ module Veritable
|
|
75
88
|
|
76
89
|
private
|
77
90
|
|
91
|
+
# Private accessor for API key
|
78
92
|
def api_key; @opts[:api_key]; end
|
93
|
+
|
94
|
+
# Private accessor for API base URL
|
79
95
|
def api_base_url; @opts[:api_base_url]; end
|
96
|
+
|
97
|
+
# Private accessor for API :ssl_verify option
|
80
98
|
def ssl_verify; @opts[:ssl_verify]; end
|
99
|
+
|
100
|
+
# Private accessor for API :enable_gzip option
|
81
101
|
def enable_gzip; @opts[:enable_gzip]; end
|
82
102
|
|
83
103
|
end
|
data/lib/veritable/cursor.rb
CHANGED
@@ -2,9 +2,19 @@ require 'veritable/object'
|
|
2
2
|
require 'veritable/resource'
|
3
3
|
|
4
4
|
module Veritable
|
5
|
+
|
6
|
+
# Generic Cursor class for collections of API resources
|
7
|
+
#
|
8
|
+
# Cursors may be initialized with 'limit', 'start', and 'per_page' options.
|
9
|
+
#
|
10
|
+
# Users should call the #each and #next methods for access to the underlying resources.
|
5
11
|
class Cursor
|
6
12
|
include VeritableResource
|
7
13
|
include Enumerable
|
14
|
+
|
15
|
+
# Initializes a new Veritable::Cursor from an API collection
|
16
|
+
#
|
17
|
+
# Optionally pass a block in for postprocessing of resources.
|
8
18
|
def initialize(opts=nil, doc=nil, &lazymap)
|
9
19
|
super(opts, doc)
|
10
20
|
|
@@ -17,6 +27,7 @@ module Veritable
|
|
17
27
|
@opts['lazymap'] = lazymap if lazymap
|
18
28
|
end
|
19
29
|
|
30
|
+
# Implements the Enumerable interface
|
20
31
|
def each
|
21
32
|
i = limit if limit
|
22
33
|
loop do
|
@@ -35,11 +46,16 @@ module Veritable
|
|
35
46
|
end
|
36
47
|
end
|
37
48
|
end
|
49
|
+
|
50
|
+
# String representation of the Cursor
|
38
51
|
def inspect; to_s; end
|
52
|
+
|
53
|
+
# String representation of the Cursor
|
39
54
|
def to_s; "#<Veritable::Cursor collection='" + collection + "'>"; end
|
40
55
|
|
41
56
|
private
|
42
57
|
|
58
|
+
# Private method to refresh the cursor from the server
|
43
59
|
def refresh
|
44
60
|
return data.length if data.length > 0
|
45
61
|
if next_page
|
@@ -52,15 +68,34 @@ module Veritable
|
|
52
68
|
return data.length
|
53
69
|
end
|
54
70
|
|
71
|
+
# Private accessor for the limit option
|
55
72
|
def limit; @opts['limit']; end
|
73
|
+
|
74
|
+
# Private setter for the limit option
|
56
75
|
def limit=(x); @opts['limit'] = x; end
|
76
|
+
|
77
|
+
# Private accessor for the start option
|
57
78
|
def start; @opts['start']; end
|
79
|
+
|
80
|
+
# Private accessor for the per_page option
|
58
81
|
def per_page; @opts['per_page']; end
|
82
|
+
|
83
|
+
# Privatre accessor for the collection
|
59
84
|
def collection; @opts['collection'] end
|
85
|
+
|
86
|
+
# Postprocessing block, if any
|
60
87
|
def lazymap; @opts['lazymap']; end
|
88
|
+
|
89
|
+
# Key for the underlying data
|
61
90
|
def key; @opts['key'] end
|
91
|
+
|
92
|
+
# Link to the next page of the collection
|
62
93
|
def next_page; link 'next' end
|
94
|
+
|
95
|
+
# True if the Cursor is on the last page of the collection
|
63
96
|
def last_page?; ! @doc.has_key? 'next' end
|
97
|
+
|
98
|
+
# Fetches the underlying data
|
64
99
|
def data; @doc[key] end
|
65
100
|
end
|
66
101
|
end
|
data/lib/veritable/datatypes.rb
CHANGED
data/lib/veritable/errors.rb
CHANGED
@@ -1,17 +1,41 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
1
|
+
module Veritable
|
2
|
+
|
3
|
+
# Class for all errors returned by veritable-ruby
|
4
|
+
#
|
5
|
+
# ==== Attributes
|
6
|
+
# * +message+ -- the String message describing the error
|
7
|
+
# * dynamically defined -- errors may have other attributes, such as +http_code+ or +row+, dynamically defined at initialization.
|
8
|
+
class VeritableError < StandardError
|
9
|
+
# Accessor for the error message
|
10
|
+
attr_reader :message
|
11
|
+
|
12
|
+
# Initializes a Veritable::VeritableError
|
13
|
+
#
|
14
|
+
# Users should not invoke directly.
|
15
|
+
#
|
16
|
+
# ==== Arguments
|
17
|
+
# +message+ -- a String message describing the error
|
18
|
+
# +opts+ -- a Hash optionally specifying other instance attributes to be dynamically defined
|
19
|
+
#
|
20
|
+
#
|
21
|
+
# See also: https://dev.priorknowledge.com/docs/client/ruby
|
22
|
+
def initialize(message, opts=nil)
|
23
|
+
@message = message
|
24
|
+
if opts.is_a? Hash
|
25
|
+
@opts = opts
|
26
|
+
eigenclass = class << self; self; end
|
27
|
+
@opts.keys.each {|k|
|
28
|
+
eigenclass.send(:define_method, k.to_sym) {
|
29
|
+
@opts[k]
|
30
|
+
}
|
11
31
|
}
|
12
|
-
|
32
|
+
end
|
13
33
|
end
|
34
|
+
|
35
|
+
# Prints the error message
|
36
|
+
def to_s; message; end
|
37
|
+
|
38
|
+
# Prints the error message
|
39
|
+
def inspect; message; end
|
14
40
|
end
|
15
|
-
|
16
|
-
def inspect; message; end
|
17
|
-
end
|
41
|
+
end
|
data/lib/veritable/object.rb
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
require 'veritable/errors'
|
2
2
|
|
3
3
|
module Veritable
|
4
|
+
# Abstracts the structure of veritable-ruby objects
|
5
|
+
#
|
6
|
+
# Users should not include this module.
|
4
7
|
module VeritableObject
|
8
|
+
# Initializes a new object from a hash of options and an API doc
|
9
|
+
#
|
10
|
+
# Users should not invoke directly.
|
5
11
|
def initialize(opts=nil, doc=nil)
|
6
12
|
@opts = opts
|
7
13
|
@doc = doc
|
@@ -9,10 +15,12 @@ module Veritable
|
|
9
15
|
|
10
16
|
private
|
11
17
|
|
18
|
+
# Private method -- requires that certain options be present at initialization
|
12
19
|
def require_opts(*keys)
|
13
20
|
keys.each {|k| raise VeritableError.new("Error initializing object -- must provide #{k}") unless @opts.has_key?(k)}
|
14
21
|
end
|
15
22
|
|
23
|
+
# Private method -- specifies default options
|
16
24
|
def default_opts(hash={})
|
17
25
|
hash.each {|k, v| @opts[k] = v unless @opts.has_key?(k)}
|
18
26
|
end
|
data/lib/veritable/resource.rb
CHANGED
@@ -2,11 +2,15 @@ require 'veritable/connection'
|
|
2
2
|
require 'veritable/errors'
|
3
3
|
|
4
4
|
module Veritable
|
5
|
+
# Abstracts the structure of Veritable API resources
|
6
|
+
#
|
7
|
+
# Users should not include this module.
|
5
8
|
module VeritableResource
|
6
9
|
include Connection
|
7
10
|
|
8
11
|
private
|
9
12
|
|
13
|
+
# Private method: retrieves the appropriate link field from the resource doc
|
10
14
|
def link(name)
|
11
15
|
@doc['links'][name]
|
12
16
|
end
|
data/lib/veritable/util.rb
CHANGED
@@ -6,17 +6,51 @@ require 'csv'
|
|
6
6
|
require 'set'
|
7
7
|
|
8
8
|
module Veritable
|
9
|
+
|
10
|
+
# Encapsulates utilities for working with data
|
11
|
+
#
|
12
|
+
# ==== Methods
|
13
|
+
# * +read_csv+ -- reads a .csv from disk into an Array of row Hashes
|
14
|
+
# * +write_csv+ -- writes an Array of row Hashes to disk as .csv
|
15
|
+
# * +split_rows+ -- splits an Array of row Hashes into two sets
|
16
|
+
# * +make_schema+ -- makes a new analysis schema from a schema rule
|
17
|
+
# * +validate_data+ -- validates an Array of row Hashes against a schema
|
18
|
+
# * +clean_data+ -- cleans an Array of row Hashes to conform to a schema
|
19
|
+
# * +validate_predictions+ -- validates a single predictions request Hash against a schema
|
20
|
+
# * +clean_predictions+ -- cleans a predictions request Hash to conform to a schema
|
21
|
+
# * +validate_schema+ -- validates a schema
|
22
|
+
# * +check_id+ -- checks that a unique ID is valid
|
23
|
+
# * +check_row+ -- checks that a row Hash is well-formed
|
24
|
+
# * +check_datatype+ -- checks that a datatype is valid
|
25
|
+
# * +query_params+ -- helper function for HTTP form encoding
|
26
|
+
# * +make_table_id+ -- autogenerates a new valid ID for a table
|
27
|
+
# * +make_analysis_id+ -- autogenerates a new valid ID for an analysis
|
28
|
+
#
|
29
|
+
# See also: https://dev.priorknowledge.com/docs/client/ruby
|
9
30
|
module Util
|
10
31
|
class << self
|
32
|
+
# Autogenerate a new table ID
|
33
|
+
#
|
34
|
+
# Users should not call directly
|
11
35
|
def make_table_id; UUID.new.generate :compact ; end
|
36
|
+
|
37
|
+
# Autogenerate a new analysis ID
|
38
|
+
#
|
39
|
+
# Users should not call directly
|
12
40
|
def make_analysis_id; UUID.new.generate :compact ; end
|
13
41
|
|
42
|
+
# Helper function for HTTP form encoding
|
43
|
+
#
|
44
|
+
# Users should not call directly
|
14
45
|
def query_params(params, parent=nil)
|
15
46
|
flatten_params(params).collect {|x|
|
16
47
|
"#{x[0]}=#{x[1]}"
|
17
48
|
}.join("&")
|
18
49
|
end
|
19
50
|
|
51
|
+
# Checks that a unique ID is valid
|
52
|
+
#
|
53
|
+
# Raises a VeritableError if the ID is invalid.
|
20
54
|
def check_id(id)
|
21
55
|
if not id.is_a? String
|
22
56
|
begin
|
@@ -33,6 +67,9 @@ module Veritable
|
|
33
67
|
end
|
34
68
|
end
|
35
69
|
|
70
|
+
# Checks that a given row is well-formed
|
71
|
+
#
|
72
|
+
# Raises a VeritableError if the row Hash is not well-formed
|
36
73
|
def check_row(row)
|
37
74
|
if not row.is_a? Hash
|
38
75
|
begin
|
@@ -53,6 +90,9 @@ module Veritable
|
|
53
90
|
end
|
54
91
|
end
|
55
92
|
|
93
|
+
# Checks tht a given datatype is valid
|
94
|
+
#
|
95
|
+
# Raises a VeritableError if the datatype is invalid.
|
56
96
|
def check_datatype(datatype, msg=nil)
|
57
97
|
if not DATATYPES.include? datatype
|
58
98
|
begin
|
@@ -65,6 +105,16 @@ module Veritable
|
|
65
105
|
end
|
66
106
|
end
|
67
107
|
|
108
|
+
# Splits an array of row Hashes into two sets
|
109
|
+
#
|
110
|
+
# ==== Arguments
|
111
|
+
# * +rows+ -- an Array of valid row Hashes
|
112
|
+
# * +frac+ -- the fraction of the rows to include in the first set
|
113
|
+
#
|
114
|
+
# ==== Returns
|
115
|
+
# An array <tt>[train_dataset, test_dataset]</tt>, each of whose members is an Array of row Hashes.
|
116
|
+
#
|
117
|
+
# See also: https://dev.priorknowledge.com/docs/client/ruby
|
68
118
|
def split_rows(rows, frac)
|
69
119
|
rows = rows.to_a
|
70
120
|
n = rows.size
|
@@ -75,32 +125,56 @@ module Veritable
|
|
75
125
|
return [train_dataset, test_dataset]
|
76
126
|
end
|
77
127
|
|
78
|
-
|
79
|
-
|
80
|
-
|
128
|
+
# Validates a schema
|
129
|
+
#
|
130
|
+
# Checks that a Veritable::Schema or Hash of the appropriate form is well-formed.
|
131
|
+
def validate_schema(schema); schema.is_a? Veritable::Schema ? schema.validate : Veritable::Schema.new(schema).validate; end
|
81
132
|
|
133
|
+
# Makes a new analysis schema from a schema rule
|
134
|
+
#
|
135
|
+
# ==== Arguments
|
136
|
+
# * +schema_rule+ -- a Hash or Array of two-valued Arrays, whose keys or first values should be regexes to match against column names, and whose values should be the appropriate datatype to assign to matching columns, for instance:
|
137
|
+
# [['a_regex_to_match', {'type' => 'continuous'}], ['another_regex', {'type' => 'count'}], ...]
|
138
|
+
# * +opts+ -- a Hash which must contain either:
|
139
|
+
# - the key <tt>'headers'</tt>, whose value should be an Array of column names from which to construct the schema
|
140
|
+
# - or the key <tt>'rows'</tt>, whose value should be an Array of row Hashes from whose columns the schema is to be constructed
|
141
|
+
#
|
142
|
+
# ==== Returns
|
143
|
+
# A new Veritable::Schema
|
144
|
+
#
|
145
|
+
# See also: https://dev.priorknowledge.com/docs/client/ruby
|
82
146
|
def make_schema(schema_rule, opts={})
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
147
|
+
if ((not opts.has_key?('headers')) and (not opts.has_key?('rows')))
|
148
|
+
raise VeritableError.new("Either 'headers' or 'rows' must be provided!")
|
149
|
+
end
|
150
|
+
headers = opts.has_key?('headers') ? opts['headers'] : nil
|
151
|
+
if headers.nil?
|
152
|
+
headers = Set.new
|
153
|
+
opts['rows'].each {|row| headers.merge(row.keys)}
|
154
|
+
headers = headers.to_a.sort
|
155
|
+
end
|
156
|
+
schema = {}
|
157
|
+
headers.each do |c|
|
158
|
+
schema_rule.each do |r, t|
|
159
|
+
if r === c
|
160
|
+
schema[c] = t
|
161
|
+
break
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
return Veritable::Schema.new(schema)
|
102
166
|
end
|
103
167
|
|
168
|
+
# Writes an Array of row Hashes out to .csv
|
169
|
+
#
|
170
|
+
# ==== Arguments
|
171
|
+
# * +rows+ -- an Array of valid row Hashes
|
172
|
+
# * +filename+ -- a path to the .csv file to write out
|
173
|
+
#
|
174
|
+
# ==== Returns
|
175
|
+
# +nil+ on success.
|
176
|
+
#
|
177
|
+
# See also: https://dev.priorknowledge.com/docs/client/ruby
|
104
178
|
def write_csv(rows, filename)
|
105
179
|
headers = Set.new
|
106
180
|
rows.each {|row| headers.merge(row.keys)}
|
@@ -112,8 +186,22 @@ module Veritable
|
|
112
186
|
csv << out_row
|
113
187
|
end
|
114
188
|
end
|
189
|
+
nil
|
115
190
|
end
|
116
191
|
|
192
|
+
# Reads a .csv with headers in as an Array of row Hashes
|
193
|
+
#
|
194
|
+
# All values are kept as strings, except empty strings, which are omitted. To clean data and convert types in accordance with a given schema, use the clean_data and validate_data functions.
|
195
|
+
#
|
196
|
+
# ==== Arguments
|
197
|
+
# * +filename+ -- a path to the .csv file to read in from
|
198
|
+
# * +id_col+ -- optionally specify the column to rename to +'_id'+. If +nil+ (default) and a column named +'_id'+ is present, that column is used. If +nil+ and no +'_id'+ column is present, then +'_id'+ will be automatically generated.
|
199
|
+
# * +na_cols+ -- a list of string values to omit; defaults to +['']+.
|
200
|
+
#
|
201
|
+
# ==== Returns
|
202
|
+
# An Array of row Hashes
|
203
|
+
#
|
204
|
+
# See also: https://dev.priorknowledge.com/docs/client/ruby
|
117
205
|
def read_csv(filename, id_col=nil, na_vals=[''])
|
118
206
|
rows = CSV.read(filename)
|
119
207
|
header = rows.shift
|
@@ -135,7 +223,29 @@ module Veritable
|
|
135
223
|
end
|
136
224
|
return rows
|
137
225
|
end
|
138
|
-
|
226
|
+
|
227
|
+
# Cleans up an Array of row Hashes in accordance with an analysis schema
|
228
|
+
#
|
229
|
+
# This method mutates its +rows+ argument. If clean_data raises an exception, values in some rows may be converted while others are left in their original state.
|
230
|
+
#
|
231
|
+
# ==== Arguments
|
232
|
+
# * +rows+ -- the Array of Hashes to clean up
|
233
|
+
# * +schema+ -- a Schema specifying the types of the columns appearing in the rows being cleaned
|
234
|
+
# * +opts+ -- a Hash optionally containing the keys:
|
235
|
+
# - +convert_types+ -- controls whether clean_data will attempt to convert cells in a column to be of the correct type (default: +true+)
|
236
|
+
# - +remove_nones+ -- controls whether clean_data will automatically remove cells containing the value +nil+ (default: +true+)
|
237
|
+
# - +remove_invalids+ -- controls whether clean_data will automatically remove cells that are invalid for a given column (default: +true+)
|
238
|
+
# - +reduce_categories+ -- controls whether clean_data will automatically reduce the number of categories in categorical columns with too many categories (default: +true+) If +true+, the largest categories in a column will be preserved, up to the allowable limit, and the other categories will be binned as <tt>"Other"</tt>.
|
239
|
+
# - +assign_ids+ -- controls whether clean_data will automatically assign new ids to the rows (default: +false=) If +true+, rows will be numbered sequentially. If the rows have an existing <tt>'_id'</tt> column, +remove_extra_fields+ must also be set to +true+ to avoid raising a Veritable::VeritableError.
|
240
|
+
# - +remove_extra_fields+ -- controls whether clean_data will automatically remove columns that are not contained in the schema (default: +false+) If +assign_ids+ is +true+ (default), will also remove the <tt>'_id'</tt> column.
|
241
|
+
#
|
242
|
+
# ==== Raises
|
243
|
+
# A Veritable::VeritableError containing further details if the data does not validate against the schema.
|
244
|
+
#
|
245
|
+
# ==== Returns
|
246
|
+
# +nil+ on success (mutates +rows+ argument)
|
247
|
+
#
|
248
|
+
# See also: https://dev.priorknowledge.com/docs/client/ruby
|
139
249
|
def clean_data(rows, schema, opts={})
|
140
250
|
validate(rows, schema, {
|
141
251
|
'convert_types' => opts.has_key?('convert_types') ? opts['convert_types'] : true,
|
@@ -150,6 +260,19 @@ module Veritable
|
|
150
260
|
'allow_empty_columns' => false})
|
151
261
|
end
|
152
262
|
|
263
|
+
# Validates an Array of row Hashes against an analysis schema
|
264
|
+
#
|
265
|
+
# ==== Arguments
|
266
|
+
# * +rows+ -- the Array of Hashes to clean up
|
267
|
+
# * +schema+ -- a Schema specifying the types of the columns appearing in the rows being cleaned
|
268
|
+
#
|
269
|
+
# ==== Raises
|
270
|
+
# A Veritable::VeritableError containing further details if the data does not validate against the schema.
|
271
|
+
#
|
272
|
+
# ==== Returns
|
273
|
+
# +nil+ on success
|
274
|
+
#
|
275
|
+
# See also: https://dev.priorknowledge.com/docs/client/ruby
|
153
276
|
def validate_data(rows, schema)
|
154
277
|
validate(rows, schema, {
|
155
278
|
'convert_types' => false,
|
@@ -164,6 +287,25 @@ module Veritable
|
|
164
287
|
'allow_empty_columns' => false})
|
165
288
|
end
|
166
289
|
|
290
|
+
# Cleans up a predictions request in accordance with an analysis schema
|
291
|
+
#
|
292
|
+
# This method mutates its +predictions+ argument. If clean_predictions raises an exception, values in some columns may be converted while others are left in their original state.
|
293
|
+
#
|
294
|
+
# ==== Arguments
|
295
|
+
# * +predictions+ -- the predictions request to clean up
|
296
|
+
# * +schema+ -- a Schema specifying the types of the columns appearing in the predictions request
|
297
|
+
# * +opts+ -- a Hash optionally containing the keys:
|
298
|
+
# - +convert_types+ -- controls whether clean_data will attempt to convert cells in a column to be of the correct type (default: +true+)
|
299
|
+
# - +remove_invalids+ -- controls whether clean_data will automatically remove cells that are invalid for a given column (default: +true+)
|
300
|
+
# - +remove_extra_fields+ -- controls whether clean_data will automatically remove columns that are not contained in the schema (default: +true+)
|
301
|
+
#
|
302
|
+
# ==== Raises
|
303
|
+
# A Veritable::VeritableError containing further details if the predictions request does not validate against the schema
|
304
|
+
#
|
305
|
+
# ==== Returns
|
306
|
+
# +nil+ on success (mutates +predictions+ argument)
|
307
|
+
#
|
308
|
+
# See also: https://dev.priorknowledge.com/docs/client/ruby
|
167
309
|
def clean_predictions(predictions, schema, opts={})
|
168
310
|
validate(predictions, schema, {
|
169
311
|
'convert_types' => opts.has_key?('convert_types') ? opts['convert_types'] : true,
|
@@ -178,6 +320,19 @@ module Veritable
|
|
178
320
|
'allow_empty_columns' => true})
|
179
321
|
end
|
180
322
|
|
323
|
+
# Validates a predictions request against an analysis schema
|
324
|
+
#
|
325
|
+
# ==== Arguments
|
326
|
+
# * +predictions+ -- the predictions request to clean up
|
327
|
+
# * +schema+ -- a Schema specifying the types of the columns appearing in the predictions request
|
328
|
+
#
|
329
|
+
# ==== Raises
|
330
|
+
# A Veritable::VeritableError containing further details if the predictions request does not validate against the schema.
|
331
|
+
#
|
332
|
+
# ==== Returns
|
333
|
+
# +nil+ on success
|
334
|
+
#
|
335
|
+
# See also: https://dev.priorknowledge.com/docs/client/ruby
|
181
336
|
def validate_predictions(predictions, schema)
|
182
337
|
validate(predictions, schema, {
|
183
338
|
'convert_types' => false,
|
@@ -194,6 +349,7 @@ module Veritable
|
|
194
349
|
|
195
350
|
private
|
196
351
|
|
352
|
+
# Private helper function for form encoding
|
197
353
|
def flatten_params(params, parent=nil)
|
198
354
|
result = []
|
199
355
|
if params.is_a? Hash
|
@@ -217,16 +373,19 @@ module Veritable
|
|
217
373
|
result
|
218
374
|
end
|
219
375
|
|
220
|
-
|
221
|
-
|
222
|
-
end
|
376
|
+
# Wraps URL encoding
|
377
|
+
def urlencode(k); URI.escape(k.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")); end
|
223
378
|
|
379
|
+
# Private helper function to convert to Integer
|
224
380
|
def to_integer(v)
|
225
381
|
return v if v.is_a? Fixnum
|
226
382
|
v.gsub!(/\A([+-]?\d+?)\.0*?\Z/, '\1')
|
227
383
|
Integer(v)
|
228
384
|
end
|
229
|
-
|
385
|
+
|
386
|
+
# Row validation logic
|
387
|
+
#
|
388
|
+
# Users should call clean_data, validate_data, clean_predictions, or validate_predictions
|
230
389
|
def validate(rows, schema, opts)
|
231
390
|
schema = Veritable::Schema.new(schema) unless schema.is_a? Veritable::Schema
|
232
391
|
|
data/lib/veritable/version.rb
CHANGED
data/lib/veritable.rb
CHANGED
@@ -10,10 +10,54 @@ require 'rest_client'
|
|
10
10
|
require 'uuid'
|
11
11
|
require 'multi_json'
|
12
12
|
|
13
|
+
# The main module for the Veritable client
|
14
|
+
#
|
15
|
+
# ==== Module methods
|
16
|
+
# Veritable.connect is the main entry point.
|
17
|
+
#
|
18
|
+
# ==== Classes
|
19
|
+
# Veritable::API represents a single user's tables and settings.
|
20
|
+
#
|
21
|
+
# Veritable::Table, Veritable::Analysis, and Veritable::Prediction represent API resources.
|
22
|
+
#
|
23
|
+
# Veritable::Schema represents schemas for Veritable analyses.
|
24
|
+
#
|
25
|
+
# Collections of API resources are returned as instances of Veritable::Cursor, an Enumerable.
|
26
|
+
#
|
27
|
+
# All errors are instances of Veritable::VeritableError.
|
28
|
+
#
|
29
|
+
# ==== Modules
|
30
|
+
# The Veritable::Connection module encapsulates the HTTP logic.
|
31
|
+
#
|
32
|
+
# The Veritable::Util module includes some helper methods for working with datasets.
|
33
|
+
#
|
34
|
+
# The Veritable::VeritableResource and Veritable::VeritableObject modules are internal abstractions.
|
35
|
+
#
|
36
|
+
# See also: https://dev.priorknowledge.com/docs/client/ruby
|
13
37
|
module Veritable
|
38
|
+
|
39
|
+
# The HTTP User-Agent header used by the client library.
|
14
40
|
USER_AGENT = 'veritable-ruby ' + VERSION
|
41
|
+
|
42
|
+
# The default base URL for the Veritable API server.
|
15
43
|
BASE_URL = "https://api.priorknowledge.com"
|
16
44
|
|
45
|
+
# The main entry point to the Veritable API
|
46
|
+
#
|
47
|
+
# ==== Arguments
|
48
|
+
# * +opts+ -- a Hash containing options for the connection. Possible keys include:
|
49
|
+
# - +:api_key+ -- the Veritable API key to use. If not set, defaults to <tt>ENV['VERITABLE_KEY']</tt>
|
50
|
+
# - +:api_base_url+ -- the base URL of the Veritable API. If not set, defaults to <tt>ENV['VERITABLE_URL']</tt> or BASE_URL
|
51
|
+
# - +:ssl_verify+ -- if +true+, the SSL certificate of the API server will be verified.
|
52
|
+
# - +:enable_gzip+ -- if +true+, requests to the API server will be gzipped.
|
53
|
+
#
|
54
|
+
# ==== Raises
|
55
|
+
# A Veritable::VeritableError if no Veritable API server is found at the indicated URL.
|
56
|
+
#
|
57
|
+
# ==== Returns
|
58
|
+
# An instance of Veritable::API.
|
59
|
+
#
|
60
|
+
# See also: https://dev.priorknowledge.com/docs/client/ruby
|
17
61
|
def self.connect(opts={})
|
18
62
|
opts[:api_key] = opts[:api_key] || ENV['VERITABLE_KEY']
|
19
63
|
opts[:api_base_url] = opts[:api_base_url] || ENV['VERITABLE_URL'] || BASE_URL
|