Class: Astroboa::Client

Inherits:
Object show all
Defined in:
lib/astroboa-rb/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(astroboaIPAddressOrFQDN, repository, username = nil, password = nil) ⇒ Client

Returns a new instance of Client.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/astroboa-rb/client.rb', line 25

def initialize(astroboaIPAddressOrFQDN, repository, username = nil , password = nil)
  # the IP or Fully Qualified Domain Name of the server that provides the astroboa data services 
  @astroboaIPAddressOrFQDN = astroboaIPAddressOrFQDN
  
  # the id of the repository from which we want to read or write resources (multiple repositories can be served by each astroboa server)
  @repository = repository
  
  @username = username
  @password = password
  
  @resourceApiBasePath = "http://#{astroboaIPAddressOrFQDN}/resource-api"
  
  # this resource is about all repositories and does not require a repository to be specified
  @repositoriesResource = RestClient::Resource.new(@resourceApiBasePath, {:user => username, :password => password, :headers => astroboa_client_headers})
  
  # all other resources require a repository to be specified
  create_repository_resources(repository)

  @log = Logger.new('/tmp/astroboa-rb-log.txt')
  @log.level = Logger::WARN

  RestClient.log = @log
  
  # some json payloads have very deep nesting and the JSON parser option should be set to allow deeper nesting
  # the default parser nesting is 20. We set it to 50 and the user may set it to a higher level through :json_options accessor
  @json_options = {:max_nesting => 50}
  
end

Instance Attribute Details

#astroboaIPAddressOrFQDNObject (readonly)

Returns the value of attribute astroboaIPAddressOrFQDN.



21
22
23
# File 'lib/astroboa-rb/client.rb', line 21

def astroboaIPAddressOrFQDN
  @astroboaIPAddressOrFQDN
end

#json_optionsObject

get and set options for the JSON parser by default the nesting level is raised from 20 to 50



19
20
21
# File 'lib/astroboa-rb/client.rb', line 19

def json_options
  @json_options
end

#modelsApiPathObject (readonly)

Returns the value of attribute modelsApiPath.



22
23
24
# File 'lib/astroboa-rb/client.rb', line 22

def modelsApiPath
  @modelsApiPath
end

#modelsResourceObject (readonly)

Returns the value of attribute modelsResource.



23
24
25
# File 'lib/astroboa-rb/client.rb', line 23

def modelsResource
  @modelsResource
end

#objectsApiPathObject (readonly)

Returns the value of attribute objectsApiPath.



22
23
24
# File 'lib/astroboa-rb/client.rb', line 22

def objectsApiPath
  @objectsApiPath
end

#objectsResourceObject (readonly)

Returns the value of attribute objectsResource.



23
24
25
# File 'lib/astroboa-rb/client.rb', line 23

def objectsResource
  @objectsResource
end

#repositoryObject

Returns the value of attribute repository.



21
22
23
# File 'lib/astroboa-rb/client.rb', line 21

def repository
  @repository
end

#resourceApiBasePathObject (readonly)

Returns the value of attribute resourceApiBasePath.



22
23
24
# File 'lib/astroboa-rb/client.rb', line 22

def resourceApiBasePath
  @resourceApiBasePath
end

#taxonomiesApiPathObject (readonly)

Returns the value of attribute taxonomiesApiPath.



22
23
24
# File 'lib/astroboa-rb/client.rb', line 22

def taxonomiesApiPath
  @taxonomiesApiPath
end

#taxonomiesResourceObject (readonly)

Returns the value of attribute taxonomiesResource.



23
24
25
# File 'lib/astroboa-rb/client.rb', line 23

def taxonomiesResource
  @taxonomiesResource
end

#topicsApiPathObject (readonly)

Returns the value of attribute topicsApiPath.



22
23
24
# File 'lib/astroboa-rb/client.rb', line 22

def topicsApiPath
  @topicsApiPath
end

#topicsResourceObject (readonly)

Returns the value of attribute topicsResource.



23
24
25
# File 'lib/astroboa-rb/client.rb', line 23

def topicsResource
  @topicsResource
end

#usernameObject (readonly)

Returns the value of attribute username.



21
22
23
# File 'lib/astroboa-rb/client.rb', line 21

def username
  @username
end

Class Method Details

.versionObject



13
14
15
# File 'lib/astroboa-rb/client.rb', line 13

def self.version
  Astroboa::VERSION
end

Instance Method Details

#astroboa_client_headersObject



311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/astroboa-rb/client.rb', line 311

def astroboa_client_headers
  if (defined? self.class.gem_version_string) 
    gem_version = self.class.gem_version_string
  else 
   gem_version = nil
  end
   
  {
    'X-Astroboa-API-Version' => '3.0.0',
    'User-Agent' => gem_version,
    'X-Ruby-Version' => RUBY_VERSION,
    'X-Ruby-Platform' => RUBY_PLATFORM
  }
end

