Class: BlackStack::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/functions.rb

Overview

Base class. List of methods you have to overload if you develop a profile type.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h) ⇒ Base

Returns a new instance of Base.



96
97
98
# File 'lib/functions.rb', line 96

def initialize(h)
    self.desc = h
end

Instance Attribute Details

#descObject

object json descriptor



90
91
92
# File 'lib/functions.rb', line 90

def desc
  @desc
end

Class Method Details

.account_value(id_account: nil, field:) ⇒ Object

sysowner must provide the id_account for getting an account value. for non sysowner it is assigned to his account.



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/functions.rb', line 108

def self.(id_account:nil, field:)
    params = {}
    params['id_account'] = 
    params['field'] = field
    # call the API
    ret = BlackStack::API.post(
        endpoint: "account_value",
        params: params
    )
    raise "Error calling account_value endpoint: #{ret['status']}" if ret['status'] != 'success'
    return ret['result']
end

.countObject

Get array of hash descriptors of profiles.

Parameters:

  • id: guid. Id of the profile to bring.



154
155
156
157
158
159
160
161
162
163
# File 'lib/functions.rb', line 154

def self.count
    params = {}
    params['backtrace'] = BlackStack::API.backtrace
    ret = BlackStack::API.post(
        endpoint: "#{self.object_name}/count",
        params: params
    )
    raise "Error calling count endpoint: #{ret['status']}" if ret['status'] != 'success'
    return ret['result'].to_i
end

.delete(id) ⇒ Object

Delete object.

Parameters:

  • id: guid. Id of the profile to bring.



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/functions.rb', line 187

def self.delete(id)
    params = {}
    params['id'] = id
    params['backtrace'] = BlackStack::API.backtrace
    ret = BlackStack::API.post(
        endpoint: "#{self.object_name}/delete",
        params: params
    )
    raise "Error calling get endpoint: #{ret['status']}" if ret['status'] != 'success'
    return self.new(ret['result']).child_class_instance
end

.get(id) ⇒ Object

Get array of hash descriptors of profiles.

Parameters:

  • id: guid. Id of the profile to bring.



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/functions.rb', line 170

def self.get(id)
    params = {}
    params['id'] = id
    params['backtrace'] = BlackStack::API.backtrace
    ret = BlackStack::API.post(
        endpoint: "#{self.object_name}/get",
        params: params
    )
    raise "Error calling get endpoint: #{ret['status']}" if ret['status'] != 'success'
    return self.new(ret['result']).child_class_instance
end

.insert(desc) ⇒ Object

Submit a hash descriptor to the server for an insert



241
242
243
244
245
246
247
248
249
250
251
# File 'lib/functions.rb', line 241

def self.insert(desc)
    params = {}
    params['desc'] = desc
    params['backtrace'] = BlackStack::API.backtrace
    ret = BlackStack::API.post(
        endpoint: "#{self.object_name}/insert",
        params: params
    )
    raise "Error calling insert endpoint: #{ret['status']}" if ret['status'] != 'success'
    return self.new(ret['result']).child_class_instance
end

.object_nameObject



92
93
94
# File 'lib/functions.rb', line 92

def self.object_name
    raise 'You have to overload this method in your class.'
end

.page(id_account: nil, promiscuous: false, page:, limit:, filters: {}) ⇒ Object

Get array of hash descriptor of profile.

Parameters:

  • id_account: guid. Id of the account to bring the profiles. Sysowner must provide the id_account for getting an account value. For non sysowner it is assigned to his account.

  • promiscuous: boolean. It works only for Sysowner. If true, it will bring all non-deleted rows, including the ones that are not owned by Sysowner. If false, it will bring only rows matching id_profile. Default: false.

  • page: integer. Page number.

  • limit: integer. Number of profiles per page.

  • params: hash. Additional filter parameters used by the specific child class.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/functions.rb', line 131

def self.page(id_account: nil, promiscuous: false, page:, limit:, filters: {})
    # add page and limit to the params
    params = {}
    params['id_account'] = 
    params['promiscuous'] = promiscuous
    params['page'] = page
    params['limit'] = limit
    params['filters'] = filters
    params['backtrace'] = BlackStack::API.backtrace
    # call the API
    ret = BlackStack::API.post(
        endpoint: "#{self.object_name}/page",
        params: params
    )
    raise "Error calling page endpoint: #{ret['status']}" if ret['status'] != 'success'
    return ret['results'].map { |h| self.new(h).child_class_instance }
end

.update(desc) ⇒ Object

Submit a hash descriptor to the server for an update



201
202
203
204
205
206
207
208
209
210
211
# File 'lib/functions.rb', line 201

def self.update(desc)
    params = {}
    params['desc'] = desc
    params['backtrace'] = BlackStack::API.backtrace
    ret = BlackStack::API.post(
        endpoint: "#{self.object_name}/update",
        params: params
    )
    raise "Error calling update endpoint: #{ret['status']}" if ret['status'] != 'success'
    return self.new(ret['result']).child_class_instance
end

.update_status(desc) ⇒ Object

Submit a hash descriptor to the server for an update of status only



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/functions.rb', line 215

