refine-ruby 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 23013da310f5a03e05cbc6cc3152567257b64184
4
- data.tar.gz: fd29dd48c328ef841812d4bc983575bf51d94136
3
+ metadata.gz: dda04c11eafc58372e17a09a56aa0e59649daeaf
4
+ data.tar.gz: 860aac69f2bcb486052094b5d43dc2756014ee64
5
5
  SHA512:
6
- metadata.gz: 621bb2bd7aa876aabe148f6214c602328b832ae09b2f35b9e0388cbdc4602cab81a9e8b2b849ac3ec8ed660679091fde2b582d0f319e9e470eb8643083d89f8a
7
- data.tar.gz: 1646755790ba2c42cfe03c39f4dda3e259754758c6a3b0f9f498d4333b26c09f59520d6e800b16d3f978cd556b8d1bbabdd011a0c2fc1394f127ddeae7c64f57
6
+ metadata.gz: 9492f07c6120b6328f258f2f9872e2bdc113b3504e7eb0b090961beb5611f1e95487a4c32af80e05d5c7c59df1047722b84c475f69e4924183b890177c05f871
7
+ data.tar.gz: 1d3cb1338180a42932f347d67a7a4a7e38d332fd33abd4f0fa645243b503aa0f95f68bbf949ace956151987c3742e8d3a974aaa3c797bdb1f66b0da9a8e750a3
@@ -8,7 +8,9 @@ h2. Install
8
8
 
9
9
  @gem install refine-ruby@
10
10
 
11
- h2. Example
11
+ h2. Use Cases
12
+
13
+ h3. Apply Operations / Export Operations
12
14
 
13
15
  Given that you have the following raw data:
14
16
 
@@ -21,42 +23,42 @@ Given that you have the following raw data:
21
23
  </code>
22
24
  </pre>
23
25
 
24
- Google Refine lets you clean up the data and export your operation history as a JSON instruction set. Here is an example that extracts the year from the above dates:
26
+ Refine Ruby lets you clean up the data and export your operation history as a JSON instruction set. Here is an example that extracts the year from the above dates:
25
27
 
26
28
  <pre>
