Class: Colore::Client
- Inherits:
-
Object
- Object
- Colore::Client
- Defined in:
- lib/colore/client.rb,
lib/colore/client/version.rb
Overview
Client for interacting with the Colore service.
Constant Summary collapse
- VERSION =
The current version of the Colore client.
'1.0.0'
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#backtrace ⇒ Object
readonly
Returns the value of attribute backtrace.
-
#base_uri ⇒ Object
readonly
Returns the value of attribute base_uri.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
-
.generate_doc_id ⇒ Object
Generates a document id that is reasonably guaranteed to be unique for your app.
Instance Method Summary collapse
-
#convert(content:, action:, language: 'en') ⇒ Hash
Performs a foreground conversion of a file.
-
#create_document(doc_id:, filename:, content:, title: nil, author: nil, actions: nil, callback_url: nil) ⇒ Hash
Stores the specified document on Colore.
-
#delete_document(doc_id:) ⇒ Hash
Completely deletes a document.
-
#delete_version(doc_id:, version:) ⇒ Hash
Completely deletes a document’s version (you cannot delete the current one).
-
#generate_doc_id ⇒ Object
Generates a document id that is reasonably guaranteed to be unique for your app.
-
#get_document(doc_id:, filename:, version: Colore::CURRENT) ⇒ String
Retrieves a document.
-
#get_document_info(doc_id:) ⇒ Hash
Retrieves information about a document.
-
#initialize(app:, base_uri:, logger: Logger.new(nil), backtrace: false, user_agent: 'Colore Client') ⇒ Client
constructor
Constructor.
-
#path_for(doc_id, filename, version = Colore::CURRENT) ⇒ String
Generates a path for the document based on its ID, filename, and version.
-
#ping ⇒ Object
Tests the connection with Colore.
-
#request_conversion(doc_id:, filename:, action:, version: Colore::CURRENT, callback_url: nil) ⇒ Hash
Requests a conversion of an existing document.
-
#update_document(doc_id:, filename:, content: nil, author: nil, actions: nil, callback_url: nil) ⇒ Hash
Updates the specified document on Colore - creates a new version and stores the new file.
-
#update_title(doc_id:, title:) ⇒ Hash
Updates the document title.
Constructor Details
#initialize(app:, base_uri:, logger: Logger.new(nil), backtrace: false, user_agent: 'Colore Client') ⇒ Client
Constructor.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/colore/client.rb', line 33 def initialize(app:, base_uri:, logger: Logger.new(nil), backtrace: false, user_agent: 'Colore Client') @base_uri = base_uri @app = app @backtrace = backtrace @logger = logger @connection = Faraday.new(url: base_uri, headers: { 'User-Agent' => user_agent }) do |faraday| faraday.request :multipart faraday.request :url_encoded faraday.adapter :net_http end end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
19 20 21 |
# File 'lib/colore/client.rb', line 19 def app @app end |
#backtrace ⇒ Object (readonly)
Returns the value of attribute backtrace.
19 20 21 |
# File 'lib/colore/client.rb', line 19 def backtrace @backtrace end |
#base_uri ⇒ Object (readonly)
Returns the value of attribute base_uri.
19 20 21 |
# File 'lib/colore/client.rb', line 19 def base_uri @base_uri end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
19 20 21 |
# File 'lib/colore/client.rb', line 19 def logger @logger end |
Class Method Details
.generate_doc_id ⇒ Object
Generates a document id that is reasonably guaranteed to be unique for your app.
22 23 24 |
# File 'lib/colore/client.rb', line 22 def self.generate_doc_id SecureRandom.uuid end |
Instance Method Details
#convert(content:, action:, language: 'en') ⇒ Hash
Performs a foreground conversion of a file.
213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/colore/client.rb', line 213 def convert(content:, action:, language: 'en') params = {} response = nil with_tempfile(content) do |io| params[:file] = file_param(io) params[:action] = action params[:language] = language if language params[:backtrace] = backtrace if backtrace response = send_request :post, 'convert', params end response end |
#create_document(doc_id:, filename:, content:, title: nil, author: nil, actions: nil, callback_url: nil) ⇒ Hash
Stores the specified document on Colore.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/colore/client.rb', line 72 def create_document(doc_id:, filename:, content:, title: nil, author: nil, actions: nil, callback_url: nil) params = {} params[:title] = title if title params[:actions] = actions if actions params[:author] = if params[:callback_url] = callback_url if callback_url params[:backtrace] = backtrace if backtrace base_filename = File.basename(filename) response = nil with_tempfile(content) do |io| params[:file] = file_param(io) response = send_request :put, "#{url_for_base doc_id}/#{encode_param(base_filename)}", params, :json end response end |
#delete_document(doc_id:) ⇒ Hash
Completely deletes a document
160 161 162 163 164 |
# File 'lib/colore/client.rb', line 160 def delete_document(doc_id:) params = {} params[:backtrace] = backtrace if backtrace send_request :delete, url_for_base(doc_id), params, :json end |
#delete_version(doc_id:, version:) ⇒ Hash
Completely deletes a document’s version (you cannot delete the current one)
172 173 174 175 176 |
# File 'lib/colore/client.rb', line 172 def delete_version(doc_id:, version:) params = {} params[:backtrace] = backtrace if backtrace send_request :delete, "#{url_for_base doc_id}/#{version}", params, :json end |
#generate_doc_id ⇒ Object
Generates a document id that is reasonably guaranteed to be unique for your app.
46 47 48 |
# File 'lib/colore/client.rb', line 46 def generate_doc_id self.class.generate_doc_id end |
#get_document(doc_id:, filename:, version: Colore::CURRENT) ⇒ String
Retrieves a document.
Please note that this method puts unwanted load on the Colore service and it is recommended that you access the document directly, using a proxying web server such as Nginx.
189 190 191 192 193 |
# File 'lib/colore/client.rb', line 189 def get_document(doc_id:, filename:, version: Colore::CURRENT) params = {} params[:backtrace] = backtrace if backtrace send_request :get, path_for(doc_id, filename, version), {} end |
#get_document_info(doc_id:) ⇒ Hash
Retrieves information about a document.
200 201 202 203 204 |
# File 'lib/colore/client.rb', line 200 def get_document_info(doc_id:) params = {} params[:backtrace] = backtrace if backtrace send_request :get, url_for_base(doc_id), params, :json end |
#path_for(doc_id, filename, version = Colore::CURRENT) ⇒ String
Generates a path for the document based on its ID, filename, and version.
233 234 235 |
# File 'lib/colore/client.rb', line 233 def path_for(doc_id, filename, version = Colore::CURRENT) "#{url_for_base doc_id}/#{version}/#{File.basename(filename)}" end |
#ping ⇒ Object
Tests the connection with Colore. Will raise an error if the connection cannot be established.
52 53 54 55 |
# File 'lib/colore/client.rb', line 52 def ping send_request :head, '/' true end |
#request_conversion(doc_id:, filename:, action:, version: Colore::CURRENT, callback_url: nil) ⇒ Hash
Requests a conversion of an existing document.
147 148 149 150 151 152 153 |
# File 'lib/colore/client.rb', line 147 def request_conversion(doc_id:, filename:, action:, version: Colore::CURRENT, callback_url: nil) params = {} params[:callback_url] = callback_url if callback_url params[:backtrace] = backtrace if backtrace send_request :post, "#{path_for(doc_id, filename, version)}/#{action}", params, :json end |
#update_document(doc_id:, filename:, content: nil, author: nil, actions: nil, callback_url: nil) ⇒ Hash
Updates the specified document on Colore - creates a new version and stores the new file.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/colore/client.rb', line 104 def update_document(doc_id:, filename:, content: nil, author: nil, actions: nil, callback_url: nil) params = {} params[:actions] = actions if actions params[:author] = if params[:callback_url] = callback_url if callback_url params[:backtrace] = backtrace if backtrace base_filename = File.basename(filename) response = nil if content with_tempfile(content) do |io| params[:file] = file_param(io) response = send_request :post, "#{url_for_base(doc_id)}/#{encode_param(base_filename)}", params, :json end else response = send_request :post, "#{url_for_base(doc_id)}/#{encode_param(base_filename)}", params, :json end response end |
#update_title(doc_id:, title:) ⇒ Hash
Updates the document title.
131 132 133 |
# File 'lib/colore/client.rb', line 131 def update_title(doc_id:, title:) send_request :post, "#{url_for_base(doc_id)}/title/#{encode_param(title)}", {}, :json end |