Class: Ubiquity::Wiredrive::API::V2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ubiquity/wiredrive/api/v2/client.rb

Direct Known Subclasses

Utilities

Defined Under Namespace

Modules: Requests

Instance Attribute Summary collapse

API METHODS collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = { }) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 9

def initialize(args = { })
  require 'ubiquity/wiredrive/api/v2/client/requests'

  initialize_logger(args)
  initialize_http_client(args)
end

Instance Attribute Details

#http_clientObject

Returns the value of attribute http_client.



7
8
9
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 7

def http_client
  @http_client
end

#http_responseObject

Returns the value of attribute http_response.



7
8
9
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 7

def http_response
  @http_response
end

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 5

def logger
  @logger
end

#requestObject

Returns the value of attribute request.



7
8
9
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 7

def request
  @request
end

#responseObject

Returns the value of attribute response.



7
8
9
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 7

def response
  @response
end

Instance Method Details

#asset_create(args = { }, options = { }) ⇒ Object

################################################################################################################## #



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 105

def asset_create(args = { }, options = { })
  _request = Requests::BaseRequest.new(
    args,
    {
      :http_path => 'assets',
      :http_method => :post,
      :default_parameter_send_in_value => :body,
      :parameters => [
        :name,
        :description,
        :workflow,
        :is_folder,
        { :name => :parent, :aliases => [ :parentid ] },
        { :name => :project, :aliases => [ :projectid ] },
      ]
    }
  )
  # http_client.get('assets')
  _response = process_request(_request, options)

  _response.first if _response.is_a?(Array)
end

#asset_delete(args = { }, options = { }) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 146

def asset_delete(args = { }, options = { })
  _request = Requests::BaseRequest.new(
    args,
    {
      :http_path => 'assets/#{arguments[:id]}',
      :http_method => :delete,
      :parameters => [
        { :name => :id, :aliases => [ :assetid ], :send_in => :path },
      ]
    }
  )
  response = process_request(_request, options)
  response
end

#asset_get(args = { }, options = { }) ⇒ Object



161
162
163
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 161

def asset_get(args = { }, options = { })

end

#asset_primary_file_init(args = { }, options = { }) ⇒ Object Also known as: asset_upload_session_get



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 128

def asset_primary_file_init(args = { }, options = { })
  args = { :id => args } if args.is_a?(String)
  _request = Requests::BaseRequest.new(
    args,
    {
      :http_path => 'assets/#{arguments[:id]}/primary_file',
      :http_success_code => 202,
      :http_method => :post,
      :parameters => [
        { :name => :id, :aliases => [ :assetid ], :send_in => :path }
      ]
    }
  )
  process_request(_request, options)
  http_client.response['location']
end

#assets_get(args = { }, options = { }) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 165

def assets_get(args = { }, options = { })
  _request = Requests::BaseRequest.new(
    args,
    {
      :http_path => 'assets',
      :parameters => [
        :limit,
        :offset,
        :id,
        :name,
        :workflow,
        :is_folder,
        :is_active,
        :has_default_thumbs,
        :date_created,
        :date_modified,
        :created_by,
        { :name => :parent, :aliases => [ :parentid ] },
        { :name => :project, :aliases => [ :projectid ] },
        :height,
        :width,
        :mime_type,
        :include
      ]
    }
  )

  name = _request.arguments[:name]
  _request.arguments[:name] = CGI.escapeHTML(name) if name

  # http_client.get('assets')
  response = process_request(_request, options)
  response
end

#errorObject



78
79
80
81
82
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 78

def error
  _error = errors.first
  return _error.inspect if _error.is_a?(Hash)
  _error
end

#error_messageObject



84
85
86
87
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 84

def error_message
  _error = error
  _error.is_a?(Hash) ? (_error['message'] || _error) : _error
end

#errorsObject



89
90
91
92
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 89

def errors
  return (response['errors'] || [ ]) if response.is_a?(Hash)
  [ ]
end

#file_upload(args = { }, options = { }) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 200

def file_upload(args = { }, options = { })
  file_path = args[:file_path]
  file_name = args[:file_name] || File.basename(file_path)

  uri = args[:destination_uri]
  uri = URI(uri) if uri.is_a?(String)

  res = nil
  File.open(file_path) do |file|
    req = Net::HTTP::Post::Multipart.new uri.path,
                                         'file' => UploadIO.new(file, 'application/octet-stream', file_name)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    res = http.request(req)
  end

  res ? res.code.start_with?('2') : false