27
- <code>
28
- [
29
- {
30
- "op": "core/text-transform",
31
- "description": "Text transform on cells in column Date using expression grel:value.toDate()",
32
- "engineConfig": {
33
- "facets": [],
34
- "mode": "row-based"
35
- },
36
- "columnName": "Date",
37
- "expression": "grel:value.toDate()",
38
- "onError": "set-to-blank",
39
- "repeat": false,
40
- "repeatCount": 10
41
- },
42
- {
43
- "op": "core/text-transform",
44
- "description": "Text transform on cells in column Date using expression grel:value.datePart(\"year\")+1",
45
- "engineConfig": {
46
- "facets": [],
47
- "mode": "row-based"
29
+ <code>
30
+ [
31
+ {
32
+ "op": "core/text-transform",
33
+ "description": "Text transform on cells in column Date using expression grel:value.toDate()",
34
+ "engineConfig": {
35
+ "facets": [],
36
+ "mode": "row-based"
37
+ },
38
+ "columnName": "Date",
39
+ "expression": "grel:value.toDate()",
40
+ "onError": "set-to-blank",
41
+ "repeat": false,
42
+ "repeatCount": 10
48
43
  },
49
- "columnName": "Date",
50
- "expression": "grel:value.datePart(\"year\")",
51
- "onError": "set-to-blank",
52
- "repeat": false,
53
- "repeatCount": 10
54
- }
55
- ]
56
- </code>
44
+ {
45
+ "op": "core/text-transform",
46
+ "description": "Text transform on cells in column Date using expression grel:value.datePart(\"year\")+1",
47
+ "engineConfig": {
48
+ "facets": [],
49
+ "mode": "row-based"
50
+ },
51
+ "columnName": "Date",
52
+ "expression": "grel:value.datePart(\"year\")",
53
+ "onError": "set-to-blank",
54
+ "repeat": false,
55
+ "repeatCount": 10
56
+ }
57
+ ]
58
+ </code>
57
59
  </pre>
58
60
 
59
- You can use this gem to apply the operation set to the raw data from ruby. You will need to have Google Refine running on your local computer, or specify an external address (see source):
61
+ You can use this gem to apply the operation set to the raw data from ruby. You will need to have OpenRefine running on your local computer, or specify an external address (see source):
60
62
 
61
63
  <pre>
62
64
  <code>
@@ -64,7 +66,6 @@ You can use this gem to apply the operation set to the raw data from ruby. You w
64
66
  prj = Refine.new('project_name' => 'date cleanup', 'file_name' => 'dates.txt')
65
67
  prj.apply_operations('operations.json')
66
68
  puts prj.export_rows('csv')
67
- prj.delete_project
68
69
  </code>
69
70
  </pre>
70
71
 
@@ -79,6 +80,60 @@ Which outputs:
79
80
  </code>
80
81
  </pre>
81
82
 
83
+ h3. Link to Facets
84
+
85
+ Continuing on the project above - assuming 'project_id' = 1594197247031.
86
+ Link to facets generates a link to the facets set up. The method can take an array of hashes, with the keys as the column name and the optional values: 'sort', 'invert', and a '<GREL expression>'.
87
+
88
+ <pre>
89
+ <code>
90
+ require 'refine'
91
+ prj = Refine.new('project_id' => 1594197247031)
92
+ prj.link_to_facets('Date' => ['invert', 'isNonBlank(value)'])
93
+ </code>
94
+ </pre>
95
+
96
+ Which outputs:
97
+
98
+ <pre>
99
+ "http://127.0.0.1:3333/project?project=1594197247031&ui=%7B%22facets%22%3A%5B%7B%22c%22%3A%7B%22columnName%22%3A%22Date%22%2C%22expression%22%3A%22value%22%2C%22name%22%3A%22Date%22%2C%22invert%22%3Atrue%7D%2C%22o%22%3A%7B%22sort%22%3A%22name%22%7D%7D%5D%7D"
100
+ </pre>
101
+
102
+ Screenshot:
103
+
104
+ !/img/link_to_facets_screenshot.png!
105
+
106
+
107
+ h3. Compute Facet
108
+
109
+ Compute facet returns a JSON object with meta info on the output of the column based on the GREL expression provided. The method can take an array of hashes with the keys as the column name and the optional values: 'sort', 'invert', and a '<GREL expression>'.
110
+ <pre>
111
+ <code>
112
+ prj = Refine.new('project_id' => 1594197247031)
113
+ prj.compute_facet('Date' => ['value'])
114
+ </code>
115
+ </pre>
116
+
117
+ Which outputs:
118
+
119
+ <pre>
120
+ [
121
+ {
122
+ "columnName"=>"Date",
123
+ "name"=>"Date",
124
+ "expression"=>"value",
125
+ "choices"=>[
126
+ {"value"=>2002, "label"=>"2002", "count"=>1, "selected"=>false},
127
+ {"value"=>2001, "label"=>"2001", "count"=>1, "selected"=>false},
128
+ {"value"=>2010, "label"=>"2010", "count"=>1, "selected"=>false}
129
+ ]
130
+ }
131
+ ]
132
+ </pre>
133
+
134
+
135
+ See test_refine.rb to see all the optional arguments for link_to_facets, and compute_facet methods.
136
+
82
137
  h2. Copyright
83
138
 
84
139
  Copyright (c) 2018 David Huynh, Max Ogden & Distill Analytics Inc.
data/Rakefile CHANGED
@@ -1,8 +1,33 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
3
 
4
- Rake::TestTask.new do |t|
4
+ require 'pry'
5
+ def make_minitest_options(args)
6
+ args ||= {}
7
+ options = []
8
+ options << "--name=#{Shellwords.escape(args[:name])} -v" if args.has_key?(:name)
9
+ options.join(" ")
10
+ end
11
+
12
+ Rake::TestTask.new do |t,args|
5
13
  t.pattern = "test/test_*.rb"
6
14
  end
7
15
 
16
+ namespace :test do
17
+ desc "Run just one test based on the name"
18
+ task :named, [:name] do |t, args|
19
+
20
+ test_options = make_minitest_options(args)
21
+
22
+ task_name = "test with opts: #{test_options}"
23
+
24
+ Rake::TestTask.new(task_name) do |test_task|
25
+ test_task.pattern = "test/test_*.rb"
26
+ test_task.options = test_options
27
+ end
28
+
29
+ Rake::Task[task_name].execute
30
+ end
31
+ end
32
+
8
33
  task default: :test
@@ -138,26 +138,24 @@ class Refine
138
138
  }
139
139
  end
140
140
 
141
- json_facet = JSON::dump(facets: [formatted.first])
141
+ json_facet = JSON::dump(facets: formatted)
142
142
 
143
143
  openrefine_response = compute_facets("engine" => json_facet)
144
144
 
145
- facet_response = openrefine_response.fetch("facets").first
145
+ openrefine_response.fetch("facets").map do |facet|
146
146
 