#create_repository_resources(repository) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
# File 'lib/astroboa-rb/client.rb', line 327

def create_repository_resources(repository)
  @objectsApiPath = "#{@resourceApiBasePath}/#{repository}/objects"
  @taxonomiesApiPath = "#{@resourceApiBasePath}/#{repository}/taxonomies"
  @topicsApiPath = "#{@resourceApiBasePath}/#{repository}/topics"
  @modelsApiPath = "#{@resourceApiBasePath}/#{repository}/models"

  @objectsResource = RestClient::Resource.new(@objectsApiPath, {:user => @username, :password => @password, :headers => astroboa_client_headers})
  @taxonomiesResource = RestClient::Resource.new(@taxonomiesApiPath, {:user => @username, :password => @password, :headers => astroboa_client_headers})
  @topicsResource = RestClient::Resource.new(@topicsApiPath, {:user => @username, :password => @password, :headers => astroboa_client_headers})
  @modelsResource = RestClient::Resource.new(@modelsApiPath, {:user => @username, :password => @password, :headers => astroboa_client_headers})
end

#createAstroboaClientException(message, api_exception) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/astroboa-rb/client.rb', line 296

def createAstroboaClientException(message, api_exception)
  api_code = 'There is no astroboa api return code. The api call was not send due to the error'
  api_response = 'There is no astroboa api response. The api call was not send due to the error'
  if api_exception 
    message = "#{message} - Cause: #{api_exception.message}"
    if api_exception.respond_to?(:http_code)
      api_code = api_exception.http_code
    end
    if api_exception.respond_to?(:response)
      api_response = api_exception.response ||= 'The astroboa api call did not produce any response'
    end
  end
  Astroboa::ClientError.new(message, api_code, api_response)
end

#createObject(objectHash, &exception_block) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/astroboa-rb/client.rb', line 125

def createObject(objectHash, &exception_block)
  if !objectHash or objectHash.empty?
     message = "You should specify an non empty hash of the object you want to create."
      @log.error message
      handle_error(message, nil, &exception_block)
      return
  end
  
  if objectHash.has_key?('cmsIdentifier')
    message = "You have specified an object identifier. Object identifiers are automatically assigned by astroboato new objects. Please remove the identifier if this is a new object or use updateObject if it is an existing object."
    @log.warn message
    handle_error(message, nil, &exception_block)
  else
    begin
      response = @objectsResource.post objectHash.to_json, :content_type => :json 
      objectHash['cmsIdentifier'] = response.body.to_s
      return response.body.to_s
    rescue => api_exception
      @log.error "Astroboa createObject call failed. Error is #{api_exception.inspect}. The object to be created was: #{objectHash}."
      message = "Astroboa createObject call failed. The object to be created was: #{objectHash}."
      handle_error(message, api_exception, &exception_block)
    end 
  end 	
end

#getModel(objectTypeOrProperty = '', output = :hash, &exception_block) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/astroboa-rb/client.rb', line 232

def getModel(objectTypeOrProperty='', output = :hash, &exception_block)
  begin
    case output
    when :json
      acceptHeader = :json
      outputParam = 'json'
    when :xml
      acceptHeader = :xml
      outputParam = 'xml'
    when :hash, :object
      acceptHeader = :json
      outputParam = 'json'
    else
      message = "Astroboa API cannot return resources as #{output}. Please see the documentation for available output options."
      log.error message
      handle_error(message, nil, &exception_block)
    end
  
    response = nil
    params = {'output' => outputParam}
    
    if objectTypeOrProperty && !objectTypeOrProperty.empty?
      response = @modelsResource["#{objectTypeOrProperty}"].get :params => params, :accept => acceptHeader
    else 
      response = @modelsResource.get :params => params, :accept => acceptHeader
    end
    
    case output
    when :json, :xml
      return response.body
    when :hash
      return JSON.parse(response.body, @json_options)
    when :object
      objectHash = JSON.parse(response.body, @json_options)
      return objectHash.to_openstruct
    end 
  rescue => api_exception
    @log.error "Astroboa getModel call failed. Error is #{api_exception.inspect}. The requested object type or object property was: #{objectTypeOrProperty}."
    message = "Astroboa getModel call failed. The requested object type or property was: #{objectTypeOrProperty}."
    handle_error(message, api_exception, &exception_block)
  end
end

#getObject(idOrName, options = {}, &exception_block) ⇒ Object

API



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/astroboa-rb/client.rb', line 72