end

#initialize_http_client(args = { }) ⇒ Object



26
27
28
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 26

def initialize_http_client(args = { })
  @http_client = Ubiquity::Wiredrive::API::V2::HTTPClient.new(args)
end

#initialize_logger(args = { }) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 16

def initialize_logger(args = { })
  @logger = args[:logger] ||= Logger.new(args[:log_to] || STDOUT)
  log_level = args[:log_level]
  if log_level
    @logger.level = log_level
    args[:logger] = @logger
  end
  @logger
end

#process_request(request, options = nil) ⇒ Object

Parameters:

Options Hash (options):

  • :execute_request (Boolean) — default: true

    Will execute the request

  • :return_request (Boolean) — default: true

    Will return the request instance instead of nil. Only applies if execute_request is false.



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
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 35

def process_request(request, options = nil)
  # @paginator = nil
  @response = nil
  @request = request
  logger.warn { "Request is Missing Required Arguments: #{request.missing_required_arguments.inspect}" } unless request.missing_required_arguments.empty?

  # if ([:all, 'all'].include?(request.arguments[:_page]))
  #   request.arguments[:_page] = 1
  #   include_remaining_pages = true
  # else
  #   include_remaining_pages = false
  # end

  request.client = self unless request.client
  options ||= request.options

  return (options.fetch(:return_request, true) ? request : nil) unless options.fetch(:execute_request, true)

  #@response = http_client.call_method(request.http_method, { :path => request.path, :query => request.query, :body => request.body }, options)
  @response = request.execute

  # if include_remaining_pages
  #   return paginator.include_remaining_pages
  # end

  if @response.is_a?(Hash)
    _results = @response['results']
    return _results if _results
  end

  @response
end

#process_request_using_class(request_class, args = { }, options = { }) ⇒ Object

def paginator

@paginator ||= Paginator.new(self) if @response

end



72
73
74
75
76
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 72

def process_request_using_class(request_class, args = { }, options = { })
  @response = nil
  @request = request_class.new(args, options)
  process_request(request, options)
end

#project_create(args = { }, options = { }) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 219

def project_create(args = { }, options = { })
  _request = Requests::BaseRequest.new(
    args,
    {
      :http_path => 'projects',
      :http_method => :post,
      :default_parameter_send_in_value => :body,
      :parameters => [
        :name,
        :description,
        :code,
        :manager,
        :notify_manager,
        :is_active,
      ]
    }
  )
  _response = process_request(_request, options)

  _response.first if _response.is_a?(Array)
end

#project_delete(args = { }, options = { }) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 241

def project_delete(args = { }, options = { })
  _request = Requests::BaseRequest.new(
    args,
    {
      :http_path => 'projects/#{arguments[:id]}',
      :http_method => :delete,
      :parameters => [
        { :name => :id, :aliases => [ :projectid ], :send_in => :path },
      ]
    }
  )
  response = process_request(_request, options)
  response
end

#project_get(args = { }, options = { }) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 256

def project_get(args = { }, options = { })
  _request = Requests::BaseRequest.new(
    args,
    {
      :http_path => 'projects/#{arguments[:id]}',
      :parameters => [
        { :name => :id, :send_in => :path },
        :q,
        :include
      ]
    }
  )
  response = process_request(_request, options)
  response
end

#projects_get(args = { }, options = { }) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 272

def projects_get(args = { }, options = { })
  _request = Requests::BaseRequest.new(
    args,
    {
      :http_path => 'projects',
      :parameters => [
        :id,
        :name,
        :code,
        :notify_manager,
        :is_active,
        :date_last_accessed,
        :date_created,
        :date_modified,
        :date_deleted,
        :manager
      ]
    }
  )

  name = _request.arguments[:name]
  _request.arguments[:name] = CGI.escapeHTML(name) if name

  response = process_request(_request, options)
  response
end

#success?Boolean

Returns:

  • (Boolean)


94
95
96
97
98
99
100
# File 'lib/ubiquity/wiredrive/api/v2/client.rb', line 94

def success?
  return unless request
  return request.success? if request.respond_to?(:success?)

  _code = http_client.response.code
  _code and _code.start_with?('2')
end