Class: Refine
- Inherits:
-
Object
- Object
- Refine
- Defined in:
- lib/refine.rb,
lib/refine/version.rb
Constant Summary collapse
- VERSION =
"0.3.2"
Instance Attribute Summary collapse
-
#project_id ⇒ Object
readonly
Returns the value of attribute project_id.
-
#project_name ⇒ Object
readonly
Returns the value of attribute project_name.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_operations(file_name_or_string) ⇒ Object
-
#call(method, params = {}) ⇒ Object
this pattern is pulled from mailchimp/mailchimp-gem.
- #compute_facet(*column_names) ⇒ Object
- #create_project(project_name, file_name) ⇒ Object
- #delete_project ⇒ Object
- #export_rows(opts = {}) ⇒ Object
- #facet_parameters(*column_names) ⇒ Object
-
#initialize(opts = {}, &block) ⇒ Refine
constructor
A new instance of Refine.
- #link_to_facets(*column_names) ⇒ Object
- #method_missing(method, *args) ⇒ Object
Constructor Details
#initialize(opts = {}, &block) ⇒ Refine
Returns a new instance of Refine.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/refine.rb', line 16 def initialize(opts = {}, &block) @http_client_config = block @server = opts["server"] || "http://127.0.0.1:3333" @throws_exceptions = opts["throws_exceptions"] === false ? false : true if opts["file_name"] && !opts["file_name"].empty? && opts["project_name"] && !opts["project_name"].empty? project_name = CGI.escape(opts["project_name"]) @project_id = create_project(project_name, opts["file_name"]) @project_name = project_name if @project_id else @project_id = opts["project_id"] = self. @project_name = CGI.escape(["name"]) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
197 198 199 200 |
# File 'lib/refine.rb', line 197 def method_missing(method, *args) # translate: get_column_info --> get-column-info call(method.to_s.gsub('_', '-'), *args) end |
Instance Attribute Details
#project_id ⇒ Object (readonly)
Returns the value of attribute project_id.
8 9 10 |
# File 'lib/refine.rb', line 8 def project_id @project_id end |
#project_name ⇒ Object (readonly)
Returns the value of attribute project_name.
7 8 9 |
# File 'lib/refine.rb', line 7 def project_name @project_name end |
Class Method Details
.get_all_project_metadata(server = "http://127.0.0.1:3333") ⇒ Object
10 11 12 13 14 |
# File 'lib/refine.rb', line 10 def self.(server="http://127.0.0.1:3333") uri = "#{server}/command/core/get-all-project-metadata" response = HTTPClient.new().get(uri) JSON.parse(response.body) end |
Instance Method Details
#apply_operations(file_name_or_string) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/refine.rb', line 52 def apply_operations(file_name_or_string) if File.exist?(file_name_or_string) operations = File.read(file_name_or_string) else operations = file_name_or_string end call('apply-operations', 'operations' => operations) end |
#call(method, params = {}) ⇒ Object
this pattern is pulled from mailchimp/mailchimp-gem
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/refine.rb', line 91 def call(method, params = {}) uri = "#{@server}/command/core/#{method}" params = { "project" => @project_id }.merge(params) response = if method.start_with?('get-') client.get(uri, params) else client.post(uri, params) end begin response = JSON.parse(response.body) rescue response = JSON.parse('[' + response.body + ']').first end if @throws_exceptions && response.is_a?(Hash) && response["code"] && response["code"] == "error" raise "API Error: #{response}" end response end |
#compute_facet(*column_names) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/refine.rb', line 127 def compute_facet(*column_names) formatted = column_names.map do |column| expression, sort_by, invert = facet_opts(column.values.first) { "columnName" => column.keys.first, "expression" => expression, "name" => column.keys.first, "invert" => invert, "sort" => sort_by, "selection" => [] } end json_facet = JSON::dump(facets: formatted) openrefine_response = compute_facets("engine" => json_facet) openrefine_response.fetch("facets").map do |facet| if facet.key?("choices") choices_hash = facet.fetch("choices").map do |h| Hash[%w(value label count selected).zip([h["v"]["v"], h["v"]["l"], h["c"], h["s"]])] end Hash[%w(columnName name expression choices).zip([facet.fetch("columnName"), facet.fetch("name"), facet.fetch("expression"), choices_hash])] elsif facet.key?("error") Hash[%w(columnName name expression error).zip([facet.fetch("columnName"), facet.fetch("name"), facet.fetch("expression"), facet.fetch("error")])] end end end |
#create_project(project_name, file_name) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/refine.rb', line 34 def create_project(project_name, file_name) uri = @server + "/command/core/create-project-from-upload" project_id = false File.open(file_name) do |file| body = { 'project-file' => file, 'project-name' => project_name } response = client.post(uri, body) url = response.header['Location'] unless url == [] project_id = CGI.parse(url[0].split('?')[1])['project'][0] end end raise "Error creating project: #{response}" unless project_id project_id end |
#delete_project ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/refine.rb', line 80 def delete_project uri = @server + "/command/core/delete-project" body = { 'project' => @project_id } @response = client.post(uri, body) JSON.parse(@response.content)['code'] rescue false end |
#export_rows(opts = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/refine.rb', line 62 def export_rows(opts={}) format = opts["format"] || 'tsv' uri = @server + "/command/core/export-rows/#{@project_name}.#{format}" body = { 'engine' => { "facets" => opts["facets"] || [], "mode" => "row-based" }.to_json, 'options' => opts["options"] || '', 'project' => @project_id, 'format' => format } @response = client.post(uri, body) @response.content end |
#facet_parameters(*column_names) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/refine.rb', line 163 def facet_parameters(*column_names) column_names.map do |column| case column when String then { "c" => { "columnName" => column, "expression"=>"value", "name"=> column, "invert"=> false }, "o" => { "sort" => "name" } } when Hash expression, sort_by, invert = facet_opts(column.values.first) { "c" => { "columnName" => column.keys.first, "expression"=> expression, "name"=> column.keys.first, "invert" => invert }, "o" => { "sort" => sort_by } } end end end |
#link_to_facets(*column_names) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/refine.rb', line 114 def link_to_facets(*column_names) uri = Addressable::URI.parse("#{@server}/project") facet = facet_parameters(*column_names) json_facet=JSON::dump(facets: facet).gsub(' ', "\t") uri.query = Addressable::URI::form_encode({project: @project_id, ui: json_facet}) uri.to_s.gsub("%09", "%20") end |