147
+ if facet.key?("choices")
147
148
 
149
+ choices_hash = facet.fetch("choices").map do |h|
150
+ Hash[%w(value label count selected).zip([h["v"]["v"], h["v"]["l"], h["c"], h["s"]])]
151
+ end
148
152
 
149
- if facet_response.key?("choices")
153
+ Hash[%w(columnName name expression choices).zip([facet.fetch("columnName"), facet.fetch("name"), facet.fetch("expression"), choices_hash])]
154
+ elsif facet.key?("error")
150
155
 
151
- choice_hash = facet_response.fetch("choices").map do |h|
152
- Hash[%w(value label count selection).zip([h["v"]["v"], h["v"]["l"], h["c"], h["s"]])]
156
+ Hash[%w(columnName name expression error).zip([facet.fetch("columnName"), facet.fetch("name"), facet.fetch("expression"), facet.fetch("error")])]
153
157
  end
154
158
 
155
- response = choice_hash.inject({}) do |hash, choice|
156
- hash.merge(choice["value"] => choice["count"])
157
- end
158
-
159
- else
160
- response = "Error: " + facet_response.fetch("error")
161
159
  end
162
160
 
163
161
  end
@@ -1,3 +1,3 @@
1
1
  class Refine
2
- VERSION = "0.2.4"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,17 +1,17 @@
1
1
  ### NOTE: The internal client-server protocol used by OpenRefine is not yet maintained as a stable external API, subject to change. ###
2
2
  ### Therefore, plase indicate changes you notice to kittelmann@sub.uni-goettingen.de ###
3
3
  ### Some examples require cURL http://curl.haxx.se ###
4
- ### It is assumed that examples are run from the 'test' directory. Otherwise paths need to be adjusted.
4
+ ### It is assumed that examples are run from the 'test' directory. Otherwise paths need to be adjusted.
5
5
  load '../lib/refine.rb'
6
6
 
7
7
  ##########################
8
8
  ### create initial project
9
9
  ##########################
10
- prj = Refine.new({ 'project_name' => 'date_cleanup', 'file_name' => 'dates.csv' })
10
+ prj = Refine.new({ 'project_name' => 'date_cleanup', 'file_name' => 'dates.csv', 'throws_exceptions'=> false })
11
11
 
12
12
 
13
13
  ##########################
14
- ### create another project
14
+ ### create another project
15
15
  ##########################
16
16
  prj.create_project( 'date_cleanup', 'dates.txt' ) # return value = project id, example: 1484090391100
17
17
 
@@ -21,6 +21,15 @@ prj.create_project( 'date_cleanup', 'dates.txt' ) # ret
21
21
  ################
22
22
  prj.apply_operations( 'operations.json' ) # return value = status code, example: {'code'=>'ok'}
23
23
 
24
+ ######################
25
+ ### link to facet(s) already set up
26
+ ######################
27
+ prj.link_to_facets("Date"=>["isNonBlank(value)"]) # return value = a link to the facet(s) (method can take multiple facet arguments)
28
+
29
+ ######################
30
+ ### compute facet(s)
31
+ ######################
32
+ prj.compute_facet({"Date"=>["value"]}, {"Date"=>["isNonBlank(value)"]}) # return value = the computed facets as an array of hashes
24
33
 
25
34
  ######################
26
35
  ### extract operations
@@ -39,8 +48,8 @@ end
39
48
  ###############
40
49
  ### export data
41
50
  ###############
42
- prj.export_rows # return value = exported data as tsv
43
- prj.export_rows( {'format'=>'tsv'} ) # return value = exported data as tsv
51
+ prj.export_rows # return value = exported data as tsv
52
+ prj.export_rows( {'format'=>'tsv'} ) # return value = exported data as tsv
44
53
  prj.export_rows( {'format'=>'csv'} ) # return value = exported data as csv
45
54
 
46
55
  ### export data in custom table format
@@ -106,4 +115,4 @@ prj.get_all_project_metadata # r
106
115
  ##################
107
116
  ### delete project
108
117
  ##################
109
- prj.delete_project # return value = status, e.g. ok
118
+ prj.delete_project # return value = status, e.g. ok
@@ -25,4 +25,4 @@
25
25
  "repeat": false,
26
26
  "repeatCount": 10
27
27
  }
28
- ]
28
+ ]
@@ -6,7 +6,7 @@ require_relative '../lib/refine.rb'
6
6
  describe Refine do
7
7
 
8
8
  before do