def getObject(idOrName, options = {}, &exception_block)
  
  if idOrName && !idOrName.empty?
    begin
      projectionPaths = options[:project]
      output = options[:output] ||= :hash
      
      case output
      when :json
        acceptHeader = :json
        outputParam = 'json'
      when :xml
        acceptHeader = :xml
        outputParam = 'xml'
      when :hash, :object
        acceptHeader = :json
        outputParam = 'json'
      else
        message = "Astroboa API cannot return resources as #{output}. Please see the documentation for available output options."
        log.error message
        handle_error(message, nil, &exception_block)
      end
    
      response = nil
      params = {'output' => outputParam}
      if projectionPaths
        params['projectionPaths'] = projectionPaths
      end
      
      response = @objectsResource["#{idOrName}"].get :params => params, :accept => acceptHeader
      
      case output
      when :json, :xml
        return response.body
      when :hash
        return JSON response.body
      when :object
        objectHash = JSON.parse(response.body, @json_options)
        return objectHash.to_openstruct
      end 
    rescue => api_exception
      @log.error "Astroboa getObject call failed. Error is #{api_exception.inspect}. The requested object id (or name) was: #{idOrName}."
      message = "Astroboa getObject call failed. The requested object id (or name) was: #{idOrName}."
      handle_error(message, api_exception, &exception_block)
    end
  else
    message = "You should specify the id or name of the object."
    @log.error message
    handle_error(message, nil, &exception_block)
  end      	
end

#getObjectCollection(query = nil, options = {}, &exception_block) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/astroboa-rb/client.rb', line 175

def getObjectCollection(query = nil, options = {}, &exception_block)
  begin
    projectionPaths = options[:project] 
    offset = options[:offset] ||= 0 
    limit = options[:limit] ||= 50 
    orderBy = options[:orderBy] ||= 'profile.modified desc'
    output = options[:output] ||= :hash
    
    case output
    when :json
      acceptHeader = :json
      outputParam = 'json'
    when :xml
      acceptHeader = :xml
      outputParam = 'xml'
    when :hash, :object
      acceptHeader = :json
      outputParam = 'json'
    else
      message = "Astroboa API cannot return resources as #{output}. Please see the documentation for available output options."
      log.error message
      handle_error(message, nil, &exception_block)
    end
  
    response = nil
    params = {'output' => outputParam}
  
    if query
      params['cmsQuery'] = query
    end
  
    if projectionPaths
      params['projectionPaths'] = projectionPaths
    end
  
    params['offset'] = offset
    params['limit'] = limit
    params['orderBy'] = orderBy
  
    response = @objectsResource.get :params => params, :accept => acceptHeader

    case output
    when :json, :xml
      return response.body
    when :hash
      return JSON.parse(response.body, @json_options)
    when :object
      objectHash = JSON.parse(response.body, @json_options)
      return objectHash.to_openstruct
    end 
  rescue => api_exception
    @log.error "Astroboa getObjectCollection call failed. Error is #{api_exception.inspect}. The requested collection query was: #{query}, with projection paths: #{projectionPaths}, offset: #{offset}, limit: #{limit}, orderBy: #{orderBy}."
    message = "Astroboa getObjectCollection call failed. The requested collection query was: #{query}, with projection paths: #{projectionPaths}, offset: #{offset}, limit: #{limit}, orderBy: #{orderBy}."
    handle_error(message, api_exception, &exception_block)
  end
end

#handle_error(message, api_exception, &exception_block) ⇒ Object

implementation



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/astroboa-rb/client.rb', line 283

def handle_error(message, api_exception, &exception_block)
  if block_given?
    astroboaClientError = createAstroboaClientException(message, api_exception)
    exception_block.call(astroboaClientError)
  elsif @error_callback
    astroboaClientError = createAstroboaClientException(message, api_exception)
    @error_callback.call(astroboaClientError)
  end

  return nil

end

#log=(logger) ⇒ Object

set your own logger



63
64
65
66
# File 'lib/astroboa-rb/client.rb', line 63

def log= (logger)
  @log = logger
  RestClient.log = logger
end

#on_error(&block) ⇒ Object



275
276
277
# File 'lib/astroboa-rb/client.rb', line 275

def on_error(&block)
    @error_callback = block
end

#updateObject(objectHash, updateLastModificationTime = true, &exception_block) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/astroboa-rb/client.rb', line 151

def updateObject(objectHash, updateLastModificationTime = true, &exception_block)
  if !objectHash or objectHash.empty?
     message = "You should specify an non empty hash of the object you want to update."
      @log.error message
      handle_error(message, nil, &exception_block)
      return
  end
  
  if objectHash.has_key?('cmsIdentifier')
    begin
      response = @objectsResource["#{objectHash['cmsIdentifier']}"].put objectHash.to_json, :params => {'updateLastModificationTime' => updateLastModificationTime}, :content_type => :json 
      return response.body.to_s
    rescue => api_exception
      @log.error "Astroboa updateObject call failed. Error is #{api_exception.inspect}. The object to be updated was: #{objectHash}."
      message = "Astroboa updateObject call failed. The object to be updated was: #{objectHash}."
      handle_error(message, api_exception, &exception_block)
    end
  else
    message = "You try to update an object that does not have an identifier. If your object is new then use createObject to create a new object."
    @log.error message
    handle_error(message, nil, &exception_block)
  end      	
end