Class: Refine
- Inherits:
-
Object
- Object
- Refine
- Defined in:
- lib/refine.rb,
lib/refine/version.rb
Constant Summary collapse
- VERSION =
"0.3.1"
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.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/refine.rb', line 17 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
198 199 200 201 |
# File 'lib/refine.rb', line 198 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.
9 10 11 |
# File 'lib/refine.rb', line 9 def project_id @project_id end |
#project_name ⇒ Object (readonly)
Returns the value of attribute project_name.
8 9 10 |
# File 'lib/refine.rb', line 8 def project_name @project_name end |
Class Method Details
.get_all_project_metadata(server = "http://127.0.0.1:3333") ⇒ Object
11 12 13 14 15 |
# File 'lib/refine.rb', line 11 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
53 54 55 56 57 58 59 60 61 |
# File 'lib/refine.rb', line 53 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
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/refine.rb', line 92 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
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 162 |
# File 'lib/refine.rb', line 128 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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/refine.rb', line 35 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
81 82 83 84 85 86 87 88 |
# File 'lib/refine.rb', line 81 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
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/refine.rb', line 63 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
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 194 |
# File 'lib/refine.rb', line 164 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
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/refine.rb', line 115 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 |