9
- @refine_project = Refine.new({ "project_name" => 'date_cleanup', "file_name" => './test/dates.txt' })
9
+ @refine_project = Refine.new({ "project_name" => 'date_cleanup', "file_name" => './test/dates.txt'})
10
10
  end
11
11
 
12
12
  describe "error handling" do
@@ -105,7 +105,7 @@ describe Refine do
105
105
 
106
106
  assert !params['ui'].nil?
107
107
 
108
- facet_spec = JSON::parse(URI::decode(params['ui']))
108
+ facet_spec = JSON::parse(CGI::unescape(params['ui']))
109
109
  assert_equal 1, facet_spec.fetch("facets").length
110
110
 
111
111
  assert_equal "Date", facet_spec.fetch("facets").first.fetch("c").fetch("columnName")
@@ -199,18 +199,72 @@ describe Refine do
199
199
 
200
200
  describe "compute_facets" do
201
201
  it "Request responds with error due to non existent column" do
202
- response = @refine_project.compute_facet({"transcript_fiscal_year"=> ["isNonBlank(value)"]})
203
- assert_equal("Error: No column named transcript_fiscal_year", response)
202
+ response = @refine_project.compute_facet({"Column 1"=> ["value"]}, {"transcript_fiscal_year"=> ["isNonBlank(value)"]})
203
+ _(response).must_equal ([{
204
+ "columnName" => "Column 1",
205
+ "name" => "Column 1",
206
+ "expression" => "value",
207
+ "choices" => [
208
+ {"value" => "7 December 2001",
209
+ "label" => "7 December 2001",
210
+ "count" => 1 ,
211
+ "selected" => false},
212
+ {"value" => "Date",
213
+ "label" => "Date",
214
+ "count" => 1 ,
215
+ "selected" => false},
216
+ {"value" => "July 1 2002",
217
+ "label" => "July 1 2002",
218
+ "count" => 1,
219
+ "selected" => false},
220
+ {"value" => "10/20/10",
221
+ "label" => "10/20/10",
222
+ "count" => 1 ,
223
+ "selected" => false}
224
+ ]
225
+ },{
226
+ "columnName" => "transcript_fiscal_year",
227
+ "name" => "transcript_fiscal_year",
228
+ "expression" => "isNonBlank(value)",
229
+ "error" => "No column named transcript_fiscal_year"
230
+ }])
204
231
  end
205
232
 
206
- it "Request executes expression and sends response with the results" do
207
- response = @refine_project.compute_facet({"Column 1"=> ["isNonBlank(value)"]})
208
- assert_equal({true=>4}, response)
233
+ it "Request executes expression and cleans up the choices response formatting" do
234
+ response = @refine_project.compute_facet({"Column 1"=> ["value"]})
235
+ _(response).must_equal([{
236
+ "columnName" => "Column 1",
237
+ "name" => "Column 1",
238
+ "expression" => "value",
239
+ "choices" => [
240
+ {"value" => "7 December 2001",
241
+ "label" => "7 December 2001",
242
+ "count" => 1 ,
243
+ "selected" => false},
244
+ {"value" => "Date",
245
+ "label" => "Date",
246
+ "count" => 1 ,
247
+ "selected" => false},
248
+ {"value" => "July 1 2002",
249
+ "label" => "July 1 2002",
250
+ "count" => 1,
251
+ "selected" => false},
252
+ {"value" => "10/20/10",
253
+ "label" => "10/20/10",
254
+ "count" => 1 ,
255
+ "selected" => false}
256
+ ]
257
+ }])
209
258
  end
210
259
 
211
260
  it "Request with faulty expression" do
212
261
  response = @refine_project.compute_facet({"Column 1"=> ["iBlank(value)"]})
213
- assert_equal("Error: Parsing error at offset 6: Unknown function or control named iBlank", response)
262
+ _(response).must_equal([{
263
+ "columnName"=>"Column 1",
264
+ "name"=>"Column 1",
265
+ "expression"=>"iBlank(value)",
266
+ "error"=>"Parsing error at offset 6: Unknown function or control named iBlank"
267
+ }])
214
268
  end
215
269
  end
216
270
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: refine-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caleb Buxton
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2018-05-15 00:00:00.000000000 Z
14
+ date: 2018-05-16 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: addressable
@@ -129,6 +129,7 @@ files:
129
129
  - README.textile
130
130
  - Rakefile
131
131
  - bin/console
132
+ - img/link_to_facets_screenshot.png
132
133
  - lib/refine.rb
133
134
  - lib/refine/version.rb
134
135
  - refine-ruby.gemspec