Class: Gini::Api::Client
- Inherits:
-
Object
- Object
- Gini::Api::Client
- Defined in:
- lib/gini-api/client.rb
Overview
Main class to operate on the Gini API
Instance Attribute Summary collapse
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
-
#delete(id) ⇒ Object
Delete document.
-
#get(id) ⇒ Gini::Api::Document
Get document by Id.
-
#initialize(options = {}) ⇒ Client
constructor
Instantiate a new Gini::Api::Client object with OAuth capabilities.
-
#list(options = {}) ⇒ Gini::Api::DocumentSet
List all documents.
-
#login(opts) ⇒ Object
Acquire OAuth2 token and popolate @oauth (instance of Gini::Api::OAuth.new) and @token (OAuth2::AccessToken).
-
#logout ⇒ Object
Destroy OAuth2 token.
-
#register_parser ⇒ Object
Register OAuth2 response parser.
-
#request(verb, resource, options = {}) ⇒ Object
Request wrapper that sets URI and accept header.
-
#search(query, options = {}) ⇒ Gini::Api::DocumentSet
Fulltext search for documents.
-
#upload(file, doctype_hint = nil, interval = 0.5, &block) ⇒ Gini::Api::Document
Upload a document.
-
#version_header(type = @api_type, version = @api_version) ⇒ Hash
Version accept header based on @api_version.
Constructor Details
#initialize(options = {}) ⇒ Client
Instantiate a new Gini::Api::Client object with OAuth capabilities
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/gini-api/client.rb', line 36 def initialize( = {}) opts = { oauth_site: 'https://user.gini.net/', oauth_redirect: 'http://localhost', api_uri: 'https://api.gini.net', api_version: 'v1', api_type: 'json', upload_timeout: 90, processing_timeout: 180, log: Logger.new(STDOUT), user_agent: "gini-api-ruby/#{VERSION} (Faraday v#{Faraday::VERSION})" }.merge() # Ensure mandatory keys are set [:client_id, :client_secret].each do |k| raise Gini::Api::Error.new("Mandatory option key is missing: #{k}") unless opts.key?(k) end # Populate instance variables from merged opts opts.each do |k, v| instance_variable_set("@#{k}", v) self.class.send(:attr_reader, k) end # Ensure STDOUT is flushed STDOUT.sync = true # Sanitize api_uri @api_uri.sub!(/(\/)+$/, '') # Register parser (json+xml) based on API version register_parser @log.info('Gini API client initialized') @log.info("Target: #{@api_uri}") end |
Instance Attribute Details
#log ⇒ Object (readonly)
Returns the value of attribute log.
14 15 16 |
# File 'lib/gini-api/client.rb', line 14 def log @log end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
14 15 16 |
# File 'lib/gini-api/client.rb', line 14 def token @token end |
Instance Method Details
#delete(id) ⇒ Object
Delete document
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/gini-api/client.rb', line 191 def delete(id) response = request(:delete, "/documents/#{id}") unless response.status == 204 raise Gini::Api::DocumentError.new( "Deletion of docId #{id} failed (code=#{response.status})", response ) end @log.info("Deleted document #{id}") end |
#get(id) ⇒ Gini::Api::Document
Get document by Id
208 209 210 |
# File 'lib/gini-api/client.rb', line 208 def get(id) Gini::Api::Document.new(self, "/documents/#{id}") end |
#list(options = {}) ⇒ Gini::Api::DocumentSet
List all documents
220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/gini-api/client.rb', line 220 def list( = {}) opts = { limit: 20, offset: 0 }.merge() limit = Integer(opts[:limit]) offset = Integer(opts[:offset]) response = request(:get, "/documents?limit=#{limit}&next=#{offset}") unless response.status == 200 raise Gini::Api::DocumentError.new( "Failed to get list of documents (code=#{response.status})", response ) end Gini::Api::DocumentSet.new(self, response.parsed) end |
#login(opts) ⇒ Object
Acquire OAuth2 token and popolate @oauth (instance of Gini::Api::OAuth.new) and @token (OAuth2::AccessToken). Supports 2 strategies: username/password and authorization code
100 101 102 103 |
# File 'lib/gini-api/client.rb', line 100 def login(opts) @oauth = Gini::Api::OAuth.new(self, opts) @token = @oauth.token end |
#logout ⇒ Object
Destroy OAuth2 token
107 108 109 |
# File 'lib/gini-api/client.rb', line 107 def logout @oauth.destroy end |
#register_parser ⇒ Object
Register OAuth2 response parser
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/gini-api/client.rb', line 75 def register_parser OAuth2::Response.register_parser(:gini_json, [version_header(:json)[:accept]]) do |body| MultiJson.load(body, symbolize_keys: true) rescue body end OAuth2::Response.register_parser(:gini_xml, [version_header(:xml)[:accept]]) do |body| MultiXml.parse(body) rescue body end OAuth2::Response.register_parser(:gini_incubator, [version_header(:json, :incubator)[:accept]]) do |body| MultiJson.load(body, symbolize_keys: true) rescue body end end |
#request(verb, resource, options = {}) ⇒ Object
Request wrapper that sets URI and accept header
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/gini-api/client.rb', line 130 def request(verb, resource, = {}) opts = { headers: version_header(.delete(:type) || @api_type) }.merge() timeout(@processing_timeout) do @token.send(verb.to_sym, resource_to_location(resource).to_s , opts) end rescue OAuth2::Error => e raise Gini::Api::RequestError.new( "API request failed: #{verb} #{resource} (code=#{e.response.status})", e.response ) rescue Timeout::Error => e raise Gini::Api::ProcessingError.new( "API request timed out: #{verb} #{resource} (#{e.})" ) end |
#search(query, options = {}) ⇒ Gini::Api::DocumentSet
Fulltext search for documents
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/gini-api/client.rb', line 245 def search(query, = {}) opts = { type: '', limit: 20, offset: 0 }.merge() query = URI.escape(query) type = URI.escape(opts[:type]) limit = Integer(opts[:limit]) offset = Integer(opts[:offset]) response = request(:get, "/search?q=#{query}&type=#{type}&limit=#{limit}&next=#{offset}") unless response.status == 200 raise Gini::Api::SearchError.new( "Search query failed with code #{response.status}", response ) end Gini::Api::DocumentSet.new(self, response.parsed) end |
#upload(file, doctype_hint = nil, interval = 0.5, &block) ⇒ Gini::Api::Document
Upload a document
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/gini-api/client.rb', line 164 def upload(file, doctype_hint = nil, interval = 0.5, &block) duration = Hash.new(0) # Document upload duration[:upload], response = upload_document(file, doctype_hint) # Start polling (0.5s) when document has been uploaded successfully if response.status == 201 doc = Gini::Api::Document.new(self, response.headers['location']) duration[:processing] = poll_document(doc, interval, &block) duration[:total] = duration.values.inject(:+) doc.duration = duration doc else fail Gini::Api::UploadError.new( "Document upload failed with HTTP code #{response.status}", response ) end end |
#version_header(type = @api_type, version = @api_version) ⇒ Hash
Version accept header based on @api_version
118 119 120 |
# File 'lib/gini-api/client.rb', line 118 def version_header(type = @api_type, version = @api_version) { accept: "application/vnd.gini.#{version}+#{type}" } end |