def self.update_status(desc)
  params = {}
  params['desc'] = desc
  params['backtrace'] = BlackStack::API.backtrace
  ret = BlackStack::API.post(
      endpoint: "#{self.object_name}/update_status",
      params: params
  )
  raise "Error calling update_status endpoint: #{ret['status']}" if ret['status'] != 'success'
  return
end

.upsert(desc) ⇒ Object

Submit a hash descriptor to the server for an upsert



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/functions.rb', line 256

def self.upsert(desc)
    params = {}
    params['desc'] = desc
    params['backtrace'] = BlackStack::API.backtrace
    ret = BlackStack::API.post(
        endpoint: "#{self.object_name}/upsert",
        params: params
    )
    raise "Error calling upsert endpoint: #{ret['status']}" if ret['status'] != 'success'
    return ret['result'] == {} ? nil : self.new(ret['result']).child_class_instance
end

.upsert2(desc) ⇒ Object

Submit a hash descriptor to the server for an upsert



270
271
272
273
274
275
276
277
278
279
280
# File 'lib/functions.rb', line 270

def self.upsert2(desc)
  params = {}
  params['desc'] = desc
  params['backtrace'] = BlackStack::API.backtrace
  ret = BlackStack::API.post(
      endpoint: "#{self.object_name}/upsert2",
      params: params
  )
  raise "Error calling upsert endpoint: #{ret['status']}" if ret['status'] != 'success'
  return ret['result'] == {} ? nil : self.new(ret['result']).child_class_instance
end

Instance Method Details

#child_class_instanceObject

Crate an instance of a child class using speicfications in the ‘desc` attribute. By default, returns the same instance.



102
103
104
# File 'lib/functions.rb', line 102

def child_class_instance
    return self
end

#updateObject

Submit a hash descriptor to the server for an update



229
230
231
# File 'lib/functions.rb', line 229

def update
    self.class.update(self.desc)
end

#update_statusObject

Submit a hash descriptor to the server for an update of status only



235
236
237
# File 'lib/functions.rb', line 235

def update_status
  self.class.update(self.desc)
end

#upsertObject

Submit a hash descriptor to the server for an upsert



284
285
286
# File 'lib/functions.rb', line 284

def upsert
    self.class.upsert(self.desc)
end

#zyte_html(url, api_key:, options:, data_filename:) ⇒ Object

return the HTML of a page downloaded by Zyte.

Parameters:

  • url: the URL of the page to download.

  • api_key: the Zyte API key.

  • options: the options to pass to Zyte.



296
297
298
299
300
301
302
303
# File 'lib/functions.rb', line 296

def zyte_html(url, api_key:, options:, data_filename:)
    ret = nil
    # getting the HTML
    zyte = ZyteClient.new(key: api_key)
    html = zyte.extract(url: url, options: options, data_filename: data_filename)
    # return the URL of the file in the cloud
    return html
end

#zyte_snapshot(url, api_key:, options:, data_filename:, dropbox_folder: nil, retry_times: 3) ⇒ Object

create a file in the cloud with the HTML of a page downloaded by Zyte. return the URL of the file.

Parameters:

  • url: the URL of the page to download.

  • api_key: the Zyte API key.

  • options: the options to pass to Zyte.

  • dropbox_folder: the folder in the cloud where to store the file. If nil, it will use the self.desc value.

  • retry_times: the number of times to retry the download until the HTML is valid.



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/functions.rb', line 315

def zyte_snapshot(url, api_key:, options:, data_filename:, dropbox_folder:nil, retry_times: 3)
    # "The garbage character must be due to the 520 error code which was caused on the second request."
    garbage = "\x9E\xE9e"

    ret = nil
    raise "Either dropbox_folder parameter or self.desc['id_account'] are required." if dropbox_folder.nil? && self.desc['id_account'].nil?
    dropbox_folder = self.desc['id_account'] if dropbox_folder.nil?
    # build path to the local file in /tmp
    id = SecureRandom.uuid
    filename = "#{id}.html"
    tmp_path = "/tmp/#{filename}"
    # build path to the file in the cloud
    year = Time.now.year.to_s.rjust(4,'0')
    month = Time.now.month.to_s.rjust(2,'0')
    dropbox_folder = "/massprospecting.bots/#{dropbox_folder}.#{year}.#{month}"
    dropbox_path = "#{dropbox_folder}/#{filename}"
    # getting the HTML - Retry mechanism
    zyte = ZyteClient.new(key: api_key)
    try = 0
    html = garbage
    while try < retry_times && html == garbage
        html = zyte.extract(url: url, options: options, data_filename: data_filename)
        try += 1
    end
    # save the HTML in the local file in /tmp
    File.open(tmp_path, 'w') { |file| file.write(html) }
    # create the folder in the cloud and upload the file
    BlackStack::DropBox.dropbox_create_folder(dropbox_folder)
    BlackStack::DropBox.dropbox_upload_file(tmp_path, dropbox_path)
    # delete the local file
    File.delete(tmp_path)
    # return the URL of the file in the cloud
    return BlackStack::DropBox.get_file_url(dropbox_path)
end