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, interval = 0.5, &block) ⇒ Gini::Api::Document
Upload a document.
-
#version_header(type = @api_type) ⇒ Hash
Version accept header based on @api_version.
Constructor Details
#initialize(options = {}) ⇒ Client
Instantiate a new Gini::Api::Client object with OAuth capabilities
35 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 |
# File 'lib/gini-api/client.rb', line 35 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), }.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
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/gini-api/client.rb', line 182 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
199 200 201 |
# File 'lib/gini-api/client.rb', line 199 def get(id) Gini::Api::Document.new(self, "/documents/#{id}") end |
#list(options = {}) ⇒ Gini::Api::DocumentSet
List all documents
211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/gini-api/client.rb', line 211 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
95 96 97 98 |
# File 'lib/gini-api/client.rb', line 95 def login(opts) @oauth = Gini::Api::OAuth.new(self, opts) @token = @oauth.token end |
#logout ⇒ Object
Destroy OAuth2 token
102 103 104 |
# File 'lib/gini-api/client.rb', line 102 def logout @oauth.destroy end |
#register_parser ⇒ Object
Register OAuth2 response parser
73 74 75 76 77 78 79 80 |
# File 'lib/gini-api/client.rb', line 73 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 end |
#request(verb, resource, options = {}) ⇒ Object
Request wrapper that sets URI and accept header
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/gini-api/client.rb', line 124 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
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/gini-api/client.rb', line 236 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, interval = 0.5, &block) ⇒ Gini::Api::Document
Upload a document
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/gini-api/client.rb', line 155 def upload(file, interval = 0.5, &block) duration = Hash.new(0) # Document upload duration[:upload], response = upload_document(file) # 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) ⇒ Hash
Version accept header based on @api_version
112 113 114 |
# File 'lib/gini-api/client.rb', line 112 def version_header(type = @api_type) { accept: "application/vnd.gini.#{@api_version}+#{type}" } end |