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:, 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.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/colore/client.rb', line 36 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.
22 23 24 |
# File 'lib/colore/client.rb', line 22 def app @app end |
#backtrace ⇒ Object (readonly)
Returns the value of attribute backtrace.
22 23 24 |
# File 'lib/colore/client.rb', line 22 def backtrace @backtrace end |
#base_uri ⇒ Object (readonly)
Returns the value of attribute base_uri.
22 23 24 |
# File 'lib/colore/client.rb', line 22 def base_uri @base_uri end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
22 23 24 |
# File 'lib/colore/client.rb', line 22 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.
25 26 27 |
# File 'lib/colore/client.rb', line 25 def self.generate_doc_id SecureRandom.uuid end |
Instance Method Details
#convert(content:, action:, language: 'en') ⇒ Hash
Performs a foreground conversion of a file.
212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/colore/client.rb', line 212 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.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/colore/client.rb', line 75 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
159 160 161 162 163 |
# File 'lib/colore/client.rb', line 159 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)
171 172 173 174 175 |
# File 'lib/colore/client.rb', line 171 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.
49 50 51 |
# File 'lib/colore/client.rb', line 49 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.
188 189 190 191 192 |
# File 'lib/colore/client.rb', line 188 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.
199 200 201 202 203 |
# File 'lib/colore/client.rb', line 199 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.
232 233 234 |
# File 'lib/colore/client.rb', line 232 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.
55 56 57 58 |
# File 'lib/colore/client.rb', line 55 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.
146 147 148 149 150 151 152 |
# File 'lib/colore/client.rb', line 146 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:, author: nil, actions: nil, callback_url: nil) ⇒ Hash
Updates the specified document on Colore - creates a new version and stores the new file.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/colore/client.rb', line 107 def update_document(doc_id:, filename:, content:, 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 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 response end |
#update_title(doc_id:, title:) ⇒ Hash
Updates the document title.
130 131 132 |
# File 'lib/colore/client.rb', line 130 def update_title(doc_id:, title:) send_request :post, "#{url_for_base(doc_id)}/title/#{encode_param(title)}", {}, :json end |